[英]How do I create a slash command in discord using discord.js
I am trying to create a slash command for my discord bot, but I don't know how to execute code when the command is exuted我正在尝试为我的不和谐机器人创建一个斜杠命令,但我不知道如何在命令执行时执行代码
The code I want to use will send a message to a different channel (Args: Message)我要使用的代码将向不同的频道发送消息(Args:Message)
Here is the code I want to use这是我想使用的代码
const channel = client.channels.cache.find(c => c.id == "834457788846833734")
channel.send(Message)
You need to listen for an event interactionCreate
or INTERACTION_CREATE
.您需要监听一个事件interactionCreate
或INTERACTION_CREATE
。 See the code below, haven't tested anything, hope it works.看下面的代码,还没有测试任何东西,希望它有效。
For discord.js v12:对于 discord.js v12:
client.ws.on("INTERACTION_CREATE", (interaction) => {
// Access command properties
const commandId = interaction.data.id;
const commandName = interaction.data.name;
// Do your stuff
const channel = client.channels.cache.find(c => c.id == "834457788846833734");
channel.send("your message goes here");
// Reply to an interaction
client.api.interactions(interaction.id, interaction.token).callback.post({
data: {
type: 4,
data: {
content: "Reply message"
}
}
});
});
For discord.js v13:对于 discord.js v13:
client.on("interactionCreate", (interaction) => {
if (interaction.isCommand()) {
// Access command properties
const commandId = interaction.commandId;
const commandName = interaction.commandName;
// Do your stuff
const channel = client.channels.cache.find(c => c.id == "834457788846833734")
channel.send("your message goes here");
// Reply to an interaction
interaction.reply("Reply message");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.