Home > Mobile >  Having issues with displaying data from a json file in a embed
Having issues with displaying data from a json file in a embed

Time:11-08

I have been trying to show the data from my json file inside of a embed. However, I can't seem to find a way to display it and needed some help finding out how. Here is the json file data:

{
    "Slims Mod Bot": {
        "Felix\u2122": 2,
        "DustinFoes": 16,
        "Slim Beatbox": 26,
   },

In other words, I don't know how to show this data in a embed neatly, without all the symbols.

Here is the code that sends the data and counts the messages:

@bot.listen()
async def on_message(message):

    if not message.author.bot: 
        with open('message_count.json','r') as f:
            global message_count
            message_count = json.load(f)
            if message.guild.name not in message_count:
                message_count[message.guild.name] = {}
        try:
                message_count[message.guild.name][message.author.name]  = 1
        except KeyError:
                message_count[message.guild.name][message.author.name] = 1
        with open('message_count.json','w') as f:
                json.dump(message_count,f,indent=4)   

CodePudding user response:

For anyone who is having the same problem my friend and his team found a answer:

@bot.command()
async def leaderboard(ctx):
    with open('messages.json') as file:
         data = json.load(file)
    for key, value in data["Slims Mod Bot"].items():
        embed = discord.Embed(
         title="Leadboard",
         description="Test",
         color=0xFF000
        )
        embed.add_field(name = key, value = value)
        await ctx.respond(embed=embed)

Please notice the difference between the first and second set of code

@bot.command()
async def leaderboard(ctx):
    embed = discord.Embed(
         title="Leadboard",
         description="Test",
         color=0xFF000
        )
    with open('messages.json') as file:
         data = json.load(file)
    for key, value in data["Slims Mod Bot"].items():
        try:
             embed.add_field(name = key, value = value)
        except: return
    await ctx.respond(embed=embed)

Instead of "embed = discord.Embed" being below the "json.load(file)", it should be above. However, The embed fields should still be below the "json.load(file)" If we don't do this it will cause bot to send the embed over and over again essentially making unwanted spam which is against ToS, Hope this helps!

  • Related