所以我编写了一个经济系统机器人,它应该能够从钱包中减去钱并将其存入银行。但是每次我尝试执行命令“存款”和“取款”时,就像它应该被执行一样(当我以错误的方式执行它时,它会发送它应该在这种情况下发送的消息)。我怀疑,问题是 if 语句“if amount>bal[0]”,但我不知道。无论如何,这是我的代码和它给出的错误,当我尝试执行“存款”和“取款”时(我只会在这里发布存款命令,因为取款和存款几乎相等):
async def get_bank_data():
with open("mainbank.json", "r") as f:
users = json.load(f)
return users
async def update_bank(user,change = 0,mode = "wallet"):
users = await get_bank_data()
users[str(user.id)][mode] =+ change
with open ("mainbank.json", "w") as f:
json.dump(users, f)
bal = [users[str(user.id)]["wallet"],users[str(user.id)]["bank"]]
return user
@client.command()
async def deposit(ctx,amount = None):
if amount == None:
await ctx.send("Bitte gebe den Betrag an!")
return
bal = await update_bank(ctx.author)
amount = int(amount)
if amount>bal[0]:
await ctx.send("Du hast nicht genug Geld!")
if amount<0:
await ctx.send("Der Betrag muss eine positive Zahl sein!")
return
await update_bank(ctx.author,-1* amount)
await update_bank(ctx.author, amount, "bank")
await ctx.send(f"Du hast {amount} Moneten auf deine Bank gelegt")
错误如下:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-
packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-
packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\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: 'Member' object does not support indexing
答案 0 :(得分:0)
您一定忘记在 deposit
命令开始时打开帐户。
async def deposit(ctx, amount=None):
await open_account(ctx.author)
您还忘记在 return
后添加 if amount > bal[0]:
。
if amount > bal[0]:
await ctx.send("**You do not have enough money to do this!**")
return
您的 update_bank
似乎也有误。您需要在最后返回 bal
,而不是 user
。在你的情况下:
async def update_bank(user,change = 0,mode = "wallet"):
users = await get_bank_data()
users[str(user.id)][mode] =+ change
with open ("mainbank.json", "w") as f:
json.dump(users, f)
bal = [users[str(user.id)]["wallet"],users[str(user.id)]["bank"]]
return bal