pythonjsondiscord.pytypeerroroperands

unsupported operand type(s) error when using %s


I am trying to get this piece of code to work which passes some JSON output into a discord.py embed but I am having some issues, this is my piece of code.

@client.event
async def on_message(message):
    if message.content.startswith('!hello'):
        print("dick")
        embed = discord.Embed(title="Verium Mining", url="https://wiki.vericoin.info/images/thumb/b/b5/Verium-01.png/176px-Verium-01.png", color=0x5da3fd)
        embed.add_field(name="Balance", value='%s coins', inline=False) % (json.loads(requests.get('https://vrm.mining-pool.ovh/index.php?page=api&action=getuserbalance&api_key=90ccec2fe5216ee73f6143ab3f59804d914a0df65165bedddd6f7a78fb6ffd15&id=4483').text)['getuserbalance']['data']['confirmed'])
        embed.add_field(name="Hashrate", value="undefined", inline=False)
        embed.add_field(name="Sharerate", value="undefined", inline=False)
        embed.set_footer(text="undefined")

When I run I get the following error:

TypeError: unsupported operand type(s) for %: 'Embed' and 'str'

How do I fix this?

Thanks in advance


Solution

  • Where you have

    embed.add_field(name="Balance", value='%s coins', inline=False) % (json.loads(requests.get('https://vrm.mining-pool.ovh/index.php?page=api&action=getuserbalance&api_key=90ccec2fe5216ee73f6143ab3f59804d914a0df65165bedddd6f7a78fb6ffd15&id=4483').text)['getuserbalance']['data']['confirmed'])
    

    you seem to mean

    embed.add_field(name="Balance", value='%s coins' % (json.loads(requests.get('https://vrm.mining-pool.ovh/index.php?page=api&action=getuserbalance&api_key=90ccec2fe5216ee73f6143ab3f59804d914a0df65165bedddd6f7a78fb6ffd15&id=4483').text)['getuserbalance']['data']['confirmed']), inline=False)
    

    though it would be clearer to write something like:

    num_coins = json.loads(requests.get('https://vrm.mining-pool.ovh/index.php?page=api&action=getuserbalance&api_key=90ccec2fe5216ee73f6143ab3f59804d914a0df65165bedddd6f7a78fb6ffd15&id=4483').text)['getuserbalance']['data']['confirmed']
    embed.add_field(name="Balance", value='%s coins'%num_coins, inline=False)
    

    If you're using a recent version of Python, you can use f-strings and avoid the % operator entirely:

    num_coins = json.loads(requests.get('https://vrm.mining-pool.ovh/index.php?page=api&action=getuserbalance&api_key=90ccec2fe5216ee73f6143ab3f59804d914a0df65165bedddd6f7a78fb6ffd15&id=4483').text)['getuserbalance']['data']['confirmed']
    embed.add_field(name="Balance", value=f'{num_coins} coins', inline=False)