首页 > 解决方案 > 导入 discord.py 模块的问题

问题描述

堆栈溢出。我一直遇到不和谐机器人的问题,这是脚本:

    def send():
        url = "https://discordapp.com/api/webhooks/762125650546131005/lgYkjh-ILrag2sb3nzqUZfF1sg2mN2a0QeABaUq9dwl7qBTNL4EqWV00K62xWZ8_sNQ5"
        data = {}
        data["content"] = ""
        data["username"] = "Suggestions"

        data["embeds"] = []
        embed = {}

        embed["description"] = "**Author** » <@" + str(message.author.id) + ">\n **Suggestion** » " + str(args)
        embed["title"] = "**New Suggestion!**"
        data["embeds"].append(embed)

        result = requests.post(url, data=json.dumps(data), headers={"Content-Type": "application/json"})

    send()

    await message.author.send("Thank you for your suggestion! We will look into it as soon as possible and will message you if it will be used.")

当我执行“;suggestion fix bugs”时,它只会发送到 webhook “fix”,这只是第一个词,我正在努力解决这个问题。请问有人可以帮忙吗?

标签: pythondiscorddiscord.py

解决方案


不要将请求与 discord.py 一起使用,它是同步的并且会阻止您的机器人,使用带有aiohttp的discord.Webhook来发送discord.Embed

例子:

from aiohttp import ClientSession

async with ClientSession() as session:
    wh = discord.Webhook.from_url("<webhook url>", adapter=discord.AsyncWebhookAdapter(session))

    e = discord.Embed()
    e.title = "Hello I am an Embed"
    ...

    await wh.send(embed=e)

推荐阅读