Discord Bot Kick命令异常求助:执行成功却提示错误
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_permissionsisn't available, you can switch back toserver_permissions. ctx.send()is the modern replacement forbot.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

