简体   繁体   中英

Issue with ban command discord.py (rewrite branch)

I've been programming a bot with discord.py (the rewrite branch) and I want to add a ban command. The bot still doesn't ban the member, it just shows an error:

@client.command(aliases=['Ban'])
async def ban(ctx,member: discord.Member, days: int = 1):
    if "548841535223889923" in (ctx.author.roles):
        await client.ban(member, days)
        await ctx.send("Banned".format(ctx.author))
    else:
        await ctx.send("You don't have permission to use this command.".format(ctx.author))
        await ctx.send(ctx.author.roles)      

It'll ban the pinged user and confirm that it did ban

Member.roles is a list of Role objects, not strings. You can use discord.utils.get to search through that list using the id (as an int).

from discord.utils import get

@client.command(aliases=['Ban'])
async def ban(ctx, member: discord.Member, days: int = 1):
    if get(ctx.author.roles, id=548841535223889923):
        await member.ban(delete_message_days=days)
        await ctx.send("Banned {}".format(ctx.author))
    else:
        await ctx.send("{}, you don't have permission to use this command.".format(ctx.author))
        await ctx.send(ctx.author.roles)

There also is no longer a Client.ban coroutine, and the additional arguments to Member.ban must be passed as keyword arguments.

async def ban(ctx, member: discord.Member=None, *, reason=None):
  if reason:
    await member.send(f'You got banned from {ctx.guild.name} by {ctx.author}, reason: ```{reason}```')
    
    await member.ban()
    
    await ctx.send(f'{member.mention} got banned by {ctx.author.mention} with reason: ```{reason}```')
  if reason is None: 
    await ctx.send("please specify a reason")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM