简体   繁体   English

如何在我的 discord 服务器中踢出所有没有角色的成员? (discord.py)

[英]How can I kick all members without a role in my discord server ? (discord.py)

I'm trying to figure out a command to kick all members without a role in my discord server.我试图找出一个命令来踢掉我的 discord 服务器中没有角色的所有成员。 I searched Google + this website and I found solutions that don't seem to work for me.我在 Google+ 这个网站上进行了搜索,发现似乎对我不起作用的解决方案。 Here's what I tried.这是我尝试过的。

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

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

@bot.command(pass_context=True)
async def noroleskick(ctx):
    server=ctx.message.server
    for member in tuple(server.members):
        if len(member.roles)==1:
            await bot.kick(member)

bot.run(token)

However, this didn't work, and here's the error:但是,这不起作用,这是错误:

File "main.py", line 111, in noroleskick
    server=ctx.message.server
AttributeError: 'Message' object has no attribute 'server'

I found this on Reddit.我在 Reddit 上找到了这个。 If this is no brainer, I sincerely apologize.如果这没有道理,我真诚地道歉。 I am a beginner at coding, so if you could go into detail I would highly appreciate it.我是编码初学者,所以如果你能详细介绍 go,我将不胜感激。

You need to change ctx.message.server to ctx.message.guild or just ctx.guild and enable the members intent from the discord developer portal and then use the following code to enable it in your script您需要将ctx.message.server更改为ctx.message.guild或只是ctx.guild并从discord 开发人员门户启用成员意图,然后使用以下代码在您的脚本中启用它

from discord import Intents
intents = Intents()
intents.members = True
bot = commands.Bot(command_prefix='$', intents=intents)
import discord
from discord.ext import commands
from discord.ext.commands import has_permissions

intents = discord.Intents.all()

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

@bot.command()
@has_permissions(administrator=True)
async def kicknorole(ctx: commands.Context):
    members = ctx.guild.members
        
    for member in members:
        if len(member.roles) == 1:
            await member.kick()
            print(member.name)
    await ctx.reply('Done!')

bot.run('TOKEN')

Here is what I used.这是我用的。 I was asked by a friend to kick all the users from their server with no roles.一位朋友要求我从他们的服务器中踢出所有没有角色的用户。 This worked for me.这对我有用。 762 members, hope it works for you! 762会员,希望对你有用!

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

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