简体   繁体   English

踢出角色为 Discord bot Python 的成员

[英]Kick members with role Discord bot Python

I want to add a function to kick users who have a certain role.我想添加一个功能来踢出具有特定角色的用户。 But nothing works out.I was looking for solutions, but they all did not help.但没有任何效果。我一直在寻找解决方案,但他们都没有帮助。 Sometimes the team worked, but did not identify people with the right role and no one was kicked有时团队工作,但没有确定合适角色的人,没有人被踢

from discord.ext import commands
import sys
import urllib.parse
import urllib.request
import re
from discord.ext.commands import *
import discord
from discord.ext.commands import has_permissions

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

@bot.command()
@commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
    if reason==None:
      reason=" no reason provided"
    await ctx.guild.kick(member)
    await ctx.send(f'Пользователь {member.mention} был кикнут по причине {reason}')

@bot.command()
@commands.has_permissions(ban_members = True)
async def ban(ctx, member : discord.Member, *, reason = None):
    await member.ban(reason = reason)


@bot.command()
@commands.has_permissions(administrator = True)
async def unban(ctx, *, member):
    banned_users = await ctx.guild.bans()
    member_name, member_discriminator = member.split("#")

    for ban_entry in banned_users:
        user = ban_entry.user

        if (user.name, user.discriminator) == (member_name, member_discriminator):
            await ctx.guild.unban(user)
            await ctx.send(f'Unbanned {user.mention}')
            return

@bot.command()
@commands.has_permissions(administrator = True)
async def clearall(ctx):
  await ctx.channel.purge()

bot.run("Token")
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')

This code works, but it doesn't see people with the role.此代码有效,但看不到具有该角色的人。 King's attempt is being carried out, but people with roles are not kicking国王的尝试正在进行中,但有角色的人没有踢

Hey The only small problem is this line嘿唯一的小问题是这条线

intents = discord.Intents.default()意图 = discord.Intents.default()

the bot basically don't have the rights to kick someone with the default intents机器人基本上没有权利用默认意图踢人

how to solve it replace this line如何解决它替换这条线

intents = discord.Intents.default()

with

intents = discord.Intents.all()

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

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