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

使用discord-py-api开发的Discord自动回复机器人无法检测消息

问题

使用Python的discord-py-api库开发Discord自动回复机器人,但机器人无法检测到收到的消息。尝试修改细节(比如移除.channel)、更换两种代码写法后都无效——既不自动回复,也不打印回复日志,期望机器人能正常实现自动回复功能。

代码1

import discord


class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged on as', self.user)

    async def on_message(self, message):
        if message.author == self.user:
            return  

        else:
            await message.channel.send(self.user + ' is not here now. Please message later. Thank you.')
            print('One message replied to ' + message.author)


print("""
---------------------------------------------------------------------------
                          Discord-auto-replier
                           By orangetheorange
           https://github.com/orangetheorange/discord-replier
---------------------------------------------------------------------------

[*] Thank you for using, if you find any bugs, please report to me.
[*] Please read token.md for your discord token.

[!] If you enter a token, please keep it secretly, if someone has it he can
have full access to your account.           
""")

global token
token = ""
with open('token.txt', 'r', encoding='utf-8') as tok:
    if tok.read() == "":
        with open('token.txt', 'w', encoding='utf-8') as tok:
            token = input('Put your discord token here: ')
            tok.write(token)
    else:
        with open('token.txt', 'r', encoding='utf-8') as toke:
            token = toke.read()
print('Used token: ' + token)
intents = discord.Intents.default()
intents.message_content = True
client = MyClient(intents=intents)
client.run(token)

代码2

#!/bin/python

import discord


print("""
---------------------------------------------------------------------------
                          Discord-auto-replier
                           By orangetheorange
           https://github.com/orangetheorange/discord-replier
---------------------------------------------------------------------------

[*] Thank you for using, if you find any bugs, please report to me.
[*] Please read token.md for your discord token.

[!] If you enter a token, please keep it secretly, if someone has it he can
have full access to your account.           
""")

global token
token = ""
with open('token.txt', 'r', encoding='utf-8') as tok:
    if tok.read() == "":
        with open('token.txt', 'w', encoding='utf-8') as tok:
            token = input('Put your discord token here: ')
            tok.write(token)
    else:
        with open('token.txt', 'r', encoding='utf-8') as toke:
            token = toke.read()
print('Used token: ' + token)
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

    else:
        await message.channel.send(f'{message.author} is not here now. Please message later. Thank you.')
        print(f'One message replied to {message.author}')

client.run(token)
解决方案

你的代码存在两个核心问题,导致消息检测和回复失效:

  • 字符串拼接错误引发异常
    代码1的on_message方法中,直接将self.user(discord.User对象)与字符串拼接会触发类型错误,导致方法执行中断,后续的回复和日志打印都无法完成。代码2的这部分格式是正常的,但还有另一个关键问题。

  • 后台权限未匹配代码配置
    虽然代码里设置了intents.message_content = True,但必须在Discord开发者后台的机器人页面中,开启消息内容特权意图(Message Content Intent)。如果后台未开启,代码中的配置不会生效,机器人仍无法读取消息内容。

修复后的代码示例(基于代码2优化)

#!/bin/python
import discord

print("""
---------------------------------------------------------------------------
                          Discord-auto-replier
                           By orangetheorange
---------------------------------------------------------------------------

[*] 感谢使用,若发现bug请反馈。
[*] 请查看token.md了解如何获取Discord令牌。

[!] 令牌请妥善保管,一旦泄露他人可完全控制你的账号。           
""")

token = ""
# 优化令牌读取逻辑,避免重复打开文件
with open('token.txt', 'r+', encoding='utf-8') as tok:
    content = tok.read().strip()
    if not content:
        token = input('请输入你的Discord令牌:')
        tok.write(token)
    else:
        token = content
print(f'使用的令牌:{token}')

# 配置意图
intents = discord.Intents.default()
intents.message_content = True
intents.messages = True  # 额外开启消息意图,确保能接收消息事件

client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f'已登录为:{client.user}')

@client.event
async def on_message(message):
    # 忽略机器人自己发送的消息
    if message.author == client.user:
        return

    try:
        await message.channel.send(f'{message.author.mention} 目前不在,请稍后再发消息,谢谢。')
        print(f'已回复消息给:{message.author}')
    except Exception as e:
        print(f'回复时出错:{e}')

client.run(token)

必要的后台配置步骤

  1. 登录Discord开发者平台,找到你的机器人应用,进入「Bot」页面。
  2. 向下滚动到「Privileged Gateway Intents」区域,开启「Message Content Intent」开关。
  3. 确保机器人已被邀请到目标服务器,且拥有「发送消息」「读取消息历史」等必要权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 14:56:10