简体   繁体   English

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

[英]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.

相关问题 有什么方法可以在 discord.js v13 上将我的 Discord Bot 的在线状态设置为移动设备? - Is there any way I can set my Discord Bot's online status to mobile on discord.js v13? 我想让我的 discord.js 机器人不可见 - I'm looking to make my discord.js bot invisible 如何修复我的 discord 机器人(在 discord.js 中)不响应我的命令 - How do I fix my discord bot (in discord.js) not responding to my commands 如何修复我的帮助命令 (discord.js) - How can I fix my help commands (discord.js) 我怎样才能实现!leaveguild<guildid> 命令(离开指定的公会)进入我的 Discord (discord.js) 机器人?</guildid> - How can I implement a !leaveguild <guildID> command (that leaves the specified guild) into my Discord (discord.js) bot? 我在 discord.js 中制作头像机器人时出错 - I'm getting an error in making avatar bot in discord.js 我可以将 discord 机器人的控制台日志打印到 discord.js 中的特定通道吗? - Can I print the console log of my discord bot into a specific channel in discord.js? 如何列出我的机器人在控制台中的所有 Discord 服务器 ID? | discord.js - How can I list the all Discord servers ID where my bot are in the console? | discord.js 我正在使用 discord.js 编写一个 discord 机器人,我正在处理一个建议命令 - I'm coding a discord bot using discord.js and I'm working on a suggestion command 我正在尝试为我的机器人在 discord.js 中创建一个自动角色 function 但它不起作用 - I'm trying to make an autorole function in discord.js for my bot but it doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM