Python开发Twitch Moderation Bot的AutoMod模块报Message无send属性求助
问题修复方案
错误根因
你遇到的AttributeError: 'Message' object has no attribute 'send'报错属于TwitchIO库的API使用错误:
- TwitchIO 2.x及以上版本的
Message对象本身没有send方法,发送消息需要调用消息归属的频道对象的send方法 ctx上下文对象仅在@bot.command()装饰的指令回调函数中才会传入,event_message是事件回调,本身没有ctx参数,所以调用ctx.send也会报错
修正后的代码
@bot.event() async def event_message(message): # 跳过机器人自己发的消息,避免循环触发 if message.echo: return print(f"Message detected:\nSender: {message.author.name}\nContent: {message.content}") for word in banned_words: if word in message.content.lower(): print("I deleted that message.") # 先删除违规消息 await message.delete() # 调用频道对象的send方法发消息/指令 await message.channel.send(f"/timeout {message.author.name} 3s You sent a non-family-friendly word, but we're a family-friendly channel.") await message.channel.send("f ¦ [Purge, 3s TO]") # 匹配到违禁词就跳出循环,避免重复触发 return
额外注意事项
- 必须给你的机器人账号开通对应频道的 moderator 权限,在直播间发送
/mod 你的机器人用户名即可 - 也可以直接调用TwitchIO封装好的超时方法,比手动拼IRC指令更稳定:
await message.author.timeout(message.channel, duration=3, reason="发送了不适宜内容")
内容的提问来源于stack exchange,提问作者Slender the Blender
相关产品推荐
相关产品推荐

