discord.py编写自动清理消息事件时出现AttributeError如何解决
错误原因
AttributeError: 'TextChannel' object has no attribute 'get_channel' 报错的核心是你搞错了get_channel()方法的所属对象:
get_channel()是discord.Client类的方法,只能通过你的客户端实例client调用message.channel本身已经是TextChannel类的实例,没有这个方法
你的需求是只在ID为763827099084521484的频道里触发自动删消息逻辑,不需要调用get_channel,直接判断当前频道的ID是否匹配即可。
修复后的代码
import asyncio # 确保你已导入asyncio库 @client.event async def on_message(message): if message.author == client.user: return # 直接对比当前频道ID和目标频道ID if message.channel.id == 763827099084521484: await asyncio.sleep(5) await message.delete() # 如果你还注册了其他文本指令,必须加这行避免指令失效 await client.process_commands(message)
补充说明
如果你确实需要通过ID获取频道对象做其他逻辑操作,正确写法是client.get_channel(763827099084521484),但当前场景下直接对比ID是性能最优、逻辑最简洁的写法。
内容的提问来源于stack exchange,提问作者COD NeX
相关产品推荐
相关产品推荐

