Discord 只识别“ping";discord.js 中的命令 [英] Discord only recognizing "ping" command in discord.js

查看:76
本文介绍了Discord 只识别“ping";discord.js 中的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Discord.JS 机器人中,我设置了多个命令(pingbeep 等),但 Discord 只识别ping".我尝试了多种设置,都一样.

这是我的代码:

const { Client, Intents } = require('discord.js');const { token } = require('./config.json');const client = new Client({ 意图:[Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });client.once('准备好了', () => {console.log('准备好了!');});client.on('interactionCreate', 异步交互 => {如果 (!interaction.isCommand()) 返回;const { commandName: command } = 交互;如果(命令 === 'ping'){等待交互.回复('乒乓!');} else if (command === 'beep') {等待交互.回复('Boop!');} else if (command === 'server') {await interaction.reply(`服务器名称:${interaction.guild.name}
成员总数:${interaction.guild.memberCount}`);} else if (command === 'user-info') {awaitinteraction.reply(`你的用户名:${interaction.user.username}
你的ID:${interaction.user.id}`);}});client.login(token);

这里是/"时的 Discords 命令视图.是输入

如您所见,ping 是唯一被 discord 识别的东西.

还值得注意的是,ping"命令具有我设置的原始描述的描述,因此问题似乎是 Discord 不会在每次脚本更改时更新命令.但是,我不知道如何解决这个问题.

解决方案

您好像注册了 ping 命令.您必须分别注册每个斜杠命令.

我猜你之前在某个图块上注册了 slashcommand,从那以后就没有删除它.您仅在代码示例中响应斜杠命令,但您必须首先创建它们.查看这里了解如何这样做.

<块引用>

注册一个全局命令可能需要一小时,所以请耐心等待.如果你没问题,只有一个公会的斜线命令,你也可以只创建 guildCommands.这些都在几分钟内启动并运行(最多 10 分钟)


这是一个简单的命令,您可以使用它更新斜杠命令(这是 docs)

client.on('messageCreate', async message => {if (!client.application?.owner) 等待 client.application?.fetch();if (message.content.toLowerCase() === '!deploy' && message.author.id === client.application?.owner.id) {常量数据 = [{名称:'平',description: '用 Pong 回复!',},{名称:'乒乓',description: '用 Ping 回复!',},];const commands = await client.application?.commands.set(data);控制台.log(命令);}});

<块引用>

注意:您必须运行 discord.js 的主分支(又名 Discord.js V13).如果你还没有安装它,你可以通过运行:npm install discord.js@latest来安装它.确保,您已经卸载了正常"的通过运行 npm uninstall discord.js 预先依赖 discord.js.

<块引用>

如果你不确定你当前安装的是什么版本,只需运行 npm list

In my Discord.JS bot, I have multiple commands setup (ping, beep, etc.) but Discord only recognizes "ping". I have tried multiple setups, and all are the same.

Here is my code:

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}
Total members: ${interaction.guild.memberCount}`);
    } else if (command === 'user-info') {
        await interaction.reply(`Your username: ${interaction.user.username}
Your ID: ${interaction.user.id}`);
    }
});

client.login(token);

And here is Discords command view when "/" is enter

As you can see, ping is the only thing being recognized by discord.

It is also worth noting the ‘ping’ command has a description which the original description I setup, so it seems like issue is that Discord is not updating the commands each time the script changes. But, I don’t know how to resolve that issue.

解决方案

It seems like you only registered the ping command. You have to register each slash command individually.

I guess you registered the slashcommand some tile earlier, and have not removed it since. You are only responding in your code example to slashcommands, but you have to create them in the first hand. Check here on how to do that.

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)


Here is a simple command, with which you can update the slashcommands (this is staright from the 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

这篇关于Discord 只识别“ping";discord.js 中的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆