简体   繁体   中英

How do I ban/kick multiple users in discord.py?

So I was wondering is there any way to ban/kick multiple users from a server on discord using one command? It might be 2 members or even 10 members would there be any way to do this? And if someone does know the answer PLEASE help me out and I will be forever grateful to you kind stranger!

Like pointed out in the comments, you can't kick or ban multiple users with one command natively in discord.py. You can however always write your own functions, which deals with that.


async def kick_multiple(memberList):
    for member in memberList:
        await member.kick()

@bot.command()
async def kickCommand(ctx):
    membersToKick = [] # fill with wanted members
    await kick_multiple(membersToKick) # only one command

You can also move this command to a different file, if you don't want to have it in your main.

You can use commands.Greedy on your discord.Member typecasted argument. As the name 'greedy' suggest, it takes all arguments of your specific typecast and converts them into a list of arguments

English definition of greediness: intense and selfish desire for something. (Implying you want it all, leave nothing and plunder everything)

Then you can use a for loop to kick/ban them one-by-one

async def ban(ctx, members: commands.Greedy[discord.Member]=None, *, reason=None):
     if not members: 
         return await ctx.send('provide at least one member argument....')
     if not reason:
         reason = "my server my reasoning"
     for member in members: # members is a list that greedy has created
         try:
           await member.ban(reason=reason)
         except discord.Forbidden:
           pass

here is an example of how it will work:

References

示例用法

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