Command raised an exception: KeyError: 'users' wen I execute ban command

Command raised an exception: KeyError: 'users' wen I execute ban command

如果用户关闭 his/her 直接消息,机器人会给我一个错误。我如何忽略此错误并仍然禁止该用户?

我的代码:

@commands.command()
async def ban(self, ctx, member : discord.Member, *, reason=None):
    if member.guild_permissions.manage_messages:
        embed = discord.Embed(title=":no_entry_sign: Error.", description="**I can't ban Moderators.**", color=discord.Color.red())
        await ctx.send(embed=embed)        
    elif ctx.message.author.guild_permissions.ban_members:
        if reason is None:
            await member.send("You was banned from *server*")
            await member.ban(reason=None, delete_message_days=0)
        else:
            await member.send("You was banned from *server*")
            await member.ban(reason=reason, delete_message_days=0)
    else:
        embed = discord.Embed(title=":no_entry_sign: Error.", description="**You are not a Moderator.**", color=discord.Color.red())
        await ctx.send(embed=embed)       

错误:

Command raised an exception: KeyError: 'users'

您的问题似乎是您正在向被禁止的用户发送 DM,如果用户关闭了他们的 dm,则在您发送该 DM 时会抛出异常,解决方案是不向该用户发送 DM。至于确定用户是否可以被 DM,我认为没有内置的方法,所以你可以简单地使用 try-except。