bos gamer
bos gamer

Reputation: 113

Discord Bot Kick Command Doesn't Work As Expected

I need help with my kick command. I don't get any error,But the result is something else then what I expected,My code is below.

@bot.command(pass_context=True, name="kick")

@has_permissions(kick_members=True)

async def kick(ctx, *, target: Member):

if target.server_permissions.administrator:

    await bot.say("Target is an admin")
else:
    try:
        await bot.kick(target)
        await bot.say('Kicked{}'.format(member.mention))
    except Exception:
        await bot.say("Something went wrong")

@kick.error
async def kick_error(error, ctx):

if isinstance(error, CheckFailure):

     await bot.send_message(ctx.message.channel, "You do not have permissions")
elif isinstance(error, BadArgument):
    await bot.send_message(ctx.message.channel, "Could not identify target")
else:
    raise error 

But the commands do kick the member but it doesn't says kicked (MEMBERNAME). Instead it says 'something went wrong'. Also there are two commands Which work for my kick command.

Upvotes: 1

Views: 526

Answers (1)

SirSeba
SirSeba

Reputation: 151

I don't really get why you try to except an exception if it isn't the case.

I deleted the * in async def kick() function as you only require the member not multiple arguments so it's kinda pointless to have it there and I also removed the try and except thing which in my opinion is useless in this case.

@bot.command(pass_context=True, name="kick")

@has_permissions(kick_members=True)

async def kick(ctx, target: discord.Member=None):

    if target.server_permissions.administrator:

        await bot.say("Target is an admin")
    else:
        await bot.kick(target)
        await bot.say('Kicked{}'.format(target.mention))

@kick.error
async def kick_error(error, ctx):

if isinstance(error, CheckFailure):

     await bot.send_message(ctx.message.channel, "You do not have permissions")
elif isinstance(error, BadArgument):
    await bot.send_message(ctx.message.channel, "Could not identify target")
else:
    raise error

If you want to check for example if the moderator or the person who uses the command is also putting a user in the command that should be kicked , you could do it with an if statement which would check if the discord.Member function is still none if yes it would output a message in chat. In this case I've put the message "You forgot the user"

@bot.command(pass_context=True, name="kick")

@has_permissions(kick_members=True)

async def kick(ctx, target: discord.Member=None):

    if target.server_permissions.administrator:

        await bot.say("Target is an admin")
    elif target = None:
        await bot.say("You forgot the user")
    else:
        await bot.kick(target)
        await bot.say('Kicked{}'.format(target.mention))

@kick.error
async def kick_error(error, ctx):

if isinstance(error, CheckFailure):

     await bot.send_message(ctx.message.channel, "You do not have permissions")
elif isinstance(error, BadArgument):
    await bot.send_message(ctx.message.channel, "Could not identify target")
else:
    raise error

Upvotes: 1

Related Questions