首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Discord.js -错误: InteractionAlreadyReplied

Discord.js -错误: InteractionAlreadyReplied
EN

Stack Overflow用户
提问于 2022-09-20 11:46:19
回答 2查看 300关注 0票数 0

我最近开始添加一些音乐功能到我的机器人,我正在创建。然而,在尝试执行命令play时,我经常遇到一个问题。命令play返回的interaction.replyinteraction.followUp很少,但是只有满足IF语句时才能执行,还有一些只能执行interaction.deferReply

据我所见,所有回复大多在执行IF语句时运行,但它们都返回了答复,这意味着一旦返回完它们,它们就应该停止代码。但是机器人仍然告诉我交互已经被回复了,但是我不知道在哪里。

错误:

代码语言:javascript
运行
复制
C:\Users\xscriptxdata\Desktop\Bot\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:101
   if (this.deferred || this.replied) throw new Error(ErrorCodes.InteractionAlreadyReplied);
                                            ^

Error [InteractionAlreadyReplied]: The reply to this interaction has already been sent or deferred.
   at ChatInputCommandInteraction.reply (C:\Users\xscriptxdata\Desktop\Bot\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:101:46)
   at SlashCommands.invokeCommand (C:\Users\xscriptxdata\Desktop\Bot\node_modules\wokcommands\dist\SlashCommands.js:185:33)
   at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
 code: 'InteractionAlreadyReplied'
}

这是我的代码,机器人运行~

index.js:

代码语言:javascript
运行
复制
const DiscordJS = require('discord.js')
const WOKCommands = require('wokcommands')
const dotenv = require('dotenv')
const { Player } = require('discord-player')
dotenv.config()
const testServer = process.env.TEST_SERVERS.split(",") 

const client = new DiscordJS.Client({
    intents: [
        "Guilds",
        "GuildMessages",
        "GuildMessageReactions",
        "GuildVoiceStates",
        "GuildMembers"
    ],
})

const player = new Player(client, {
    autoSelfDeaf: true,
    leaveOnEmpty: true,
    leaveOnEmptyCooldown: 5,
    leaveOnEnd: false,
    leaveOnStop: false,
});
client.player = player;

client.on('ready', () => {
    console.log(`${client.user?.tag} is ready to work!`)

    const wok = new WOKCommands(client, {
        commandsDir: path.join(__dirname, 'cmds'),
        testServers: testServer,
        botOwners: process.env.BOT_OWNER,
        debug: true,
        ignoreBots: true,
        disabledDefaultCommands: [
            'help',
            'command',
            'language',
            'prefix',
            'requiredrole',
            'channelonly'
        ],
    })
})

client.login(process.env.BOT_TOKEN)

play通讯~

play.js:

代码语言:javascript
运行
复制
module.exports = {
    category: 'Music',
    description: 'Play a music',

    slash: true,
    testOnly: true,
    guildOnly: true,

    options:[{
        name: 'song',
        description: 'Song of your choice',
        required: true,
        type: 3,
    }],

    callback: async({ client, interaction, args }) => {

        // SPECIFY VOICE CHANNELS OF BOT AND MEMBER
        const myVoice = interaction.guild.members.me.voice.channelId;
        const memberVoice = interaction.member.voice.channelId;

        // CHECK IF MEMBER IS IN VOICE CHANNEL
        if (!memberVoice) return await interaction.reply({
            content: `*You're not in the voice channel!*`,
            ephemeral: true
        });

        // IF BOT IS IN THE VOICE CHANNEL, CHECK IF MEMBER IS IN SAME ONE/NONE
        if (myVoice && memberVoice !== myVoice) return await interaction.reply({
            content: `*You're not in the same voice channel as me!*`,
            ephemeral: true
        });

        // CREATE QUEUE
        const queue = client.player.createQueue(interaction.guild, {
            metadata: {
                channel: interaction.channel
            }
        });

        // TRIES TO CONNECT TO VOICE CHANNEL, THROWS AN ERROR ON FAIL
        try {
            if (!queue.connection) await queue.connect(interaction.member.voice.channel);
        } catch (e) {
            console.log(`Issue connecting bot to voice channel, error:\n${e}`)
            queue.destroy()
            return await interaction.reply({
                content: `*Couldn't join the voice channel!*`,
                ephemeral: true
            });
        }

        // Bot is thinking...
        await interaction.deferReply();

        // SEARCHES FOR THE SONG
        const song = await client.player.search(args[0], {
            requestedBy: interaction.user
        }).then(s => s.tracks[0]);

        // IF SONG IS NOT FOUND
        if (!song) return await interaction.followUp(`*❌ | Track **${args[0]}** not found!*`);

        // SONG IS FOUND AND PLAYS
        queue.play(song);
        return await interaction.followUp(`*⏳ | Queuing up song **${song.title}**!*`);

    }
}

我使用了WokCommands,并且运行NodeJS version 18.9 & DiscordJS v14.

任何帮助都是非常感谢的。

EN

回答 2

Stack Overflow用户

发布于 2022-09-22 00:37:18

我不确定,但我认为这是因为await在您的interaction.reply()interaction.followUp()面前。因此,它将回答这一点,如果尝试捕获有错误,它将再次尝试回复,导致它失败,因为它已经被回复了。再说一遍,我不是百分之百肯定,但尽量不要使用等待在您的互动回复或跟进,而只是return interaction.reply() or followUp()

票数 0
EN

Stack Overflow用户

发布于 2022-09-24 15:25:40

由于您使用的是interaction#deferReply方法,interaction#followUp方法将使您的bot进行两次交互。因此,您需要执行interaction#followUp方法而不是interaction#editReply方法,这样bot就可以编辑延迟回复消息,而不是抛出错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73786180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档