我最近开始添加一些音乐功能到我的机器人,我正在创建。然而,在尝试执行命令play
时,我经常遇到一个问题。命令play
返回的interaction.reply
和interaction.followUp
很少,但是只有满足IF语句时才能执行,还有一些只能执行interaction.deferReply
。
据我所见,所有回复大多在执行IF语句时运行,但它们都返回了答复,这意味着一旦返回完它们,它们就应该停止代码。但是机器人仍然告诉我交互已经被回复了,但是我不知道在哪里。
错误:
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:
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:
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.
任何帮助都是非常感谢的。
发布于 2022-09-22 00:37:18
我不确定,但我认为这是因为await
在您的interaction.reply()
和interaction.followUp()
面前。因此,它将回答这一点,如果尝试捕获有错误,它将再次尝试回复,导致它失败,因为它已经被回复了。再说一遍,我不是百分之百肯定,但尽量不要使用等待在您的互动回复或跟进,而只是return interaction.reply() or followUp()
。
发布于 2022-09-24 15:25:40
由于您使用的是interaction#deferReply
方法,interaction#followUp
方法将使您的bot进行两次交互。因此,您需要执行interaction#followUp
方法而不是interaction#editReply
方法,这样bot就可以编辑延迟回复消息,而不是抛出错误。
https://stackoverflow.com/questions/73786180
复制相似问题