Replit上Discord Bot的>kick命令无响应问题求助
问题分析与修复方案
代码层面核心问题
你的>kick命令无响应的关键原因是自定义on_message事件未触发命令处理流程,同时代码存在两处语法/逻辑错误:
- 自定义
on_message事件后,没有调用await bot.process_commands(message),导致Discord.py的命令系统无法识别>kick命令。 kick函数中ctx.reply(embed=embed)缺少await关键字,会触发异步错误,被try-except静默捕获后无反馈。kick函数末尾的await bot.process_commands(ctx)属于冗余错误调用,完全不需要。
修复后的完整代码
import discord import os from discord.ext import commands bot_token = os.environ['TOKEN'] intent = discord.Intents.all() intent.message_content = True bot = commands.Bot(command_prefix=">", intents=intent) @bot.event async def on_ready(): print(f'Logged in as {bot.user}') @bot.event async def on_message(message): if message.author == bot.user: return if message.content.startswith('!hello'): await message.channel.send('Hello!') # 必须添加此行,让Bot处理命令 await bot.process_commands(message) @bot.command(name="kick") @commands.has_permissions(kick_members=True) async def kick(ctx, user: discord.Member, *, reason="No Reason"): try: await user.kick(reason=reason) embed = discord.Embed(color=discord.Colour.red()) embed.add_field( name="Kicked:", value=f"The user **{user}** has been kicked.\nReason = **{reason}**", inline=False ) await ctx.reply(embed=embed) except Exception as e: embed = discord.Embed(color=discord.Colour.red()) embed.add_field( name="Error:", value=f"Failed to kick user: {str(e)}", inline=False ) await ctx.reply(embed=embed) bot.run(bot_token)
额外验证项
- Discord权限配置:在Discord开发者平台的机器人页面,确认
Message Content Intent已开启;邀请机器人到服务器时,需赋予其Kick Members权限,且机器人角色层级高于目标用户。 - 用户权限:执行
>kick命令的用户必须拥有服务器的Kick Members权限。 - Replit环境:检查环境变量
TOKEN是否正确,确保项目处于持续运行状态。
内容的提问来源于stack exchange,提问作者ojasmaheshwari
相关产品推荐
相关产品推荐

