2
votes

I'm working on my first python discord bot and it's turning out to be pretty decent but I wanted to use embeds for certain responses. One of them includes sending all the features of the bots which uses many embeds. I don't have any experience about Javascript or JSON so I used a website "https://discohook.org/" to create an embed as it's GUI based. But as a result, I only get the JSON file and I couldn't find a way where I could load a JSON file and send it as an embed.

The JSON file looks something like this -

{
  "content": "Bot Name",
  "embeds": [
    {
      "description": "This command lists all the possible commands which can be used to interact with the bot.",
      "author": {
        "name": "Help",
        "icon_url": "http://4.bp.blogspot.com/-wuUbc-OPOt4/T3ioPh2pAAI/AAAAAAAAAdM/BFFvS5fxVMY/w1200-h630-p-k-no-nu/Questionmark.jpg"
      }
    },
    {
      "description": "Bot blesses you. ",
      "author": {
        "name": "Bless ",
        "icon_url": "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fwallpapercave.com%2Fwp%2Fwp4775695.jpg&f=1&nofb=1"
      }
    },
    {
      "description": "Set status as idle",
      "color": 16777215,
      "author": {
        "name": "Go Sleep",
        "icon_url": "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fmedia.forgecdn.net%2Favatars%2F170%2F652%2F636723641550179947.png&f=1&nofb=1"
      }
    }
  ]
2

2 Answers

1
votes

You could probably just load the individual variables.

with open(“JSON_FILE.txt”) as json_file:
    data = json.load(json_file)
data = data[“embeds”]

Then you can have some way to get the specific dictionary.

for embed in data:
    pass

Then just have data defined as the dictionary you want to use.

embed = discord.Embed(description = data[“description”])
embed.set_author(name = data[“author”][“name”], icon_url = data[“author”][“icon_url”]
1
votes

Maybe it've been a long time, but I have exactly the same problem and I found my own solution. Here it is:

from json import loads
from discord import Embed

# Just for our convinience let's make a function
def parse_embed_json(json_file):
    embeds_json = loads(json_file)['embeds']

    for embed_json in embeds_json:
        embed = Embed().from_dict(embed_json)
        yield embed

# And the main code looks like this
with open("bot/embeds/temp_ban_embeds.json", "r") as file:
    temp_ban_embeds = parse_embed_json(file.read())

for embed in temp_ban_embeds:
    await ctx.send(embed=embed)

So I found a special method from_dict. And it seems like it works with dicts we can get from json.loads method. So you can find the documentation here.