問題描述
在我的 Discord.JS 機(jī)器人中,我設(shè)置了多個(gè)命令(ping
、beep
等),但 Discord 只識(shí)別ping".我嘗試了多種設(shè)置,都一樣.
這是我的代碼:
const { Client, Intents } = require('discord.js');const { token } = require('./config.json');const client = new Client({ 意圖:[Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });client.once('準(zhǔn)備好了', () => {console.log('準(zhǔn)備好了!');});client.on('interactionCreate', 異步交互 => {如果 (!interaction.isCommand()) 返回;const { commandName: command } = 交互;如果(命令 === 'ping'){等待交互.回復(fù)('乒乓!');} else if (command === 'beep') {等待交互.回復(fù)('Boop!');} else if (command === 'server') {await interaction.reply(`服務(wù)器名稱:${interaction.guild.name}
成員總數(shù):${interaction.guild.memberCount}`);} else if (command === 'user-info') {awaitinteraction.reply(`你的用戶名:${interaction.user.username}
你的ID:${interaction.user.id}`);}});client.login(token);
這里是/"時(shí)的 Discords 命令視圖.是輸入
如您所見,ping
是唯一被 discord 識(shí)別的東西.
還值得注意的是,ping"命令具有我設(shè)置的原始描述的描述,因此問題似乎是 Discord 不會(huì)在每次腳本更改時(shí)更新命令.但是,我不知道如何解決這個(gè)問題.
您好像只注冊(cè)了 ping 命令.您必須分別注冊(cè)每個(gè)斜杠命令.
我猜你之前在某個(gè)圖塊上注冊(cè)了 slashcommand,從那以后就沒有刪除它.您僅在代碼示例中響應(yīng)斜杠命令,但您必須首先創(chuàng)建它們.查看這里了解如何這樣做.
<塊引用>注冊(cè)一個(gè)全局命令可能需要一小時(shí),所以請(qǐng)耐心等待.如果你沒問題,只有一個(gè)公會(huì)的斜線命令,你也可以只創(chuàng)建 guildCommands.這些都在幾分鐘內(nèi)啟動(dòng)并運(yùn)行(最多 10 分鐘)
這是一個(gè)簡(jiǎn)單的命令,您可以使用它更新斜杠命令(這是 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) {常量數(shù)據(jù) = [{名稱:'平',description: '用 Pong 回復(fù)!',},{名稱:'乒乓',description: '用 Ping 回復(fù)!',},];const commands = await client.application?.commands.set(data);控制臺(tái).log(命令);}});
<塊引用>
注意:您必須運(yùn)行 discord.js 的主分支(又名 Discord.js V13
).如果你還沒有安裝它,你可以通過運(yùn)行:npm install discord.js@latest
來安裝它.確保,您已經(jīng)卸載了正常"的通過運(yùn)行 npm uninstall discord.js
預(yù)先依賴 discord.js.
如果你不確定你當(dāng)前安裝的是什么版本,只需運(yùn)行 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 runningnpm uninstall discord.js
.If you are not sure what version you currently have installed, simply run
npm list
這篇關(guān)于Discord 只識(shí)別“ping";discord.js 中的命令的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!