【问题标题】:Create a discord channel with the discord API使用不和谐 API 创建不和谐频道
【发布时间】:2021-11-23 05:22:43
【问题描述】:

我正在尝试在 python 中使用 discord api 来处理请求,现在我正在尝试创建一个频道

这是我的代码

import requests
API_URL = "https://discord.com/api/v9"
res = requests.post(f"{API_URL}/guilds/{guild_id}/channels", data={"name": "my channel name", "permission_overwrites": [], "type": 0}, headers={"authorization": f"Bot {the_bot_token}", "Content-Type": "application/json"})
print(res.text)

但它给了我这样的回应:

{"message": "400: Bad Request", "code": 0}

有人可以帮助我吗?

【问题讨论】:

  • 你考虑过使用 discord.py 吗?作为一个库,它非常适合将 api 包装到异步 python 函数中,并且通常做得很好。不幸的是,对它的支持即将结束,但是随着明年 API 的新版本的推出,所有不和谐的机器人都必须重新编写:/
  • 是的,我知道 discord.py,但我的意图是能够在不使用它的情况下创建一个机器人
  • Roger - 我知道这些类型的 cmets 经常让我感到紧张,所以我理解 - 不幸的是我不知道答案,但我会用细齿梳来满足您的要求。不合适的 : 或空格可能是被解析的有效请求和错误格式之间的区别

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


【解决方案1】:

好吧,你的代码是错误的,你搞砸了请求数据

您应该将数据替换为 json

这是工作代码

import requests
API_URL = "https://discord.com/api/v9"
res = requests.post(f"{API_URL}/guilds/{guild_id}/channels", json={"name": "my channel name", "permission_overwrites": [], "type": 0}, headers={"authorization": f"Bot {the_bot_token}", "Content-Type": "application/json"})
print(res.text)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-25
    • 2021-09-15
    • 2019-08-22
    • 2020-09-06
    • 2020-11-07
    • 2020-12-28
    • 2020-05-07
    • 1970-01-01
    相关资源
    最近更新 更多