【问题标题】:Bot gives same error message for any input given for a give command discord.py对于给定命令 discord.py 的任何输入,Bot 都会给出相同的错误消息
【发布时间】:2021-05-21 12:42:36
【问题描述】:

我有一个经济机器人,我正在发出 give 命令。我已经设置好了,这样你就可以输入>give @whoever 5,机器人将从你的钱包中取出 5 个并将 5 个添加到其他人的钱包中。但即使我有足够的钱给别人,机器人也会说我没有那么多钱可以给。

如您在上面看到的,我的帐户中有 200 个,但如果我尝试提供 200 或 50 个等。他们的机器人会回复:

代码

@bot.command()
async def give(ctx, member : discord.Member, amount=None):
  await open_account(ctx.author)
  await open_account(member)

  if amount == None:
    await ctx.send(f"{ctx.author.mention}, Please enter the amount that you want to give!")
    return

  users = await get_bank_data()
  user = member
  bal = users[str(user.id)]["wallet"]

  amount = int(amount)

  if amount < 1:
    await ctx.send(f"{ctx.author.mention}, Your amount needs to be larger than 0!")
    return
  if amount > bal:
    await ctx.send(f"{ctx.author.mention}, You do not have enough money in your wallet to do this!")
    return
  if amount < bal:
    await update_bank(ctx.author,-1*amount,"wallet")
    await update_bank(member,amount,"wallet")
    await ctx.send(f"{ctx.author.mention}, You just gave `{amount}` Ulti Coins to {member.name}!")

我什至在if amount &gt; bal: 之后有一个返回声明,但这似乎不起作用。我怎样才能让我的机器人知道我实际上有足够的付出?

【问题讨论】:

  • 您能否添加您正在调用的导致机器人发送“您没有足够...”的确切命令?
  • @JacobLee 这是&gt;give @member (any number) 除了0 及以下我给出的任何数字,即使我有足够的钱,它也会给我这个错误。
  • 包含所有用户钱包数据的字典是否被修改过?即,目前,机器人现在使用的数据是否与提供的数据相同?
  • 从那以后我没有改变它。
  • 只是确认一下,机器人没有修改它,对吗?

标签: python discord.py


【解决方案1】:

问题在于您试图获取错误用户的钱包数据。该机器人检查提到的用户钱包中是否有足够的货币。但是,机器人应该检查调用该命令的用户是否有足够的货币。

因此,代码应如下所示:

@bot.command()
async def give(ctx, member : discord.Member, amount=None):
    await open_account(ctx.author)
    await open_account(member)

    if amount is None:
        await ctx.send(f"{ctx.author.mention}, Please enter the amount that you want to give!")
        return

    users = await get_bank_data()
    bal = users[str(ctx.author.id)]["wallet"]   # Use 'ctx.author' instead of 'member'

    amount = int(amount)

    if amount < 1:
        await ctx.send(f"{ctx.author.mention}, Your amount needs to be larger than 0!")
        return
    elif amount > bal:
        await ctx.send(f"{ctx.author.mention}, You do not have enough money in your wallet to do this!")
        return
    elif amount <= bal:
        await update_bank(ctx.author, -amount, "wallet")
        await update_bank(member, amount, "wallet")
        await ctx.send(f"{ctx.author.mention}, You just gave `{amount}` Ulti Coins to {member.name}!")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-06
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 2016-11-13
    • 2017-11-08
    相关资源
    最近更新 更多