Search code examples
pythondiscorddiscord.pybotscounter

Issue with decreasing on a counter


I am trying to make a command that adds pigs to a counter which does work but also add a command that takes it away. here is my code.

client.message_counter = 0

@client.event
async def on_ready():
    print ("Bot online!")
    

@client.command()
async def add(ctx,*,amount):
    client.message_counter += int(amount)
    #await ctx.send("🐷")
    await ctx.send(f"{client.message_counter} pigs are spotted, how many are dying?")

@client.command()
async def kill(ctx,*,amount):
  j = int(client.message_counter)
  g = int(amount)
  end = j - g

    await ctx.send(f"{g} of pigs where killed \n {end} is left")

The error message I'm getting looks like this

Ignoring exception in command kill:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 25, in kill
    end = j - g
TypeError: unsupported operand type(s) for -: 'int' and 'str'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: unsupported operand type(s) for -: 'int' and 'str'

Here is what it looks like in discord

enter image description here


Solution

  • Here's the fix ;D

    total_pigs = 0
    
    @client.event
    async def on_ready():
        print ("Bot online!")
        
    
    @client.command()
    async def add(ctx,*,amount):
        global total_pigs
        total_pigs += int(amount)
        await ctx.send(f"{total_pigs} pigs are spotted, how many are dying?")
    
    @client.command()
    async def kill(ctx,*,amount):
        global total_pigs
        total_pigs = int(total_pigs) - int(amount)
        await ctx.send(f"{amount} of pigs where killed \n {total_pigs} is left")
    

    Here is a picture showing it:

    Here is pic with it