unban命令discord.py重写

时间:2020-05-24 19:33:11

标签: python discord discord.py-rewrite

我需要有效的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即可。谢谢您的回答!

3 个答案:

答案 0 :(得分:1)

希望对您有所帮助

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)

答案 1 :(得分:0)

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

例如:

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

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

答案 2 :(得分:0)

@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)")