Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I replace certain characters when messaging in discord.py

My issue is that I am trying to make a leader board command for my discord bot but when I sort the json file and then message that it looks like this:

{‘| Name’: 784779, ‘| Name_1’: 539427, ‘| Name_2’: 310407, ‘| Name_3’: 280250, ‘| Name_4’: 0}

its sorted and everything but it looks messy because of the brackets and quotes and also because it all on one line. I tried to fix this with replace() but it gave an error. Is there a different way to replace characters? my code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if "!leaderboard" in message.content:
    with open("income.json", "r+") as f:
        incomes = json.load(f)
    Sort = {k: v for k, v in sorted(incomes.items(), key=lambda item: item[1], reverse= True)}

    embedVar = discord.Embed(title= "Leader board",value=(""), inline=False, color=0x71368a)
    embedVar.add_field(name=('﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉'), value=(Sort))
    await message.channel.send(embed=embedVar)

>Solution :

Sort is not a string but a dictionary, that’s why replace doesn’t work. It gets automatically converted to a string but if you wanna format it yourself, you will have to iterate through it and convert it manually.

my_string = ""
for k, v in Sort.items():
    my_string += str(k) + ": " + str(v) + "\n"

Then pass my_string to embedVar.addfield instead of passing Sort. This is the basic idea, modify it to fit your needs.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading