javascript - Discord 仅识别 discord.js 中的 "ping"命令

标签 javascript node.js discord discord.js

在我的 Discord.JS 机器人中,我设置了多个命令( pingbeep 等),但 Discord 仅识别“ping”。我尝试了多种设置,并且都相同。
这是我的代码:

const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

client.once('ready', () => {
    console.log('Ready!');
});

client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    const { commandName: command } = interaction;

    if (command === 'ping') {
        await interaction.reply('Pong!');
    } else if (command === 'beep') {
        await interaction.reply('Boop!');
    } else if (command === 'server') {
        await interaction.reply(`Server name: ${interaction.guild.name}\nTotal members: ${interaction.guild.memberCount}`);
    } else if (command === 'user-info') {
        await interaction.reply(`Your username: ${interaction.user.username}\nYour ID: ${interaction.user.id}`);
    }
});

client.login(token);
这是输入“/”时的 Discords 命令 View
Discords command view
如您所见,ping 是唯一被discord认可的东西。
还值得注意的是,“ping”命令具有我设置的原始描述的描述,因此问题似乎是 Discord 没有在每次脚本更改时更新命令。但是,我不知道如何解决这个问题。

最佳答案

好像你仅限 注册了 ping 命令。您必须 注册每个单独的斜线命令。
我猜你之前注册了 slashcommand 一些图 block ,从那以后就没有删除它。您仅在代码示例中响应斜杠命令,但您必须首先创建它们。
查看here关于如何做到这一点。

it may take up to one hour to register a global command tho, so be patient. If you are fine, with slashcommands for one guild only, you can also only create guildCommands. These are up and running within a view minutes (under 10minutes max)



这是一个简单的命令,您可以使用它来更新斜杠命令(这是来自 docs 的明星)
client.on('messageCreate', async message => {
    if (!client.application?.owner) await client.application?.fetch();

    if (message.content.toLowerCase() === '!deploy' && message.author.id === client.application?.owner.id) {
        const data = [
            {
                name: 'ping',
                description: 'Replies with Pong!',
            },
            {
                name: 'pong',
                description: 'Replies with Ping!',
            },
        ];

        const commands = await client.application?.commands.set(data);
        console.log(commands);
    }
});

NOTE: you have to be running the master branch of discord.js (aka Discord.js V13). If you have not installed it yet, you can install it by running: npm install discord.js@latest. Make sure, you have uninstalled the "normal" discord.js dependency beforehand, by running npm uninstall discord.js.

If you are not sure what version you currently have installed, simply run npm list

关于javascript - Discord 仅识别 discord.js 中的 "ping"命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68692294/

相关文章:

javascript - 防止页面离开时弹出 "Confirm Navigation"对话框

javascript - For 循环在 Jade 中的 Node.js 中声明的变量在 Javascript 中不起作用

node.js - 将 HTML 按钮链接到 node.js 帖子

python - 查找消息的作者

javascript - 使用 jscript 检测 exe 的版本和公司名称

node.js - Redis 与 NodeJS - 创建一组十个项目

javascript - 读取未定义的 JSON 文件(异步)

python - 如何让我的 Python 3.6.1 Discord 机器人在服务器中创建新的文本 channel ?

javascript - 类型错误 : Cannot read property 'id' of undefined (also 'startWith' )

javascript - 设置复杂嵌套对象的格式