[英]Is there any way i can fix my discord bot, which i`m creating discord.js
I am creating a bot using guide by Discord.js, however after like 3 or sometimes 3 commands the bot stops working and i get discord message i have tried to restart it many times but after sometime it just stop working again and again我正在使用 Discord.js 的指南创建一个机器人,但是在执行 3 个或有时 3 个命令后,机器人停止工作,我收到discord 消息,我曾多次尝试重新启动它,但过了一段时间后,它一次又一次地停止工作
const fs = require('node:fs');
const path = require('node:path')
const { Client, Events, GatewayIntentBits, Collection ,ActionRowBuilder,EmbedBuilder, StringSelectMenuBuilder } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
const commandsPath = path.join(__dirname,'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath,file);
const command = require(filePath);
if('data' in command && 'execute' in command){
client.commands.set(command.data.name,command);
}else{
console.log(`[WARNING] The command at ${filePath} is missing`);
}
}
client.once(Events.ClientReady, () => {
console.log('Ready!');
})
//menu
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === 'ping') {
const row = new ActionRowBuilder()
.addComponents(
new StringSelectMenuBuilder()
.setCustomId('select')
.setPlaceholder('Nothing selected')
);
const embed = new EmbedBuilder()
.setColor(0x0099FF)
.setTitle('pong')
.setDescription('Some description here')
.setImage('https://media.istockphoto.com/id/1310339617/vector/ping-pong-balls-isolated-vector-illustration.jpg?s=612x612&w=0&k=20&c=sHlz5sbJrymDo7vfTQIuaj4lbmwlvAhVE7Uk_631ZA8=')
await interaction.reply({ content: 'Pong!', ephemeral: true, embeds: [embed]});
}
});
//======================================================================================================================
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand ||
interaction.isButton() ||
interaction.isModalSubmit()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found`)
return;
}
try {
await command.execute(interaction);
}catch(error){
console.error(error);
await interaction.reply({content: 'There was an error while executing this command!', ephemeral: true});
}
console.log(interaction);
});
client.login(token);
Error i get in terminal我进入终端时出错
I wanted this bot to continue to execute commands as long as it's up and running我希望这个机器人只要启动并运行就继续执行命令
This is a common error in Discord.JS that occurs when you already replied to an interaction and you attempt to reply again.这是 Discord.JS 中的常见错误,当您已经回复了交互并尝试再次回复时会发生这种错误。
From the discord.js discord server:来自 discord.js discord 服务器:
You have already replied to the interaction.您已经回复了互动。
• Use.followUp() to send a new message • 使用.followUp() 发送新消息
• If you deferred reply it's better to use.editReply() • 如果您推迟回复,最好使用.editReply()
• Responding to slash commands / buttons / select menus • 响应斜杠命令/ 按钮/ select 菜单
To fix this error, you can use .followUp()
to send another message to the channel or .editReply()
to edit the reply as shown above.要修复此错误,您可以使用.followUp()
向频道发送另一条消息或.editReply()
编辑回复,如上所示。
You can see the documentation on a command interaction here您可以在此处查看有关命令交互的文档
Yes the problem is that you cant reply to a slash command more then once in discords api so instead you should use是的,问题是您不能在 discords api 中多次回复斜杠命令,所以您应该使用
interaction.channel.send()
or或者
interaction.editReply()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.