Discord.py禁止用户在DM';残疾的

Discord.py禁止用户在DM';残疾的,discord,discord.py,Discord,Discord.py,我有我的discord机器人的代码,它可以禁止人们,并首先告诉他们禁止的原因 cooldown = [] @bot.command(pass_context = True) @commands.has_role('Senior Moderator') async def ban(ctx, member: discord.Member = None, *, reason = None): author = str(ctx.author) if author in cooldown

我有我的discord机器人的代码,它可以禁止人们,并首先告诉他们禁止的原因

cooldown = []

@bot.command(pass_context = True)
@commands.has_role('Senior Moderator')
async def ban(ctx, member: discord.Member = None, *, reason = None):
    author = str(ctx.author)
    if author in cooldown:
        await ctx.send('Calm down! You\'ve already banned someone less than two hours ago.')
        return

    try:
        if reason == None:
            reason = 'breaking the rules.'
        await member.send(f'You have been banned from **{ctx.guild.name}** for **{reason}**')
        await member.ban(reason = f"Banned by {ctx.message.author} for "+reason)
        await ctx.send(f'{member.mention} has been banned.')
        cooldown.append(author)
        await asyncio.sleep(2 * 60 * 60)    #The argument is in seconds. 2hr = 7200s
        cooldown.remove(author)
    except:
        await ctx.send('Error. Please check you are typing the command correctly. `!ban @username (reason)`.')
但是,如果我试图禁止的用户禁用了DM,则bot无法发送禁止原因消息,因此不会继续下一步,即禁止它们,并返回错误消息,即error。请检查您键入的命令是否正确!ban@username(原因)


请你重写代码,允许它在禁止某人之前尝试对其进行推理,但如果他们禁用了DM,它仍将禁止他们。谢谢大家!

只需移动要首先执行的禁令(因为这是一个优先级),然后它就会尝试对用户进行dm

我还重新调整了代码的某些部分。它现在将尝试发送dm,如果不发送,它仍将禁止,但会向通道发送一条消息,提醒未向被禁止的用户发送消息

@bot.command(pass_context = True)
@commands.has_role('Senior Moderator')
async def ban(ctx, member: discord.Member = None, *, reason = None):
    author = ctx.author
    
    if author in cooldown:
        await ctx.send('Calm down! You\'ve already banned someone less than two hours ago.')
        return
    
    if member == None:
        await ctx.send('Please mention a member to ban!')
        return
    if reason == None:
        reason = 'breaking the rules.'
        
    await member.ban(reason = f"Banned by {ctx.message.author} for " + reason)
    
    try:
        await member.send(f'You have been banned from **{ctx.guild.name}** for **{reason}**')

    except:
        await ctx.send(f'A reason could not be sent to {ctx.message.author} as they had their dms off.')

    await ctx.send(f'{member.mention} has been banned.')
    cooldown.append(author)
    await asyncio.sleep(2 * 60 * 60)
    cooldown.remove(author)

通过简单地移动要首先执行的禁令(因为这是一个优先级),然后它将尝试对用户进行dm

我还重新调整了代码的某些部分。它现在将尝试发送dm,如果不发送,它仍将禁止,但会向通道发送一条消息,提醒未向被禁止的用户发送消息

@bot.command(pass_context = True)
@commands.has_role('Senior Moderator')
async def ban(ctx, member: discord.Member = None, *, reason = None):
    author = ctx.author
    
    if author in cooldown:
        await ctx.send('Calm down! You\'ve already banned someone less than two hours ago.')
        return
    
    if member == None:
        await ctx.send('Please mention a member to ban!')
        return
    if reason == None:
        reason = 'breaking the rules.'
        
    await member.ban(reason = f"Banned by {ctx.message.author} for " + reason)
    
    try:
        await member.send(f'You have been banned from **{ctx.guild.name}** for **{reason}**')

    except:
        await ctx.send(f'A reason could not be sent to {ctx.message.author} as they had their dms off.')

    await ctx.send(f'{member.mention} has been banned.')
    cooldown.append(author)
    await asyncio.sleep(2 * 60 * 60)
    cooldown.remove(author)

当我第一次发出bot-ban命令时,我也注意到了同样的事情,我做了以下操作来修复我的ban命令。首先,我尝试了一个场景,其中bot可以dm用户,如果可以,那么它将dm用户,然后禁止用户(注意:在DMing用户之前不要禁止用户,因为当用户和bot共享一个公共服务器时,bot只能dm用户)。但是,我随后做了一个例外,如果bot无法对用户进行dm,它将在执行命令的通道中发送一条消息“cannotdm the user”,然后禁止该用户

try:
      await member.send(embed=embo)
      await ctx.guild.ban(member, reason=reason)
except Exception:
      await ctx.send("Couldn't send dm to the user")
      await ctx.guild.ban(member, reason=reason)
编辑: 您还可以选择以下选项:

try:
      await member.send(embed=embo)
      await ctx.guild.ban(member, reason=reason)
except discord.HTTPException:
      await ctx.send("Couldn't send dm to the user")
      await ctx.guild.ban(member, reason=reason)
在我指定这个之前,我遇到了以下错误

后来我修复了它,添加了异常情况


当我第一次发出bot-ban命令时,我也注意到了同样的事情,我做了以下操作来修复我的ban命令。首先,我尝试了一个场景,其中bot可以dm用户,如果可以,那么它将dm用户,然后禁止用户(注意:在DMing用户之前不要禁止用户,因为当用户和bot共享一个公共服务器时,bot只能dm用户)。但是,我随后做了一个例外,如果bot无法对用户进行dm,它将在执行命令的通道中发送一条消息“cannotdm the user”,然后禁止该用户

try:
      await member.send(embed=embo)
      await ctx.guild.ban(member, reason=reason)
except Exception:
      await ctx.send("Couldn't send dm to the user")
      await ctx.guild.ban(member, reason=reason)
编辑: 您还可以选择以下选项:

try:
      await member.send(embed=embo)
      await ctx.guild.ban(member, reason=reason)
except discord.HTTPException:
      await ctx.send("Couldn't send dm to the user")
      await ctx.guild.ban(member, reason=reason)
在我指定这个之前,我遇到了以下错误

后来我修复了它,添加了异常情况