在所有有没有 GuildID 的机器人的服务器中使用斜线命令 (Discord.js v12)

tha*_*550 4 javascript node.js discord discord.js

我希望人们能够在任何服务器上对我的机器人使用 Slash 命令,只要该机器人在那里。我已进一步授予机器人application.commands权限。我引用了这个答案,但它似乎需要服务器的 GuildID。我可以允许任何使用 Slash 命令的人在没有 GuildID 的情况下访问我的机器人吗?人们如何使用它?(我使用命令处理程序)

对不起,我的英语不好

Sku*_*sal 5

您可能想使用全局斜杠命令。全局意味着它适用于机器人所在的所有公会,并且您不需要提供任何公会 ID。

client.on("ready", () => {

    // Register global slash command
    client.api.applications(client.user.id).commands.post({
        data: {
            name: "hello",
            description: "Say 'Hello, World!'"
        }
    });

    // Listen for an interaction (e.g. user typed command)
    client.ws.on("INTERACTION_CREATE", (interaction) => {
        // Access command properties
        const commandId = interaction.data.id;
        const commandName = interaction.data.name;
        
        // Reply only to commands with name 'hello'
        if (commandName == "hello") {
            // Reply to an interaction
            client.api.interactions(interaction.id, interaction.token).callback.post({
                data: {
                    type: 4,
                    data: {
                        content: "Hello, World!"
                    }
                }
            });
        }
    });

});
Run Code Online (Sandbox Code Playgroud)

这是用户如何使用您的命令:

在此输入图像描述

回复如下所示:

在此输入图像描述