【问题标题】:unban command discord.py rewrite解禁命令 discord.py 重写
【发布时间】:2020-09-11 10:35:12
【问题描述】:

我需要一个有效的 discord.py 命令,通过标签而不是他们的名字和鉴别器来取消禁止用户。我该怎么办? 这是我编写的代码,它适用于*unban name#1234

@client.command()
@commands.has_any_role(702909770956406885, 545323952428417064, 545323570587369472)
async def unban(ctx, *, member):
    banned_user = await ctx.guild.bans()
    member_name, member_discriminator = member.split("#")
    for ban_entry in banned_user:
        user = ban_entry.user

        if (user.name, user.discriminator) == (member_name, member_discriminator):
            await ctx.guild.unban(user)
            embed = discord.Embed(title="Fatto!", description=f"Ho sbannato {user.mention}!", color=discord.Color.green())
            await ctx.send(embed=embed)
            return

我怎样才能让它与标签一起工作?我知道你不能直接标记一个被禁止的用户,但你可以用他的 id 来做。感谢您的回答!

【问题讨论】:

    标签: python discord discord.py-rewrite


    【解决方案1】:

    使用 member 对象作为 unban 函数中的参数。

    例如:

    @client.command()
    @commands.has_any_role(702909770956406885, 545323952428417064, 545323570587369472)
    async def unban(ctx, *, member: discord.Member):
        await ctx.guild.unban(member)
    

    然后,您可以通过提及用户来执行命令。 例如:*unban @aleee

    【讨论】:

      【解决方案2】:
      @client.command(description="bans a user with specific reason (only admins)") #ban
      @commands.has_permissions(administrator=True)
      async def ban (ctx, member:discord.User=None, reason =None):
       try:
          if (reason == None):
              await ctx.channel.send("You  have to specify a reason!")
              return
          if (member == ctx.message.author or member == None):
              await ctx.send("""You cannot ban yourself!""") 
          else:
              message = f"You have been banned from {ctx.guild.name} for {reason}"
              await member.send(message)
              await ctx.guild.ban(member, reason=reason)
              print(member)
              print(reason)
              await ctx.channel.send(f"{member} is banned!")
       except:
          await ctx.send(f"Error banning user {member} (cannot ban owner or bot)")
      

      【讨论】:

      • 这是一个按名字封禁用户的套路,不回答问题,即“按标签解除封禁”
      【解决方案3】:

      希望对您有所帮助

      from discord.ext.commands import has_permissions
      
      @bot.command()
      @has_permissions(ban_members=True)
      async def unban(ctx, userId: discord.User.id):
        user = get(id=userId)
        await ctx.guild.unban(user)
      

      【讨论】:

        猜你喜欢
        • 2021-07-27
        • 2020-10-19
        • 2021-07-15
        • 2019-08-31
        • 2020-05-03
        • 2020-11-06
        • 2021-03-30
        • 2018-11-07
        • 2021-04-04
        相关资源
        最近更新 更多