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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 13:18:05