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

如何让机器人被提及后自动响应?现有代码无响应求助

解决机器人被提及无响应的问题

我来帮你排查下问题所在,你的代码没反应大概率是提及格式匹配错误导致的,另外还有几个细节需要调整:

问题分析

  • Discord里@机器人的消息内容,实际格式是<@!435379055253127178>或者<@435379055253127178>,不是直接的@435379055253127178,所以startswith根本匹配不到触发条件。
  • 另外看你用的是client.send_message,这是Discord.py v0.16.x版本的旧方法,如果你用的是v1.x+版本,这个方法已经被移除了,得用channel.send()。

修正方案

方案1:适配旧版Discord.py(v0.16.x)

直接匹配正确的提及格式,同时简化你的消息编辑逻辑:

# 先获取机器人的正确提及格式
bot_mention = f"<@!{client.user.id}>"  # 带昵称的提及格式
# 或者也可以同时匹配两种格式
bot_mention_alt = f"<@{client.user.id}>"

if bot_mention in message.content or bot_mention_alt in message.content:
    # 发送初始消息并获取消息对象
    text = await client.send_message(message.channel, "**Baking a cake**")
    # 编辑消息
    await client.edit_message(text, "Hi i'm cake bot nice to meet you!")

方案2:适配新版Discord.py(v1.x+)

如果是新版本,方法名已经变更,同时需要确保启用了message_content意图:

import discord

# 初始化客户端时启用意图
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)

@client.event
async def on_message(message):
    # 避免机器人自己触发自己
    if message.author == client.user:
        return
    
    bot_mention = f"<@!{client.user.id}>"
    bot_mention_alt = f"<@{client.user.id}>"
    if bot_mention in message.content or bot_mention_alt in message.content:
        text = await message.channel.send("**Baking a cake**")
        await text.edit(content="Hi i'm cake bot nice to meet you!")

额外检查项

  • 确保机器人在对应频道有发送消息和编辑消息的权限。
  • 如果是新版Discord.py,一定要在开发者门户里开启message_content意图,否则机器人接收不到消息内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:59:28