I am trying to take json from a site called http://discohook.org
I would then like to be able to put the json it generates into a discord command for the bot to then post as an embed message.
This is what I have as my code:
payload = message.content.strip("$embed ")
embed = Embed.from_dict(json.loads(payload))
await message.channel.send(embed=embed)
The json looks like this:
{
"content": null,
"embeds": [
{
"title": "Testy Test",
"description": "Hello World!",
"color": 16711680,
"footer": {
"text": "I hope this works"
},
"timestamp": "2021-12-09T11:36:00.000Z"
}
]
}
However, when I use the command with the above json (or any other seemingly-valid json for that matter), I get an error
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
Any help in the right direction would be greatly appreciated: (Edit: Included full error)
from_dict()
& to_dict()
methods work on/with Embed objects. So, the json message needs to be trimmed down to a single embed message like below else the function discord.Embed.from_dict()
cannot parse it to appropriate fields (find documentation of supported fields here ) causing the above empty message error.
Note: Added timezone in the timestamp because of time parsing issue in discord/utils.py", line 110, in parse_time
.
Please find the full sample code which I used to test below,
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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.