不和谐Python机器人massban

2024-10-02 12:35:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我为我的discord bot向服务器内外的massban成员发出了此命令。结果发现我得到了一个错误:

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.

列出的ID是真实的ID,是massban命令的测试对象

这是我的密码

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


Tags: 命令clientididsmessagebotawaitcommands
2条回答

试试这个:

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

首先,导致错误的是这一行:await ctx.channel.fetch_message(ids),这在您的案例中是不必要的

第二,await client.guild.ban(int(id), delete_message_days=0)并不是禁止成员的方式。属性client不接受guild。相反,它应该是ctx.guild.ban()

最后,当您将id变量转换为整数时,它不会直接转换为discord用户。在大多数情况下,您应该使用client.get_user(id)方法来呈现用户对象

因此,在本例中,您将使用如下命令:[prefix]massban id1 id2 id3 ...

我想在这里回答我自己的问题,因为我知道了。首先,我将字符串列表转换为int,因为它会导致错误,然后使用该int list使用foreach loop来获取/获取它们的ID并逐个禁止它们。这也适用于公会以外的用户

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

相关问题 更多 >

    热门问题