2
votes

I was making a discord bot using the discord module in python... I am having a lot of trouble trying to make the kick command work. I tried using bot.kick, client.kick and ctx.kick but they all give the same error which says,

Ignoring exception in command kick:
Traceback (most recent call last):
  File "C:\Users\user\AppData\Roaming\Python\Python36\site-packages\discord\ext\commands\core.py", line 62, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "C:\Users\user\Desktop\Code\bot.py", line 44, in kick
    await client.kick(user)
AttributeError: 'Client' object has no attribute 'kick'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\user\AppData\Roaming\Python\Python36\site-packages\discord\ext\commands\bot.py", line 886, in invoke
    yield from ctx.command.invoke(ctx)
  File "C:\Users\user\AppData\Roaming\Python\Python36\site-packages\discord\ext\commands\core.py", line 493, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\user\AppData\Roaming\Python\Python36\site-packages\discord\ext\commands\core.py", line 71, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Client' object has no attribute 'kick'

I tried searching various different youtube videos and posts related to the problem I am having but nobody seems to be having a solution. I have written the code below. If you spot any errors which I missed please let me know.

import time
import random
import discord
import asyncio
from discord.ext import commands

#Initialize
client = discord.Client()#Creates Client
bot = commands.Bot(command_prefix='!')#Sets prefix for commands(!Command)

@bot.event
async def on_ready():
    print('Name:', end=' ')
    print(bot.user.name)
    print('ID: ')
    print(bot.user.id)       

@bot.command(pass_context=True)
async def user_info(ctx, user: discord.Member):
    await ctx.send(f'The username of the user is {user.name}')
    await ctx.send(f'The ID of the user is {user.id}')
    await ctx.send(f'The status of the user is {user.status}')
    await ctx.send(f'The role of the user is {user.top_role}')
    await ctx.send(f'The user joined at {user.joined_at}')

@bot.command(pass_context=True)
async def kick(ctx, user: discord.Member):
    await ctx.send(f'The Kicking Hammer Has Awoken! {user.name} Has Been Banished')
    await client.kick(user)

bot.run('SECRET')
client.run('SECRET')
1
What version of discord.py are you using? Try import discord; print(discord.__version__) - Patrick Haugh
The problem may be because you are using both bot and client. Remove all references to client and replace them with bot. - Patrick Haugh

1 Answers

3
votes

You appear to be using the newer discord.py 1.0, also called the rewrite branch. You should read this, which is the documentation of the many breaking changes that were made in that switch. You should also refer solely to that documentation, as most documentation for the older 0.16 version is not compatible.

Many things were moved out of Client and into places that made a little more sense. Specifically, kick is now a method of Guilds.

import asyncio
from discord.ext import commands

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

@bot.event
async def on_ready():
    print('Name:', end=' ')
    print(bot.user.name)
    print('ID: ')
    print(bot.user.id)       

@bot.command(pass_context=True)
async def user_info(ctx, user: discord.Member):
    await ctx.send(f'The username of the user is {user.name}')
    await ctx.send(f'The ID of the user is {user.id}')
    await ctx.send(f'The status of the user is {user.status}')
    await ctx.send(f'The role of the user is {user.top_role}')
    await ctx.send(f'The user joined at {user.joined_at}')

@bot.command(pass_context=True)
async def kick(ctx, user: discord.Member):
    await ctx.send(f'The Kicking Hammer Has Awoken! {user.name} Has Been Banished')
    await ctx.guild.kick(user)

bot.run('secret')

Note that I've also removed all references to client in the above. Bot is a subclass of Client, so you can access all the Client methods through Bot.