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

4
我希望人们可以在任何服务器上使用斜杠命令与我的机器人交互,只要该机器人存在。我已经授予了机器人application.commands权限。我参考了this answer,但似乎需要服务器的GuildID。我能否允许任何人在没有GuildID的情况下使用斜杠命令来与我的机器人交互?人们如何使用它?(我使用Commands Handler)
1个回答

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!"
                    }
                }
            });
        }
    });

});

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

enter image description here

回复的样子如下:

enter image description here


感谢您的回答。我认为现在应该将链接更改为:https://discord.com/developers/docs/interactions/application-commands#registering-a-command - Usman Sabuwala
@UsmanSabuwala 我已经进行了编辑,谢谢。 - Skulaurun Mrusal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接