如何使用discord.js在discord中创建斜杠命令

yeotifhr  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(479)

我正试图为我的discord机器人创建一个斜杠命令,但我不知道在命令发出时如何执行代码
我要使用的代码将向其他通道发送消息(args:message)
这是我想要使用的代码

const channel = client.channels.cache.find(c => c.id == "834457788846833734")
channel.send(Message)
d5vmydt9

d5vmydt91#

你需要倾听一个事件 interactionCreateINTERACTION_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.application.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");
    }
});

相关问题