在柜台上减少的问题

Issue with decreasing on a counter

我正在尝试创建一个命令,将猪添加到一个确实有效的计数器,但也添加一个命令将它拿走。这是我的代码。

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")

我收到的错误消息如下所示

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'

这是它在 discord 中的样子

这是解决方法;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")

这是一张展示它的图片: