如何使用嵌入 webhook 的 discord.py 修复此错误

Posted

技术标签:

【中文标题】如何使用嵌入 webhook 的 discord.py 修复此错误【英文标题】:How to fix this error with discord.py embed in webhooks 【发布时间】:2022-01-21 15:09:31 【问题描述】:

我正在尝试制作一个 webhook,但我设法修复了大部分问题,但现在我不断收到此错误,我不知道从哪里开始。

这是错误:

/discordpy-webhook/main.py
Traceback (most recent call last):
  File "/home/ayden/webhook/discordpy-webhook/main.py", line 23, in <module>
    response = webhook.execute()
  File "/home/ayden/.local/lib/python3.7/site-packages/discord_webhook/webhook.py", line 157, in execute
    response = self.api_post_request(url)
  File "/home/ayden/.local/lib/python3.7/site-packages/discord_webhook/webhook.py", line 137, in api_post_request
    timeout=self.timeout)
  File "/usr/lib/python3/dist-packages/requests/api.py", line 116, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 519, in request
    prep = self.prepare_request(req)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 462, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/usr/lib/python3/dist-packages/requests/models.py", line 316, in prepare
    self.prepare_body(data, files, json)
  File "/usr/lib/python3/dist-packages/requests/models.py", line 466, in prepare_body
    body = complexjson.dumps(json)
  File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 382, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3/dist-packages/simplejson/encoder.py", line 296, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3/dist-packages/simplejson/encoder.py", line 378, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3/dist-packages/simplejson/encoder.py", line 273, in default
    o.__class__.__name__)
TypeError: Object of type Embed is not JSON serializable

这是文件:

import discord
from discord_webhook import DiscordWebhook

webhook = DiscordWebhook(url='[REDACTED]')

em = discord.Embed(title='Intersoviet Airlines Information Board', description='Welcome to the Intersoviet Airlines Discord! \n Below you will find different links and info to help you get around.')
em.add_field(name='Dont forget to join the group!', value='Here is the ||hyper||link to the [group](https://www.roblox.com/groups/3524494/Intersoviet-Airlines#!/), here you can also buy perks (BC and FC)')
em.add_field(name='Want to fast travel to our perks?', value='This link shoots you over to our [Buisness Class](https://www.roblox.com/catalog/4484201358/Business-Class), and this one goes to our [First Class.](https://www.roblox.com/catalog/1136633425/First-Class)')
em.add_field(name='Want to apply?', value='Head over to our <#538681693674930185> channel for more info.')
em.add_field(name='Hvae a suggestion?', value='Head to our <#538486459062288405> channel and use `-suggest [Suggestion] (the message will be deleted bt dont worry that means it worked)')
em.add_field(name='need to report a bug or staff?', value='Dm one of our High Command members and we will get right on it')
em.add_field(name='Need a list of our High Command members?', value='Overseer - <@442764814020902938> \n Commissar - <@526366627068117010> \n Deputy Commissar - <@597478415481700377> \n Director of Aviation - <@398959101322854400> \n Deputy Director of Aviation - <@640359824844128279>')
em.add_field(name='Want a list of our heads?', value='Head of Ground Crew - N/A \n Head of Air Marshals - <@392236139945263105> \n Head of Flight Attendants - <@805344685647855617> \n Head of Captains - N/A \n\n Have a question about these departments? DM them, they are glad to help!')
em.add_field(name='Thinking of becoming a developer for us?', value='DM the Overseer to apply.')

webhook.add_embed(em)
response = webhook.execute()

如果您能给我任何尝试,那将对我有很大帮助;我也在使用最新的 discord.py 模块和 python 3.7.3

【问题讨论】:

【参考方案1】:

不确定这是否可行,但请尝试一下

webhook.add_embed(json.dumps(em))

【讨论】:

那个@3eeaaa webhook.add_embed(json.dumps(em)) NameError: name 'json' is not defined有个小问题 导入 json :)) 吐出同样的错误 ;-;并感谢您对此的帮助(原始错误) 如果你愿意,我们可以讨论一下不和谐以加快速度gawaxine.exe#1106

以上是关于如何使用嵌入 webhook 的 discord.py 修复此错误的主要内容,如果未能解决你的问题,请参考以下文章

如何使用嵌入discord.net webhook c#发送文件附件

使用 XHR 请求将 Discord 嵌入发送到 Webhook

使用变量作为掩码链接; discord.py 嵌入通过 webhook 发布; Python

Discord webhook embed php curl 在最近的 Discord 更新后不再工作

如何使用 Discord.js 发布到 Discord Webhook(代码 400 错误请求)?

使用 Discord.js,如何在触发 webhook 时触发机器人?