繁体   English   中英

有什么办法可以修复我正在创建的 discord 机器人 discord.js

[英]Is there any way i can fix my discord bot, which i`m creating discord.js

我正在使用 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);

我进入终端时出错

我希望这个机器人只要启动并运行就继续执行命令

这是 Discord.JS 中的常见错误,当您已经回复了交互并尝试再次回复时会发生这种错误。

来自 discord.js discord 服务器:

您已经回复了互动。

• 使用.followUp() 发送新消息

• 如果您推迟回复,最好使用.editReply()

• 响应斜杠命令/ 按钮/ select 菜单

要修复此错误,您可以使用.followUp()向频道发送另一条消息或.editReply()编辑回复,如上所示。

您可以在此处查看有关命令交互的文档

是的,问题是您不能在 discords api 中多次回复斜杠命令,所以您应该使用

interaction.channel.send()

或者

interaction.editReply()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM