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

Replit开发的Discord Bot kick命令无法显示及功能异常问题

问题:Discord Bot的!kick命令无法正常触发显示

问题详情

在Replit中开发的Discord Bot,其!kick命令无法在服务器中被识别,机器人也未显示“支持命令”标识,调整邀请链接及初步调试后问题仍存在。

原代码

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.messages = True
intents.message_content = True

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

roles_to_monitor = [ROLE_ID]

monitored_channels = [CHANNEL_ID]


@bot.event
async def on_ready():
  print(f'We have logged in as {bot.user}')


@bot.event
async def on_message(message):
  if message.channel.id in monitored_channels and message.mentions:
    for mentioned_user in message.mentions:
      mentioned_roles = [
          role for role in mentioned_user.roles if role.id in roles_to_monitor
      ]
      if mentioned_roles:
        role_names = ', '.join([role.name for role in mentioned_roles])

        warning_message = f"{message.author.mention}, be careful, you pinged HR+ !"
        await message.channel.send(warning_message)

        embed = discord.Embed(description=warning_message,
                              color=discord.Color.red())
        embed.set_image(
            url="IMAGE_LINK")
        embed.add_field(
            name="Additional Message",
            value=("MESSGAE")
        await message.channel.send(embed=embed)

        break


allowed_roles = [ROLE_ID]


@bot.command()
async def kick(ctx, member: discord.Member):
  if any(role.id in allowed_roles for role in ctx.author.roles):
    await member.kick()
    await ctx.send(f"{member.mention} has been kicked.")
    print(f"Command enabled: /kick invoked by {ctx.author.name}")
  else:
    await ctx.send("You don't have permission to use this command.")


bot.run('BOT_TOKEN')

控制台输出

2023-07-31 03:51:55 INFO discord.client logging in using static token
2023-07-31 03:51:56 INFO discord.gateway Shard ID None has connected to Gateway (Session ID: ############).
We have logged in as AURP#4566

问题原因及修复方案

1. on_message事件拦截命令处理

discord.py中,自定义on_message事件会覆盖默认的命令处理流程,必须在事件末尾手动添加await bot.process_commands(message),否则命令系统无法识别并处理!kick命令。

2. 代码存在语法错误

embed.add_field方法的参数括号未闭合(value=("MESSGAE")缺少右括号),会导致代码运行异常,命令无法正常加载。

修复后的完整代码

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.messages = True
intents.message_content = True

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

roles_to_monitor = [ROLE_ID]

monitored_channels = [CHANNEL_ID]


@bot.event
async def on_ready():
  print(f'We have logged in as {bot.user}')


@bot.event
async def on_message(message):
  if message.channel.id in monitored_channels and message.mentions:
    for mentioned_user in message.mentions:
      mentioned_roles = [
          role for role in mentioned_user.roles if role.id in roles_to_monitor
      ]
      if mentioned_roles:
        role_names = ', '.join([role.name for role in mentioned_roles])

        warning_message = f"{message.author.mention}, be careful, you pinged HR+ !"
        await message.channel.send(warning_message)

        embed = discord.Embed(description=warning_message,
                              color=discord.Color.red())
        embed.set_image(
            url="IMAGE_LINK")
        # 修复:闭合add_field的参数括号
        embed.add_field(
            name="Additional Message",
            value=("MESSGAE")
        )
        await message.channel.send(embed=embed)

        break
  # 新增:手动触发命令处理流程
  await bot.process_commands(message)


allowed_roles = [ROLE_ID]


@bot.command()
async def kick(ctx, member: discord.Member):
  if any(role.id in allowed_roles for role in ctx.author.roles):
    await member.kick()
    await ctx.send(f"{member.mention} has been kicked.")
    print(f"Command enabled: !kick invoked by {ctx.author.name}")
  else:
    await ctx.send("You don't have permission to use this command.")


bot.run('BOT_TOKEN')

额外说明

  • 确保机器人邀请时拥有Kick Members权限,否则执行!kick时会报错。
  • 代码中ROLE_ID、CHANNEL_ID、IMAGE_LINK、BOT_TOKEN需替换为实际有效值。

内容的提问来源于stack exchange,提问作者Shashank Pandey

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 09:40:21