【问题标题】:Issue with decreasing on a counter在柜台上减少的问题
【发布时间】:2021-12-26 09:47:56
【问题描述】:

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

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'

这是不和谐的样子

【问题讨论】:

  • 看起来amountkill() 函数中被视为字符串?
  • 如果amount实际上是一个整数,并且可以保证,可以使用g = int(amount)
  • @LarrytheLlama 似乎仍然没有工作,它确实帮助了我更多,但这就是它向我展示的内容
  • 我将上面的消息编辑为当前显示的内容以及我添加到代码中以使其看起来像这样的内容
  • 不应该只是await ctx.send(f"{g} of pigs where killed \n {end} is left”)吗?

标签: python discord discord.py bots counter


【解决方案1】:

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

这是一张显示它的图片:

【讨论】:

  • 这确实有效,但不会保存后续信息。
  • 你可以在await ctx.send(f"{amount} of pigs where killed \n {end} is left”)之后重新定义total_pigsend
  • 完成了,现在修复了,看看编辑,这是你想要的吗?
  • 好吧,问题是它根本没有保存任何信息,当它说你杀了他们,你添加了更多信息,它实际上并没有带走任何信息,向上滚动并查看我的图片供参考@Nick403
  • 看照片“这里有照片”,看看,就像你要求的那样,如果你有更多问题,请在 dis nick403#2885 上联系我,会更快
猜你喜欢
  • 2016-02-25
  • 1970-01-01
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多