bot 命令有效,但多次使用后崩溃。我收到错误“未知交互”

问题描述 投票:0回答:2

所以我刚刚完成了我的不和谐机器人的编码,除了一个命令之外,所有命令都可以顺利运行。 timeout.js 可以工作,但多次使用后整个机器人崩溃,我必须重新启动。不确定为什么。我希望声明中缺少“await”或缺少“;”。有人有什么想法吗?

const ms = require("ms")

module.exports = {
    name: "timeout",
    description: "This command puts a user in the server in timeout!",
    permission: "ADMINISTRATOR",
    options: [
        {
            name: "put",
            description: "Puts the user in timeout!",
            type: "SUB_COMMAND",
            options: [
                {
                    name: "member",
                    description: "Specify the member you want to put in timeout!",
                    type: "USER",
                    required: true
                },
                {
                    name: "duration",
                    description: "Specify the duration of the timeout!",
                    type: "STRING",
                    required: true
                },
                {
                    name: "reason",
                    description: "Specify the reason for this timeout!",
                    type: "STRING",
                    required: false
                }
            ]
        },
        {
            name: "remove",
            description: "This option removes the user from timeout!",
            type: "SUB_COMMAND",
            options: [
                {
                   name: "member",
                   description: "Specify the member you want to remove from timeout!",
                   type: "USER",
                   required: true 
                },
                {
                    name: "reason",
                    description: "Specify the reason you want to remove the timeout!",
                    type: "STRING",
                    required: false
                }
            ]
        }
    ],
    /**
     * 
     * @param {CommandInteraction} interaction 
     */
    async execute(interaction){

        const choice = interaction.options.getSubcommand()
        const user = interaction.options.getUser('member')
        const duration = interaction.options.getString('duration')
        const reason = interaction.options.getString('reason') ?? `No reason provided.`
        const member = interaction.guild.members.cache.get(user.id)

        if (user === interaction.member.user) return interaction.reply({
            embeds: [new MessageEmbed({
                color: '#5104DB',
                description: 'You can\'t use this command on yourself.'
            })], ephemeral: false
        })

        if (user === interaction.client.user) return interaction.reply({
            embeds: [new MessageEmbed({
                color: '#5104DB',
                description: 'You can\'t use this command on me.'
            })], ephemeral: false
        })

        if (user.bot === true) return interaction.reply({
            embeds: [new MessageEmbed({
                color: '#5104DB',
                description: 'You can\'t use this command on bots.'
            })], ephemeral: false
        })

        switch (choice) {
            case 'put':
                try {
                    const durationMs = ms(duration)
                    if (!durationMs) return interaction.reply({
                        embeds: [new MessageEmbed({
                            color: '#5104DB',
                            description: 'Please specify a valid duration for timeout.'
                        })], ephemeral: false
                    })
                    await interaction.reply({
                        embeds: [new MessageEmbed({
                            color: '#5104DB',
                            description: 'Putting user in timeout...'
                        })], ephemeral: false
                    })
                   await member.timeout(durationMs, reason)
                    interaction.editReply({
                        embeds: [new MessageEmbed({
                            color: '#5104DB',
                            description: `${user} is successfully put in timeout.\n\n**Duration:** ${duration}\n**Reason:** ${reason}`
                        })]
                    })
                } catch (e) {
                    await interaction.reply({
                        embeds: [new MessageEmbed({
                            color: '#5104DB',
                            description: `An error occurred:\n\n${e}`
                        })], ephemeral: false
                    })
                }
                break;
            case 'remove':
                try {
                    await interaction.reply({
                        embeds: [new MessageEmbed({
                            color: '#5104DB',
                            description: 'Removing user from timeout...'
                        })], ephemeral: false
                    })
                   await member.timeout(null, reason)
                    interaction.editReply({
                        embeds: [new MessageEmbed({
                            color: '#5104DB',
                            description: `${user} is successfully removed from timeout.\n\n**Reason:** ${reason}`
                        })]
                    })
                } catch (e) {
                    await interaction.reply({
                        embeds: [new MessageEmbed({
                            color: '#5104DB',
                            description: `An error occurred:\n\n${e}`
                        })], ephemeral: false
                    })
                }
                break;
        };
    },
};```
discord discord.js
2个回答
0
投票

可能有不同的原因:

  • 交互可能会在3秒后超时
  • 互动已回复

我建议首先使用

interaction.reply
,然后在消息准备好时使用
interaction.deferReply
,而不是使用
interaction.editReply

await interaction.deferReply();

const result = await longWaitingMethod(email);

await interaction.editReply({ 
   content: result.error || result.success, 
   ephemeral: true 
});

0
投票

如果您使用交互,请尝试以下操作:

await interaction.response.defer()

await interaction.edit_original_response("message")

这可以帮助您有时间找到要发布的答案。

© www.soinside.com 2019 - 2024. All rights reserved.