Cant reply json on discord.py

Viewed 48

Im making a cripto price tracker and i cant make it reply the json that im making with the coin decko URL and Arg1 for example Eternal or SLP, etc.

# bot = scrt.bot
bot   = commands.Bot(command_prefix = "!")
login = 0



tokens_dict = {
    'morw' :     '0x6b61b24504a6378e1a99d2aa2a5efcb1f5627a3a',
    'slp'  :     '0xcc8fa225d80b9c7d42f96e9570156c65d6caaa25',
    'pvu'  :     '0x31471e0791fcdbe82fbf4c44943255e923f1b794',
    'eternal' :  '0xd44fd09d74cd13838f137b590497595d6b3feea4'
}

# Login
@bot.event
async def on_ready():
    global login
    print('We have logged in as {0.user}'.format(bot))
    login = 1

@bot.command()
async def coin(ctx, arg1):
    global tokens_dict

    if(arg1 in tokens_dict.keys()):
        url = 'https://api.pancakeswap.info/api/v2/tokens/' + tokens_dict[arg1]
        response = request.get(url)
        responseDict = json.loads(response.text)
        await ctx.reply(responseDict)
    else:
        await ctx.reply("The token " + str(arg1) +  " is not in the token list, if you want to add " + str(arg1) + " to the list please use the command : " + '\n' + "!add_token")

In the coin function im trying to reply the json that ive created but i dont know how to.

1 Answers
    response = request.get(url)
    responseDict = json.loads(response.text)
    await ctx.reply(responseDict)

I'm assuming this is the part you're talking about.

You actually don't need to call json.loads(response.text) if you're going to send it anyway. If the raw response is unformatted then you can do this

response_dict = json.loads(response.text)
formatted_dict = json.dumps(response_dict, indent=4)
await ctx.reply(formatted_dict)

This should send the json as a string with indents. The reason discord.py wasn't letting you send the message was because it wasn't a string.

Related