简体   繁体   English

Discord python 机器人-massban

[英]Discord python bot - massban

I made this command for my discord bot to massban members that are in and without the server.我为我的 discord 机器人向服务器内外的 massban 成员发出了这个命令。 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.列出的 ID 是真实的 ID,是我对 massban 命令的测试对象。

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.首先,导致您出现错误的原因是这一行: await ctx.channel.fetch_message(ids) ,在您的情况下这不是必需的。

Second, await client.guild.ban(int(id), delete_message_days=0) isn't exactly the way you ban a member.其次, await client.guild.ban(int(id), delete_message_days=0)并不是您禁止会员的方式。 The attribute client doesn't take in guild .属性client不接受guild Instead, it should be ctx.guild.ban() .相反,它应该是ctx.guild.ban()

Finally, the id variable isn't directly converted to a discord user when you turn it to an integer.最后,当您将id变量转换为 integer 时,它不会直接转换为 discord 用户。 You should, in most cases, use the client.get_user(id) method to render a user object.在大多数情况下,您应该使用client.get_user(id)方法来呈现用户 object。

So in this case, you would use the command like: [prefix]massban id1 id2 id3...因此,在这种情况下,您可以使用如下命令: [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.首先,我将字符串列表转换为int ,因为它会导致错误,然后使用该int list来使用foreach loop来获取/获取它们的 id 并一一禁止它们。 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.")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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