简体   繁体   中英

Discord python bot - massban

I made this command for my discord bot to massban members that are in and without the server. Turns out that I get an error:

discord.ext.commands.errors.CommandInvokeError: Command raised an
exception: HTTPException: 400 Bad Request (error code: 50035): Invalid
Form Body In message_id: Value "757201002062544946 759785520065806366"
is not snowflake.

The IDs listed are real IDs and are my test subjects for the massban command.

Here is my code

@client.command(aliasas=['mb'])
@commands.has_permissions(ban_members = True)
async def massban(ctx, *, ids:str):
    await ctx.channel.fetch_message(ids)
    ids = ids.split(" ")
    success = 0
    for id in ids:
        await client.guild.ban(int(id), delete_message_days=0)
        success += 1
    await ctx.send("Massbanned " + str(success) + " member/s.")

Try this:

@client.command(aliasas=['mb'])
@commands.has_permissions(ban_members = True)
async def massban(ctx, *, ids:str):
    list_of_ids = ids.split(' ')
    success = 0

    for id in list_of_ids:
        id = client.get_user(int(id))
        await ctx.guild.ban(id, reason='You were mass-banned')
        success += 1

    await ctx.send("Massbanned " + str(success) + " member/s.")

First off, what was causing you the error was this line: await ctx.channel.fetch_message(ids) , which wasn't necessary in your case.

Second, await client.guild.ban(int(id), delete_message_days=0) isn't exactly the way you ban a member. The attribute client doesn't take in guild . Instead, it should be ctx.guild.ban() .

Finally, the id variable isn't directly converted to a discord user when you turn it to an integer. You should, in most cases, use the client.get_user(id) method to render a user object.

So in this case, you would use the command like: [prefix]massban id1 id2 id3...

I'm gonna answer my own question over here since I figured it out. First I converted the list of strings to int since it causes errors then used that int list to use the foreach loop in fetching/getting their ids and banning them one by one. This also applies to users outside the guild.

    @client.command(aliasas=['mb'])
    @commands.has_permissions(administrator = True)
    async def massban(ctx, *, ids:str):
        list_of_ids = ids.split(' ')
        list_of_idsx = list(map(int, list_of_ids))
        success = 0
    
        for id in list_of_idsx:
            user = await client.fetch_user(id)
            await ctx.guild.ban(user, delete_message_days=0)
            success += 1
    
        await ctx.send("Massbanned " + str(success) + " member/s.")

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