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

Discord工单状态机器人代码异常:无法发消息及特权意图缺失报错

Discord工单状态机器人无法发送消息及权限报错解决

问题情况

开发的Discord工单状态机器人已成功登录,但无法向指定频道发送消息。已在开发者门户配置权限、代码中指定服务器信息、服务器内设置对应权限,仍收到报错:

Privileged message content intent is missing, commands may not work as expected.

报错原因

Discord的Message Content Intent属于特权网关意图,必须在开发者门户手动开启,同时代码中也需要显式声明该意图,否则机器人无法读取消息内容,也无法正常触发on_message事件或发送消息。

修复步骤

1. 开启开发者门户的特权意图

  • 登录Discord开发者门户,进入你的机器人应用
  • 切换到「Bot」标签页
  • 找到「Privileged Gateway Intents」区域,开启「Message Content Intent」开关

2. 修正代码中的问题

  • 补充特权意图声明:在intents配置中添加intents.message_content = True
  • 修正频道ID类型:ticket_channel_id需要是整数类型,而非字符串
  • 移除无效代码:删除无意义的commands.Bot行
  • 修复消息发送逻辑:移除on_message事件中重复的消息发送代码,避免变量未定义报错;添加机器人自我消息过滤,防止循环触发

修正后的完整代码

import discord
from discord.ext import commands
import datetime

# 配置意图,添加Message Content特权意图
intents = discord.Intents.default()
intents.guilds = True
intents.messages = True
intents.message_content = True  # 新增:开启消息内容特权意图

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

# 频道ID改为整数类型
ticket_channel_id = 1217573242009817138
open_tickets_count = 5  

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

@bot.event
async def on_message(message):
    # 先处理默认命令逻辑,避免事件冲突
    await bot.process_commands(message)
    
    # 过滤机器人自身消息,防止循环触发
    if message.author.bot:
        return
        
    if message.channel.id == ticket_channel_id:
        channel = message.channel
        ticket_status = ''
        if open_tickets_count < 10:
            ticket_status = 'Low'
        elif 10 <= open_tickets_count <= 15:
            ticket_status = 'Moderate'
        else:
            ticket_status = 'Busy'

        now = datetime.datetime.now()
        day = now.weekday()  # 0 (周一) 到 6 (周日)
        hour = now.hour  # 0 到 23

        if 0 <= day <= 4:  # 周一到周五(修正原代码日期判断错误)
            if 9 <= hour < 17:
                response_time = 'Slow'
            elif hour < 21:
                response_time = 'Fast'
            else:
                response_time = '次日回复可能性低'
        elif day == 5:  # 周六
            if hour < 10:
                response_time = 'Slow'
            elif hour < 18:
                response_time = 'Fast'
            elif hour < 21:
                response_time = 'Moderate'
            else:
                response_time = '次日回复可能性低'
        else:  # 周日
            response_time = '回复可能性低'

        response_message = f'工单状态: {ticket_status}\n回复时效: {response_time}'
        try:
            await channel.send(response_message)
        except discord.HTTPException as e:
            print(f'发送消息失败: {e}')

bot.run('你的机器人令牌')

注:原代码中weekday()的日期判断逻辑有误(原1-5对应周二到周六),已修正为0-4对应周一到周五,确保时间规则正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 06:42:38