使用 Discord 按钮的建议命令 Discord.JS
Posted
技术标签:
【中文标题】使用 Discord 按钮的建议命令 Discord.JS【英文标题】:Suggestion Command Using Discord Buttons Discord.JS 【发布时间】:2022-01-24 00:51:04 【问题描述】:所以我正在尝试使用 Discord.JS 中的不和谐按钮提出建议命令。当我运行命令时,嵌入和按钮发送,但每当我点击其中一个按钮时,无论是 Upvote、Maybe 还是 Downvote 它编辑嵌入,但它从不更新数字。我试过upvote_number ++
和upvote_number + 1
但它不起作用。如果有人可以帮助我,那就太棒了。谢谢。
const Discord = require('discord.js');
const disbut = require('discord-buttons');
const MessageActionRow, MessageButton = require("discord-buttons");
const Color, Prefix = require("../../config.js");
const MessageEmbed = require("discord.js");
const disbutpages = require("discord-embeds-pages-buttons")
const db = require('quick.db');
module.exports =
name: "suggest",
aliases: [],
description: "Suggestion Command",
usage: "^suggest <suggestion>",
run: async(client, message, args) =>
const SayMessage = message.content.slice(8).trim();
let upvote_number = 0
let downvote_number = 0
let maybe_number = 0
const embed = new Discord.MessageEmbed()
.setAuthor("Suggestion from: " + message.author.tag, message.author.displayAvatarURL(dynamic: true))
.setThumbnail(message.author.displayAvatarURL(dynamic: true))
.setDescription(SayMessage)
.addField(`Votes`, `Upvote: **$upvote_number**
Downvote: **$downvote_number**`)
let upvotebutton = new MessageButton()
.setLabel(`Upvote`)
.setID(`upvote`)
.setEmoji(`⬆️`)
.setStyle("green")
let maybebutton = new MessageButton()
.setLabel(`Maybe`)
.setID(`maybe`)
.setEmoji(`????`)
.setStyle("blurple")
let downvotebutton = new MessageButton()
.setLabel(`Downvote`)
.setID(`downvote`)
.setEmoji(`⬇️`)
.setStyle("red")
let row = new MessageActionRow()
.addComponents(upvotebutton, maybebutton, downvotebutton)
const MESSAGE = await message.channel.send(embed, components: [row] )
const filter = ( button ) => button.clicker.user.id === message.author.id
const collector = MESSAGE.createButtonCollector(filter, time : 120000 );
collector.on('collect', async (b) =>
if(b.id == "upvote")
await upvote_number + 1
await MESSAGE.edit(embed, components: [row] );
await b.reply.defer()
if(b.id == "downvote")
downvote_number + 1
MESSAGE.edit(embed, components: [row] );
await b.reply.defer()
if(b.id == "maybe")
maybe_number + 1
MESSAGE.edit(embed, components: [row] );
await b.reply.defer()
)
collector.on('end', (b) =>
)
;
【问题讨论】:
【参考方案1】:您希望使用+=
运算符来回答您的问题。顺便说一句,由于按钮收集器的工作方式,建议会在一段时间后超时,这可能会在将来给您带来一些问题,因此您可能希望切换到事件。我唯一担心的其他问题是您使用的 d.js 12
和 discord-buttons
都已弃用。由于不和谐的 api 更新,d.js 12 很快就会失去支持,我强烈建议切换到 v13,因为它具有内置按钮,以及许多其他新功能,例如菜单和斜杠命令。
let upvote_number = 0
let downvote_number = 0
let maybe_number = 0
/* ... */
collector.on('collect', async b =>
if(b.id == "upvote")
upvote_number += 1
updateEmbed()
await MESSAGE.edit(embed, components: [row] )
await b.reply.defer()
else if (b.id == "downvote")
downvote_number += 1
updateEmbed()
await MESSAGE.edit(embed, components: [row] )
await b.reply.defer()
else if (b.id == "maybe")
maybe_number += 1
updateEmbed()
await MESSAGE.edit(embed, components: [row] )
await b.reply.defer()
);
updateEmbed()
embed.fields.find(i=>i.name==='Votes').value = `Upvote: **$upvote_number**
Downvote: **$downvote_number**`
tldr: Use += to set vars dynamically as well as update the embed object, and update to `discord.js@latest`
【讨论】:
感谢您的回答,我会确保更新到 Discord.JS V13,但它仍然没有更新嵌入中的数字。 嘿,所以我意识到我完全忘记更新嵌入对象了,我编辑了我原来的帖子,很抱歉。 谢谢!它起作用了:D以上是关于使用 Discord 按钮的建议命令 Discord.JS的主要内容,如果未能解决你的问题,请参考以下文章
使用 Discord py 使用 Discord 按钮编辑嵌入描述