[英]Parsing json data for embed in discord using discord.py
我正在嘗試從名為http://discohook.org 的站點獲取 json
然后,我希望能夠將它生成的 json 放入 discord 命令中,然后機器人將其作為嵌入消息發布。
這就是我的代碼:
payload = message.content.strip("$embed ")
embed = Embed.from_dict(json.loads(payload))
await message.channel.send(embed=embed)
json 看起來像這樣:
{
"content": null,
"embeds": [
{
"title": "Testy Test",
"description": "Hello World!",
"color": 16711680,
"footer": {
"text": "I hope this works"
},
"timestamp": "2021-12-09T11:36:00.000Z"
}
]
}
但是,當我將命令與上述 json (或任何其他看似有效的 json )一起使用時,我收到一個錯誤
Traceback (most recent call last):
File "D:\Dev\London-RP\lrp-bot\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "D:\Dev\London-RP\lrp-bot\_Project\lrp-bot\main.py", line 116, in on_message
await message.channel.send(embed=embed)
File "D:\Dev\London-RP\lrp-bot\lib\site-packages\discord_slash\dpy_overrides.py", line 323, in send_override
return await send(channel, *args, **kwargs)
File "D:\Dev\London-RP\lrp-bot\lib\site-packages\discord_slash\dpy_overrides.py", line 300, in send
data = await state.http.send_message(
File "D:\Dev\London-RP\lrp-bot\lib\site-packages\discord\http.py", line 254, in request
raise HTTPException(r, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50006): Cannot send an empty message
任何正確方向的幫助將不勝感激:(編輯:包括完整錯誤)
from_dict()
和to_dict()
方法適用於/與 Embed 對象一起使用。 因此,需要將 json 消息縮減為單個嵌入消息,如下所示,否則 function discord.Embed.from_dict()
會導致相應的空消息( 此處支持的字段為空的錯誤文檔)
注意:由於discord/utils.py", line 110, in parse_time
。
請在下面找到我用來測試的完整示例代碼,
import discord
import json
bot = discord.Client()
@bot.event
async def on_ready():
print(f'Logged in as {bot.user} (ID: {bot.user.id})')
print('------')
@bot.event
async def on_message(message):
response = '''
{
"title": "Testy Test",
"description": "Hello World!",
"color": 16711680,
"footer": {
"text": "I hope this works"
},
"timestamp": "2021-12-09T11:36:00.000+00:00"
}
'''
# we do not want the bot to reply to itself
if message.author.id == bot.user.id:
return
if message.content.startswith('$hello'):
await message.reply('Hello!', mention_author=True)
if message.content.startswith('$embed'):
payload = message.content.strip("$embed ")
embed = discord.Embed.from_dict(json.loads(response))
await message.channel.send(embed=embed)
bot.run('token')
Output:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.