【问题标题】:新的 Discord 斜线命令
【发布时间】:2023-03-17 23:20:01
【问题描述】:

最近,discord 为您自己的应用程序添加了对斜杠命令的支持。我通读了它的文档,并尝试搜索一些视频(但是该功能刚刚出现),但我不明白我实际上需要做什么才能使其正常工作。我正在使用 WebStorm(js,node.js)。有没有人成功发出过斜线命令,如果有,如何?

Documentation

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    您可以使用常规的discord.js,现在是v12.5.1

    这只是一个示例,但对我有用。

    const Discord = require('discord.js');
    const client = new Discord.Client();
    
    client.on('ready', () => {
        client.api.applications(client.user.id).guilds(YOUR_GUILD_ID_HERE).commands.post({
            data: {
                name: "hello",
                description: "hello world command"
                // possible options here e.g. options: [{...}]
            }
        });
    
    
        client.ws.on('INTERACTION_CREATE', async interaction => {
            const command = interaction.data.name.toLowerCase();
            const args = interaction.data.options;
    
            if (command === 'hello'){ 
                // here you could do anything. in this sample
                // i reply with an api interaction
                client.api.interactions(interaction.id, interaction.token).callback.post({
                    data: {
                        type: 4,
                        data: {
                            content: "hello world!!!"
                        }
                    }
                })
            }
        });
    });
    
    client.login(token);
    

    当然你可以有选择,见文档...

    IDE 不会注册新代码...至少我的 phpstorm 目前没有 :)

    但是,给予机器人正确的权限以使用这种类型的命令很重要!

    所以转到Discord.com/developers,选择您的应用程序,转到OAuth2 并选择

    application.commands

    来自范围部分。这应该在中间列的底部。您还应该选择bot,并在Bot Permissions下设置一些其他特定权限。然后使用新的邀请链接重新邀请机器人。

    如果没有application.commands 许可,该命令将无法运行,您将收到类似Missing Access 或类似的错误。

    重要事项

    1. 使用.guilds('11231...').comma 来测试这些命令。不使用它时,大约需要 1 小时才能激活。使用它会立即激活它。

    2. 给机器人正确的权限。 application.commands

    【讨论】:

    • 您好!感谢您的详细回复,我如何授予 application.commands 权限。我在开发者中心的任何地方都找不到它!?编辑:找到了!它在开发者中心的 OAuth 部分中
    • 不应该 .guilds('11231...').comma.guilds('11231...').commands.get()
    • 不是在你想发帖的时候。当然你有不同的方法来做你想做的事,这取决于你想要什么^^
    • 每次机器人启动时都必须运行这段代码吗?如果机器人关闭,斜线命令是否会出现在 Discord 中?
    • @ma1234 命令有点预装在不和谐机器人中。如您所见,部分命令位于就绪部分,实际上表明机器人正在运行。如果 bot 不在线,命令也会消失.. 或者如果 bot 的时间更长,我猜至少无法使用并消失。
    【解决方案2】:

    您好,我通常不使用 discord.js,但我能够找到一些很好的文档。您应该能够在定义“客户端”的情况下这样做。

    const interactions = require("discord-slash-commands-client");
    
    const client = new interactions.Client(
      "you unique bot token",
      "your bots user id"
    );
    

    如果客户端的定义如图所示,那么 /command 应该可以这样定义。

    // will create a new command and log its data. 
    //If a command with this name already exist will that be overwritten.
    client.createCommand({
        name: "your command name",
        description: "description for this command",
      })
      .catch(console.error)
      .then(console.log);
    

    【讨论】:

    • 您好,不幸的是,我的 IDE 告诉我找不到所需的不和谐斜线命令模块。其次,我使用 bot 而不是客户端,并且我在其他位置使用变量 bot,所以我的 IDE 也告诉我它已经定义了:/ 你有解决方案吗?
    • 您必须将模块添加到您的 IDE。如果您在终端中使用 Visual Studio Code 类型 npm i discord-slash-commands-client。如果您使用bot 而不是client,只需将@Shifu 代码中的client 重命名为bot。 @Shreyas007
    • 更具体地说,您必须将模块添加到您的节点项目中。
    猜你喜欢
    • 2021-05-09
    • 2021-10-06
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2022-11-09
    • 2021-12-31
    • 1970-01-01
    • 2022-01-25
    相关资源
    最近更新 更多