【发布时间】:2023-03-17 23:20:01
【问题描述】:
最近,discord 为您自己的应用程序添加了对斜杠命令的支持。我通读了它的文档,并尝试搜索一些视频(但是该功能刚刚出现),但我不明白我实际上需要做什么才能使其正常工作。我正在使用 WebStorm(js,node.js)。有没有人成功发出过斜线命令,如果有,如何?
【问题讨论】:
标签: javascript discord discord.js
最近,discord 为您自己的应用程序添加了对斜杠命令的支持。我通读了它的文档,并尝试搜索一些视频(但是该功能刚刚出现),但我不明白我实际上需要做什么才能使其正常工作。我正在使用 WebStorm(js,node.js)。有没有人成功发出过斜线命令,如果有,如何?
【问题讨论】:
标签: javascript discord discord.js
您可以使用常规的discord.js,现在是v12.5.1。
这只是一个示例,但对我有用。
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
client.api.applications(client.user.id).guilds(YOUR_GUILD_ID_HERE).commands.post({
data: {
name: "hello",
description: "hello world command"
// possible options here e.g. options: [{...}]
}
});
client.ws.on('INTERACTION_CREATE', async interaction => {
const command = interaction.data.name.toLowerCase();
const args = interaction.data.options;
if (command === 'hello'){
// here you could do anything. in this sample
// i reply with an api interaction
client.api.interactions(interaction.id, interaction.token).callback.post({
data: {
type: 4,
data: {
content: "hello world!!!"
}
}
})
}
});
});
client.login(token);
当然你可以有选择,见文档...
IDE 不会注册新代码...至少我的 phpstorm 目前没有 :)
但是,给予机器人正确的权限以使用这种类型的命令很重要!
所以转到Discord.com/developers,选择您的应用程序,转到OAuth2 并选择
application.commands
来自范围部分。这应该在中间列的底部。您还应该选择bot,并在Bot Permissions下设置一些其他特定权限。然后使用新的邀请链接重新邀请机器人。
如果没有application.commands 许可,该命令将无法运行,您将收到类似Missing Access 或类似的错误。
重要事项
使用.guilds('11231...').comma 来测试这些命令。不使用它时,大约需要 1 小时才能激活。使用它会立即激活它。
给机器人正确的权限。 application.commands
【讨论】:
.guilds('11231...').comma 是 .guilds('11231...').commands.get()
您好,我通常不使用 discord.js,但我能够找到一些很好的文档。您应该能够在定义“客户端”的情况下这样做。
const interactions = require("discord-slash-commands-client");
const client = new interactions.Client(
"you unique bot token",
"your bots user id"
);
如果客户端的定义如图所示,那么 /command 应该可以这样定义。
// will create a new command and log its data.
//If a command with this name already exist will that be overwritten.
client.createCommand({
name: "your command name",
description: "description for this command",
})
.catch(console.error)
.then(console.log);
【讨论】:
npm i discord-slash-commands-client。如果您使用bot 而不是client,只需将@Shifu 代码中的client 重命名为bot。 @Shreyas007