discord.js |按钮在机器人重新启动后不起作用

ldfqzlk8  于 2021-09-23  发布在  Java
关注(0)|答案(2)|浏览(275)

按钮在执行命令后工作,但在重启机器人并按下按钮后,它说 interaction failed 这是我的票

const { MessageButton } = require('discord-buttons');

module.exports = {
    name: 'ticket-setup',
    aliases: ['close'],
    category: 'Miscellaneous',
    description: 'Makes a ticket embed',
    async execute(message, args, cmd, client, Discord){

        if(!message.member.permissions.has("MANAGE_CHANNELS")) return message.reply("Normies can't do this command")

        if (cmd == 'close') {
            if (!message.channel.name.includes('ticket-')) return message.channel.send('You cannot use that here!');
            message.channel.delete();
           }

        let title;
        let desc;
        let ticketMsg;

        const filter = msg => msg.author.id == message.author.id;
        let options = {
            max: 1
        };

        message.channel.send("What will the ticket title be?\nSay cancel to cancel")
        let col = await message.channel.awaitMessages(filter, options)
        if(col.first().content == 'cancel') return message.channel.send("Cancelled");
        title = col.first().content

        message.channel.send('What will the description be?\nSay cancel to cancel')
        let col2 = await message.channel.awaitMessages(filter, options)
        if(col2.first().content == 'cancel') return message.channel.send("Cancelled");
        desc = col2.first().content

        message.channel.send('What is the message that the user will see when they make a ticket?\nSay cancel to cancel')
        let col3 = await message.channel.awaitMessages(filter, options)
        if(col3.first().content == 'cancel') return message.channel.send("Cancelled");
        ticketMsg = col3.first().content

        const setupEmbed =  new Discord.MessageEmbed()
        .setTitle(title)
        .setDescription(desc)
        .setFooter(message.guild.name, message.guild.iconURL({ dynamic: true }))
        .setColor('00f8ff')

        const hiEmbed = new Discord.MessageEmbed()
        .addField(ticketMsg, "\n\nDo a.close or press the button to close the ticket")
        .setColor("RANDOM")
        .setTimestamp()

        const createTicketButton = new MessageButton()
        .setID("ticketCreate")
        .setStyle("blurple")
        .setLabel("📨");

        const closeTicketButton = new MessageButton()
        .setID("ticketClose")
        .setLabel("Close ticket")
        .setStyle("red");

        if(cmd == 'ticket-setup'){ 
        message.channel.send({embed: setupEmbed, button: createTicketButton })      
        }

           client.on('clickButton', async (button) => {
            await button.clicker.fetch();
            await button.reply.defer();
               const user = button.clicker.user
            if (button.id === 'ticketCreate') {
              button.guild.channels.create(`ticket-${user.id}`,  {
                permissionOverwrites: [
                 {
                  id: user.id,
                  allow: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
                 },
                 {
                  id: button.message.guild.roles.everyone,
                  deny: ['VIEW_CHANNEL'],
                 },
                ],
                type: 'text',
               }).then(async (channel) =>{
                   channel.send({embed: hiEmbed, button: closeTicketButton })
               })
            } else if(button.id == 'ticketClose'){
                button.message.channel.delete()
            }
          });
    }
}

我用这个包裹 discord-buttons 文档链接
我尝试将clickbutton事件放入我的事件处理程序中,但由于出现了很多错误,因此没有成功。如何使按钮在重新启动后仍能工作?

iqjalb3h

iqjalb3h1#

我也有这个问题,但不是互动失败,我得到了不完整的按钮信息。

client.ws.on('INTERACTION_CREATE', interaction => {
//complete interaction
})

按钮是触发此操作的交互,您可以检查这是斜杠命令还是带有 interaction.data.custom_id (这可能是错误的,我无法测试它)。如果它不是按钮,它将是未定义的,但如果它是按钮,它将保留按钮的自定义id。

vnzz0bqm

vnzz0bqm2#

问题
机器人重新启动后按钮无法工作的原因是 client.on("clickButton") 事件处理程序位于“票证设置”命令的代码中。这意味着您的事件仅在bot重新启动后使用ticket setup命令时设置,或者换句话说,一次 execute() 在bot启动后对此文件调用。
想想这个:你的 client.on("clickButton") 直到您的 ticket-setup 司令部 execute() 函数被调用。这将给您带来多个问题。首先,如上所述 clickButton 在使用之前,事件甚至不会被处理 ticket-setup 在bot启动后至少一次。其次,这将在每次使用该命令时创建一个额外的事件处理程序。换句话说,如果你要使用 ticket-setup 命令两次或两次以上,代码在 clickButton 处理程序将在每次单击按钮时执行多次(在特定场景中,它将在每次单击按钮时创建多个票证)。
解决方案
你面临的问题有一个非常简单的解决方法。你只需要移动你的整个身体 clickButton 事件处理程序超出 execute() 方法。也许把它移到你的主楼里 server.jsbot.js 文件,与您的 client.on("ready")client.on("message") 事件处理程序。这将确保 clickButton 事件处理程序只设置一次,并且在bot启动时立即设置。
但是,请注意,您确实需要对 clickButton 事件处理程序,以确保其正常工作。您需要为您的应用程序移动代码 hiEmbedcloseTicketButton 进入你的 client.on("clickButton") 处理程序。
下面是这可能看起来的样子 server.js ,根据您问题中的代码:

client.on('clickButton', async (button) => {        
    await button.clicker.fetch();
    await button.reply.defer();
    const user = button.clicker.user;

    const hiEmbed = new Discord.MessageEmbed()
    .addField(ticketMsg, "\n\nDo a.close or press the button to close the ticket")
    .setColor("RANDOM")
    .setTimestamp();

    const closeTicketButton = new MessageButton()
    .setID("ticketClose")
    .setLabel("Close ticket")
    .setStyle("red");

    if (button.id === 'ticketCreate') {
        button.guild.channels.create(`ticket-${user.id}`,  {
            permissionOverwrites: [
            {
                id: user.id,
                allow: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
            },
            {
                id: button.message.guild.roles.everyone,
                deny: ['VIEW_CHANNEL'],
            },
            ],
            type: 'text',
        }).then(async (channel) =>{
            channel.send({embed: hiEmbed, button: closeTicketButton })
        })
    } else if(button.id == 'ticketClose'){
        button.message.channel.delete()
    }
});

您可能已经注意到了这方面的另一个问题: ticketMsg 将不定义变量。您还需要进行更改以解决该问题。我建议保存 ticketMsg 访问json文件或数据库,并在 client.on("clickButton") . 如果此代码表示正确的票务系统,则无论您是否使用此解决方案,您都需要执行此操作,否则您的用户将需要使用 ticket-setup 在每次机器人重新启动时重新设置票务系统。

相关问题