【问题标题】:Discord only recognizing "ping" command in discord.jsDiscord 仅识别 discord.js 中的“ping”命令
【发布时间】:2021-10-11 23:31:47
【问题描述】:

在我的 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 命令视图

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

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

【问题讨论】:

  • 使用单独的 if 语句时是否会出现同样的问题?看起来不和谐将 if else if 作为一个语句..
  • 命令文件也会发生这种情况,所以我怀疑这是问题所在。无论如何我会调查的。
  • 我会看看我在帖子底部所做的编辑
  • 你在哪里注册命令?您可以将该代码添加到您的问题中吗?
  • 请注意,您提供的代码中的任何内容均不对不和谐中显示的内容(作为命令)负责。

标签: javascript node.js discord discord.js


【解决方案1】:

您似乎注册了 ping 命令。您必须分别注册每个斜杠命令。

我猜你之前注册了 slashcommand 一些图块,从那以后就没有删除它。您只在代码示例中响应斜杠命令,但您必须首先创建它们。 查看here 了解如何操作。

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


这是一个简单的命令,您可以使用它来更新斜杠命令(这是来自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);
    }
});

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

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

【讨论】:

    【解决方案2】:

    这对我有用,想法是分别渲染每个命令。

    我还进行了一些硬编码配置:guild_id 和 client_id(以在开发模式下赢得一些时间)

    const { Client, Collection, Intents } = require('discord.js');
    const { token, client_id, guild_id } = require('./config.json');
    
    const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
    
    client.once('ready', () => {
        console.log('Ready!');
    });
    
    
    client.on('messageCreate', async message => {
        if (!client.application?.owner) await client.application?.fetch();
    
        // => client_id was just a quick fix, the correct value should be the id of the guild owner(s)
        if (message.content.toLowerCase() === '!deploy' && message.author.id === client_id) {
            const data = [
                {
                    name: 'ping',
                    description: 'Replies with Pong!',
                },
                {
                    name: 'foo',
                    description: 'Replies with bar!',
                },
                {
                    name: 'chicken',
                    description: 'Replies with sticks!',
                },
            ];
    
            // same here, guild_id should also be replaced by the correct call to the idea number, just as client_id in this code!
            for (let i = 0; data.length > i; i++) {
                await client.guilds.cache.get(guild_id)?.commands.create(data[i]);
    
            }
        }
    });
    client.on('interactionCreate', async interaction => {
        if (!interaction.isCommand()) return;
    
        const command = interaction.commandName;
    
        if (command === 'ping') {
            await interaction.reply('pong')
        } else if (command === 'foo') {
            await interaction.reply('bar')
        } else if (command === 'chicken') {
            await interaction.reply('nuggets')
        }
    });
    
    client.login(token);
    

    【讨论】:

      猜你喜欢
      • 2020-12-04
      • 1970-01-01
      • 2020-04-29
      • 2018-08-15
      • 2022-07-12
      • 2023-01-22
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      相关资源
      最近更新 更多