You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord Bot Kick命令异常求助:执行成功却提示错误

Fixing Your Discord Bot's Kick Command Problems

Let's work through the two issues you're having with your kick command:

1. Why you see "Something went wrong" instead of the mention message

The main issue here is a simple variable name mismatch. You've got the target user stored in the target parameter, but when you try to send the success message, you're using member.mention—and member doesn't exist in that scope. This throws an uncaught error, so your code jumps straight to the except block and shows the generic error message.

Fix this by swapping member.mention with target.mention:

await bot.say('Kicked {}'.format(target.mention))

2. Why there are two kick commands available

This is probably because of the pass_context=True parameter combined with the name="kick" argument. In newer versions of discord.py, pass_context=True is deprecated—context is automatically passed to commands now. When you set both the function name to kick and explicitly set name="kick", it can cause the command to register twice.

Remove the pass_context=True line to fix this. Also double-check your code to make sure you haven't accidentally defined the kick command somewhere else.

Updated Working Code

Here's the full fixed version, with a couple of extra improvements for compatibility with newer discord.py versions:

@bot.command(name="kick")
@has_permissions(kick_members=True)
async def kick(ctx, *, target: Member):
    # Note: server_permissions was renamed to guild_permissions in recent discord.py versions
    if target.guild_permissions.administrator:
        await ctx.send("Target is an admin")
    else:
        try:
            await bot.kick(target)
            await ctx.send('Kicked {}'.format(target.mention))
        except Exception as e:
            # Print the error to your console for easier debugging
            print(f"Error kicking user: {str(e)}")
            await ctx.send("Something went wrong")

@kick.error
async def kick_error(ctx, error):
    # Parameter order changed in newer versions - ctx comes first
    if isinstance(error, CheckFailure):
        await ctx.send("You do not have permissions")
    elif isinstance(error, BadArgument):
        await ctx.send("Could not identify target")
    else:
        raise error

Quick Additional Tips:

  • If you're using an older version of discord.py where guild_permissions isn't available, you can switch back to server_permissions.
  • ctx.send() is the modern replacement for bot.say()—it works the same but is more consistent with other discord.py methods.
  • By capturing the exception with as e, you can see exactly what's going wrong in your console, which is super helpful for debugging weird issues.

内容的提问来源于stack exchange,提问作者bos gamer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:16:20