我正在尝试为我的不和谐机器人创建一个斜杠命令,但我不知道当命令被执行时如何执行代码
我想使用的代码会向不同的频道发送消息(Args: Message)
这是我要使用的代码
const channel = client.channels.cache.find(c => c.id == "834457788846833734")
channel.send(Message)
答案 0 :(得分:1)
您需要监听事件 interactionCreate
或 INTERACTION_CREATE
。看下面的代码,没测试过,希望能用。
对于 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"
}
}
});
});
对于 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");
}
});