【问题标题】:Discord.py banning user not working if DM's disabled如果 DM 被禁用,Discord.py 禁止用户不工作
【发布时间】:2021-04-28 20:31:42
【问题描述】:

我的 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,则机器人无法发送禁止原因消息,因此不会继续下一步,即禁止他们,并返回错误消息,这是错误。请检查您是否正确输入了命令。 !ban @username(原因)

请你重写代码,让它在禁止某人之前尝试向某人推理推理,但如果他们禁用了 DM,它仍然会禁止他们。谢谢!

【问题讨论】:

    标签: discord discord.py


    【解决方案1】:

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

    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)
    

    在我指定这个之前,我遇到了以下错误。

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

    【讨论】:

      【解决方案2】:

      通过简单地移动要首先执行的禁令(因为它是优先级),然后它将尝试对用户进行 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)
      

      【讨论】:

        猜你喜欢
        • 2021-07-27
        • 2021-09-13
        • 1970-01-01
        • 2022-01-24
        • 2023-03-18
        • 1970-01-01
        • 2022-01-23
        • 2019-11-12
        • 1970-01-01
        相关资源
        最近更新 更多