簡體   English   中英

當成員不在 JSON 文件中時,Discord.py bot 無法發送消息

[英]Discord.py bot failing to send a message when the member is not in the JSON file

@bot.command()
async def coincount(ctx,*, member: discord.Member = None):
    member = ctx.author if not member else member
    member_id = str(member.id)
    with open('users.json', 'r') as f:
        users = json.load(f)

        coins = users[member_id]['coins']
        if member_id in users:
            if coins > 1:
                await ctx.send(f'{member.mention} has {coins} coins.')
            if coins == 1:
                await ctx.send(f'{member.mention} has {coins} coin.')
        else:
            await ctx.send(f'{member.mention} has no coins.')

沒有回溯。 如果成員在文件中,機器人會發送適當的消息,但如果不在文件中,則不執行任何操作。

如果成員 ID 不在您的 JSON 文件中,則coins = users[member_id]['coins']將引發KeyError
您應該將它移到 if 語句中。

由於on_command_error中的錯誤處理,它可能不會引發回溯。

暫無
暫無

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

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