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

Disnake on_message_edit事件报错:找不到message.channel及参数异常求助

问题解决方法

错误1:Cannot find reference 'channel' in 'message'

你代码里用了message.channel.id,但message这个变量根本没在函数参数里定义——on_message_edit事件的参数是before(编辑前的消息)和after(编辑后的消息),这俩才是有效的消息对象,都带channel属性。把message换成before或者after就行(编辑前后消息所在频道不会变,用哪个都一样)。

错误2:TypeError: on_message_edit() missing 1 required positional argument: 'after'

Disnake的on_message_edit事件签名是固定的,必须接收before和after两个参数,你额外加message参数后,事件触发时只会传before和after,导致参数不匹配,所以报错。必须保持函数参数为async def on_message_edit(before, after):,不能乱加参数。

修正后的完整代码

import disnake
from datetime import datetime

@bot.event
async def on_message_edit(before, after):
    # 用before.channel.id替代原来的message.channel.id
    if before.channel.id == 1218632975441330266:
        pass
    else:
        chan = bot.get_channel(1203013518408810497)
        embed = disnake.Embed(
            title=f"{before.author} redacted his message",
            description=f"Before: {before.content}\nAfter: {after.content}\nAuthor: {before.author.mention}\nWhere: {before.channel.mention}",
            timestamp=datetime.now(), 
            color=disnake.Colour.blue()
        )
        embed.set_author(name=after.author.name, icon_url=after.author.display_avatar.url)
        # 注意:display_avatar需要加.url才能获取头像链接
        await chan.send(embed=embed)

额外注意点

  • 原代码里after.author.display_avatar直接用的话可能有问题,需要加.url才能拿到可访问的头像链接,否则Embed显示不出头像
  • 确保导入了datetime模块,不然datetime.now()会报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 15:02:14