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

Discord机器人未识别命令提示功能开发求助:解决event message is not recognized问题

Discord Bot: Fixing "event message is not recognized" Error & Implementing Unrecognized Command Prompts

Hey there! Let's work through your Discord bot problem together. First, let's recap what you're trying to build:

  • Handle specific commands like ?help, ?creator, and ?ownerfavOP
  • Send a friendly prompt when users type a ?-prefixed command that isn't recognized

Here's the original code you shared that's hitting the "event message is not recognized" error:

import os
import discord
client = discord.Client()
unrecogcommand = "I don't recognize this command do **?help** for the list of the available commands :3"
@client.event
async def on_ready():
    print("We have logged in as {0.user}".format(client))
@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith('?'):
        if message.content.startswith('?ownerfavOP'):
            await message.channel.send("The server Owner really love Kaikai Kitan https://www.youtube.com/watch?v=i1P-9IspBus :3")
        if message.content.startswith('?creator'):
            await message.channel.send("PashPash#7668 is the username of the creator and have a yt channel https://www.youtube.com/channel/UCxJXY07JAsmJBaYf67KKk7Q")
        if message.content.startswith('?help'):
            await message.channel.send("Command available ```?help``` ```ownerfavOP```")
client.run(os.getenv('TOKEN'))

What's Causing the "event message is not recognized" Error?

The main issue here is Discord.py v2.x's intent requirements. Starting with v2.0, Discord requires bots to explicitly enable the message_content intent to read message content. If you don't enable this (both in the Discord Developer Portal and in your code), your bot won't receive message events at all—hence the "event not recognized" error.

Additionally, your code was missing logic to handle unrecognized commands, and using multiple independent if statements instead of elif can lead to unnecessary checks.

Fixed Code with Explanations

Here's the updated code that fixes the error and implements all your desired features:

import os
import discord

# Step 1: Configure intents (required for Discord.py v2.x)
intents = discord.Intents.default()
intents.message_content = True  # Enable message content access

# Initialize client with intents
client = discord.Client(intents=intents)

unrecogcommand = "I don't recognize this command do **?help** for the list of the available commands :3"

@client.event
async def on_ready():
    print("We have logged in as {0.user}".format(client))

@client.event
async def on_message(message):
    # Ignore messages sent by the bot itself
    if message.author == client.user:
        return
    
    # Handle all ?-prefixed commands
    if message.content.startswith('?'):
        # Use elif to avoid redundant checks
        if message.content.startswith('?ownerfavOP'):
            await message.channel.send("The server Owner really love Kaikai Kitan :3")
        elif message.content.startswith('?creator'):
            await message.channel.send("PashPash#7668 is the username of the creator and have a yt channel")
        elif message.content.startswith('?help'):
            # Fixed the command list to include all valid commands with ? prefix
            await message.channel.send("Command available ```?help``` ```?ownerfavOP``` ```?creator```")
        # Step 2: Handle unrecognized ? commands
        else:
            await message.channel.send(unrecogcommand)

client.run(os.getenv('TOKEN'))

Key Fixes & Improvements:

  1. Intent Setup:

    • We added explicit intent configuration to enable message_content access. You also need to enable this intent in your Discord Developer Portal:
      • Go to your bot's page → "Privileged Gateway Intents" → Toggle on "Message Content Intent"
  2. Command Logic Optimization:

    • Switched from multiple if statements to elif—this ensures only one command block runs per message, making the code more efficient.
    • Added an else block to catch any unrecognized ? commands and send your predefined prompt.
  3. Help Command Fix:

    • Updated the ?help message to include all valid commands with their correct ? prefix, so users know exactly what to type.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 21:02:44