You need to enable JavaScript to run this app. discord.py跨服务器转发消息时报错'NoneType'对象无'send'属性
优惠活动
大模型
产品
解决方案
定价
更多

discord.py跨服务器转发消息时报错'NoneType'对象无'send'属性

问题排查与修复方案

核心报错说明

你遇到的AttributeError: 'NoneType' object has no attribute 'send'报错,本质是client.get_channel(849317968193912842)调用后返回了空值(None),没有成功获取到目标频道对象,同时代码还存在语法错误,具体问题和修复方法如下:

具体问题1:缺少必要的意图配置

discord.py v2.0及以上版本要求必须显式开启message_content意图,才能正常读取消息内容、获取跨服务器的频道对象。你当前代码仅开启了members意图,未开启该配置,需要同时确认Discord开发者平台的Privileged Gateway Intents中已经开启了Message Content Intent权限。

具体问题2:send方法调用语法错误

代码中await general.send (**message.content + ' . ' +message.author.name)里的**是Python的字典解包运算符,不能用在字符串参数前,属于多余的错误语法。

具体问题3:缺少频道存在性校验

调用get_channel后没有判断返回值是否有效,一旦频道获取失败就会直接触发属性调用报错。

修复后可运行代码

import os
import discord
intents = discord.Intents.default()
intents.members = True
# 新增开启消息内容意图
intents.message_content = True
client = discord.Client(intents=intents)
# 可以提前把目标频道ID存为变量方便修改
TARGET_CHANNEL_ID = 849317968193912842
@client.event
async def on_ready():
  # 启动时就校验频道是否能正常获取
  test_channel = client.get_channel(TARGET_CHANNEL_ID)
  if test_channel:
    print(f'成功获取目标频道:{test_channel.name}')
  else:
    print('警告:未找到目标频道,请检查ID是否正确、机器人是否已加入目标服务器')
  print('i am ready ')
my_secret = os.environ['TOKEN']
@client.event
async def on_message(message):
  if message.author == client.user:
    return
  if message.content.startswith('$work'):
    general = client.get_channel(TARGET_CHANNEL_ID)
    # 新增频道存在性判断
    if general:
      # 去掉多余的**运算符
      await general.send(message.content + ' . ' + message.author.name)
    else:
      await message.channel.send('未找到目标转发频道,请检查配置')
  if message.content.startswith('tesla'):
    await message.channel.send('elon musk' )
client.run(my_secret)

额外校验项

如果修改后仍无法获取频道,请确认:

  • 机器人已经被邀请加入目标频道所在的服务器
  • 机器人在目标服务器拥有查看频道、发送消息的权限
  • 目标频道ID复制正确,没有多输/少输数字

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.18 16:06:06