如何在不和谐机器人上使用斜杠命令?

时间:2021-03-31 23:06:27

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

我知道最近,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)

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

1 个答案:

答案 0 :(得分:0)

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

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