discord.py运行触发AttributeError: 'int' object has no attribute 'send'求助
报错根因
你代码里的CHANNEL_ID是整数类型的频道ID,直接赋值给channel变量后,channel为整数对象,而discord.py 只有discord.TextChannel类的实例才具备send()方法,整数对象本身没有该方法,因此触发AttributeError报错。
修复方案
你需要先通过频道ID获取到对应的频道对象,再调用send()方法,修改后的代码参考如下:
# 确保已经导入你定义的CHANNEL_ID from tokens import CHANNEL_ID @client.event async def on_message(message): # 先从客户端缓存中获取频道对象 channel = client.get_channel(CHANNEL_ID) # 缓存中不存在时主动拉取频道信息 if not channel: channel = await client.fetch_channel(CHANNEL_ID) if not message.author.bot: await channel.send(message.content)
额外注意事项
- 若你使用的是discord.py 2.0及以上版本,需要先在Discord开发者后台开启消息内容意图,同时在初始化client时声明对应意图,否则无法正常读取消息内容:
import discord intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents=intents)
- 原代码中冗余的
if isbot: pass判断可以直接简化为取反判断,代码更简洁。
内容的提问来源于stack exchange,提问作者treeqr
相关产品推荐
相关产品推荐

