【问题标题】:Discord.js v13 the reaction role buttons not work after i restart the botDiscord.js v13 重启机器人后反应角色按钮不起作用
【发布时间】:2021-12-10 02:21:04
【问题描述】:

这是反应角色按钮命令它工作但在我重新启动机器人后它说此交互失败我该如何解决它?不可能吗?我使用的是 simpledjs 它工作但我无法更改消息,当用户获得他们的角色时,机器人将发送消息并且我想更改它但我不能所以我使用收集并且在我重新启动机器人后它不工作所以为什么我会问这个问题。

const Command = require("../Structures/Command.js");
const Discord = require('discord.js');
const { MessageButton } = require('discord.js');

module.exports = new Command({

    name: "reactionrolebuttons",
    description: "reaction role but button",
    async run(message, args, client, interaction, collect) {
        let embed = new Discord.MessageEmbed()
            .setTitle("Ping Roles:")
            .setColor("#0099ff")
            .setDescription("`Click some button bellow if you want get notified!`")

        const row = new Discord.MessageActionRow()
            .addComponents(
                new MessageButton()
                    .setCustomId('Acm')
                    .setLabel('Announcement')
                    .setStyle('SECONDARY')
                    .setEmoji("????"),
                new MessageButton()
                    .setCustomId('Gv')
                    .setLabel('Giveaways')
                    .setStyle('SECONDARY')
                    .setEmoji("????")
            )
        const m = await message.channel.send({ embeds: [embed], components: [row] });

        const iFilter = i => i.user.id === message.author.id;

        const collector = m.createMessageComponentCollector({ filter: iFilter, time: 60000 })

        collector.on('collect', async i => {
            if (i.customId === 'Acm') {
                const role = message.guild.roles.cache.get("901353036759310386");
                if (i.member.roles.cache?.has('901353036759310386')) {
                    i.member.roles.remove('901353036759310386')
                    i.reply({ content: `${role} was removed from you`, ephemeral: true });
                } else {
                    i.member.roles.add('901353036759310386')
                    i.reply({ content: `${role} was added to you`, ephemeral: true });
                }
            } else if (i.customId === 'Gv') {
                const role = message.guild.roles.cache.get("901370872827346975");
                if (i.member.roles.cache?.has('901370872827346975')) {
                    i.member.roles.remove('901370872827346975')
                    i.reply({ content: `${role} was removed from you`, ephemeral: true });
                } else {
                    i.member.roles.add('901370872827346975')
                    i.reply({ content: `${role} was added to you`, ephemeral: true });
                }
            }
        })
    }
});

【问题讨论】:

标签: javascript discord.js


【解决方案1】:

不要忘记在你的过滤器上添加 await i.deferUpdate()

此外,您的收集器似乎不再使用 await,如果您想使用 async,只需添加 await。

在收藏家

await i.reply("......")

在你的过滤器上

const filter = async i => {
  await i.deferUpdate()
  return i.customId && i.user.id === message.author.id;
}

[编辑] 您还需要在用户单击按钮后结束收集器

await i.reply..........
await collector.stop("complete")

如果您想继续使用该按钮进行下一次交互,顺便说一句,您不需要收集器,

只需将条件包装在interactionCreate事件上

client.on("interactionCreate", async (interaction) => {
 if (!interaction.isButton()) return;
  if(interaction.customId === "Acm") {
   \\Your code here
 }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多