簡體   English   中英

Discord.py - 經濟系統機器人中的存款命令無法正常工作

[英]Discord.py - Deposit command in Economy System Bot doesn't work properly

所以我編寫了一個經濟系統機器人,它應該能夠從錢包中減去錢並將其存入銀行。 但是每次我嘗試執行命令“存款”和“提款”時,就像它應該被執行一樣(當我以錯誤的方式執行它時,它會發送它應該在這種情況下發送的消息)。 我懷疑問題出在 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

您一定是在您的deposit命令開始時忘記了開戶。

    async def deposit(ctx, amount=None):
        await open_account(ctx.author)

您還忘記在if amount > bal[0]:之后添加return

        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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM