1

按钮在执行命令后工作,但在重新启动机器人并按下按钮后,它显示interaction failedHere's my ticket.js

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 事件放在我的事件处理程序中,但它没有工作,因为我得到了很多错误。即使重新启动后,我如何仍然使按钮工作?

4

2 回答 2

1

问题

机器人重新启动后按钮不起作用的原因是您的client.on("clickButton")事件处理程序位于“ticket-setup”命令的代码中。这意味着只有在机器人重新启动后使用 ticket-setup 命令时才会设置您的事件,或者换句话说,在机器人启动execute()后在此文件上调用一次。

想一想:在调用命令的函数client.on("clickButton")之前,不会到达您的代码。这会给您带来多个问题。首先,如上所述,直到您在机器人启动后至少使用一次之后,才会处理该事件。其次,这将在每次使用命令时创建一个额外的事件处理程序。换句话说,如果您要使用该命令两次或更多次,则处理程序中的代码将在每次单击按钮时执行多次(在您的特定场景中,每次单击按钮会创建多个票证)。ticket-setupexecute()clickButtonticket-setupticket-setupclickButton

解决方案

您面临的问题有一个非常简单的解决方案。您只需将整个clickButton事件处理程序移出execute()方法即可。也许将它与您的和事件处理程序一起移动到您的主文件server.jsbot.js文件中。这将确保事件处理程序只设置一次,并且在机器人启动时立即设置。client.on("ready")client.on("message")clickButton

但是请注意,您确实需要对clickButton事件处理程序进行一些细微的添加以确保其正常工作。您需要将代码移动到您的hiEmbed处理程序中。closeTicketButtonclient.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设置票务系统。

于 2021-07-12T14:19:01.817 回答
0

我也遇到了这个问题,但不是交互失败,而是按钮信息不完整。

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

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

于 2021-07-10T14:45:19.740 回答