【问题标题】:How to use slash commands on a discord bot?如何在不和谐机器人上使用斜杠命令?
【发布时间】:2021-03-31 23:06:27
【问题描述】:

我知道最近,discord 增加了机器人使用斜杠命令的能力。我检查了开发人员门户,并在 discord.py 的文档中找到了他们的资源。这是他们提供的示例:

import requests


url = "https://discord.com/api/v8/applications/<my_application_id>/commands"

json = {
    "name": "blep",
    "description": "Send a random adorable animal photo",
    "options": [
        {
            "name": "animal",
            "description": "The type of animal",
            "type": 3,
            "required": True,
            "choices": [
                {
                    "name": "Dog",
                    "value": "animal_dog"
                },
                {
                    "name": "Cat",
                    "value": "animal_cat"
                },
                {
                    "name": "Penguin",
                    "value": "animal_penguin"
                }
            ]
        },
        {
            "name": "only_smol",
            "description": "Whether to show only baby animals",
            "type": 5,
            "required": False
        }
    ]
}

# For authorization, you can use either your bot token
headers = {
    "Authorization": "Bot 123456"
}

# or a client credentials token for your app with the applications.commands.update scope
headers = {
    "Authorization": "Bearer abcdefg"
}

r = requests.post(url, headers=headers, json=json)

是什么意思?当我尝试基本上为我的机器人复制和粘贴这段代码时,我无法让它工作,它一直说我没有被授权。如何成功地将斜杠命令添加到我的应用程序,以及如何让机器人响应斜杠命令输入?

【问题讨论】:

    标签: python-3.x python-requests discord.py


    【解决方案1】:

    Discord 已实现按钮、选择菜单和斜线命令。

    discord.py 2.0 中提供了按钮和选择菜单,(您需要使用此命令升级到它:pip install -U git+https://github.com/Rapptz/discord.py)

    我建议使用一个名为 discord_slash 的模块,它支持按钮和下拉菜单,这与大量其他斜杠命令模块不同。

    【讨论】:

      【解决方案2】:

      看看这个易于使用且有效的库。

      https://github.com/eunwoo1104/discord-py-slash-command 祝你有美好的一天

      【讨论】:

      • 非常感谢!如果我对此有任何问题,我会告诉你的。
      • 这个库是否处理注册/创建新命令,或者只是在有人使用命令时做出反应?无论哪种方式,它都是一个很棒的工具,但我需要一种方法来注册我的机器人命令。
      • 它确实提到了如何做到这一点in their docs。这包括添加和删除斜杠命令。
      • 有了你们所有有用的 cmets,我得到了它的工作。非常感谢!
      猜你喜欢
      • 2022-11-05
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 2018-07-21
      • 2021-12-31
      • 2017-05-31
      • 2023-01-13
      • 2021-10-21
      相关资源
      最近更新 更多