Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

AttributeError: 'User' object has no attribute 'kick'

So I have done tons of research, found many answers, but none of them worked. I am trying to get my bot to kick a user on command, but I don’t know the proper syntax. Help would be appreciated, thanks.

intents = discord.Intents.all()
intents.members = True
bot = commands.Bot(command_prefix='#', intents=intents)

@bot.event
async def on_message(message):
    if '#kick' in message.content.lower():
        mod=discord.utils.get(message.author.guild.roles, name='Mod')
        if mod in message.author.roles:
            await message.channel.send('Who do you want to kick?')
            try:
                message=await bot.wait_for('message', check=lambda m: m.author==message.author and m.channel==message.channel, timeout=15)
            except asyncio.TimeoutError:
                await message.channel.send('Your victim got away')
            else:
                member2id=message.mentions[0].id
                member2=await bot.fetch_user(member2id)
                await member2.kick()
                await message.channel.send(f'{message.content} was kicked')
        else:
            await message.channel.send('You dont have the permission to use that command')
bot.run('token')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I believe you’re looking for Member.kick, as opposed to User.kick (which does not exist).

I believe message.mentions returns a list of Member objects, so the snippet might be:

intents = discord.Intents.all()
intents.members = True
bot = commands.Bot(command_prefix='#', intents=intents)

@bot.event
async def on_message(message):
    if '#kick' in message.content.lower():
        mod=discord.utils.get(message.author.guild.roles, name='Mod')
        if mod in message.author.roles:
            await message.channel.send('Who do you want to kick?')
            try:
                message=await bot.wait_for('message', check=lambda m: m.author==message.author and m.channel==message.channel, timeout=15)
            except asyncio.TimeoutError:
                await message.channel.send('Your victim got away')
            else:
                await message.mentions[0].kick()
                await message.channel.send(f'{message.content} was kicked')
        else:
            await message.channel.send('You dont have the permission to use that command')
bot.run('token')

However, note that Message.mentions can be empty, and that the list is randomly ordered.

Alternatively, discord.py supports more kick functions, see them here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading