简体   繁体   English

尝试使用 Discord.py 踢所有具有角色的成员

[英]Trying to Kick All Members with a Role using Discord.py

I'm fairly new to scripting in Python, but I used stack overflow to create a command for a Discord bot that should kick all members with a given role.我对 Python 中的脚本编写相当陌生,但我使用堆栈溢出为 Discord 机器人创建了一个命令,该命令应该踢出具有给定角色的所有成员。 This is the code I currently have:这是我目前拥有的代码:

    import discord 
from discord.ext import commands
from discord.ext.commands import has_permissions

bot = commands.Bot(command_prefix = ".")

@bot.command()

#async def hello(ctx):
 #   await ctx.reply('Hello!')

@has_permissions(administrator=True)
async def kickMembers(ctx, role: discord.Role, reason: str=None):
    await ctx.reply('Kicking members')
    for member in ctx.guild.members:
        await ctx.reply(f'Looping through member  {member}')
        if role in member.roles: # does member have the specified role?
            await ctx.reply('Found a member, kicking!')
            await ctx.guild.kick(member,reason=reason)

Now, the issue is that it begins to loop through, but completely freezes after sending the message 'Looping through member {member}'.现在,问题是它开始循环,但在发送消息“循环通过成员 {member}”后完全冻结。 It sends a message saying that it is looping through itself (the bot), but then freezes and does't loop through anyone else.它发送一条消息说它正在循环通过自己(机器人),但随后冻结并且不循环通过其他任何人。

If anyone has any help that is much appreciated, thanks!如果有人有任何帮助,非常感谢,谢谢!

This works for me.这对我有用。 You should adjust the intents, and send kickMyRole from the user whose first role you want to kick.您应该调整意图,并从您要踢其第一个角色的用户发送kickMyRole

From what it sounds like, though, you just haven't given it the correct intents: make sure on the developer portal ( https://discord.com/developers/applications/ ), select your application, then click bot, and select the server members intent and the message content one.但是,从听起来的情况来看,您只是没有给它正确的意图:确保在开发人员门户( https://discord.com/developers/applications/ )上,选择您的应用程序,然后单击 bot,然后选择服务器成员的意图和消息内容之一。 You will need to update your code accordingly.您将需要相应地更新您的代码。 (I have added some more stuff to this code to make it easier to test). (我在此代码中添加了更多内容以使其更易于测试)。

import discord 
from discord.ext import commands
from discord.ext.commands import has_permissions

intents = discord.Intents.default()
intents.members = True
intents.message_content = True
bot = commands.Bot(command_prefix = ".", intents=intents)

@bot.command()

#async def hello(ctx):
 #   await ctx.reply('Hello!')


# @has_permissions(administrator=True)
async def kickMembers(ctx, role: discord.Role, reason: str=None):
    print(role)
    await ctx.reply('Kicking members')
    for member in ctx.guild.members:
        await ctx.reply(f'Looping through member  {member}')
        if role in member.roles: # does member have the specified role?
            await ctx.reply('Found a member, kicking!')
            await ctx.guild.kick(member,reason=reason)

@bot.event
async def on_message(ctx):
    print(ctx.content)
    if ctx.content == "kickMyRole":
        await kickMembers(ctx, ctx.author.roles[1]) # Not @everyone

bot.run("TOKEN")

I commented about looping through the members associated with a role, so here is an example of that with your use case:我评论了循环遍历与角色关联的成员,所以这里有一个与您的用例有关的示例:

import discord 
from discord.ext import commands
from discord.ext.commands import has_permissions

bot = commands.Bot(command_prefix='.')

@bot.command()
@has_permissions(administrator=True)
async def kickMembers(ctx, role: discord.Role, reason: str = None):
    await ctx.reply('Kicking members')

    for member in role.members:
        await member.kick(reason=reason)

    await ctx.reply('Members kicked')

Doing it this way saves time by not looping through all the members of a guild and not needing to use logic to determine if the member has the role you are looking for.这样做可以节省时间,因为不需要遍历公会的所有成员,也不需要使用逻辑来确定成员是否具有您正在寻找的角色。

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

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