阻止wechaty插件消息传递

阻止wechaty插件消息传递

情景说明

async def main() -> None:
    bot = MyBot()
    bot.use([
        Test1Plugin(),
        Test2Plugin(),
        Test3Plugin()
    ])
await bot.start()

这样你的业务代码就分离到了 Test1、2、3 中,但是实际运行中你会发现当你的机器人触发了 Test1 中的关键词后,wechaty 仍然会把消息继续向 Test2、3 中继续传递,这就造成了资源的浪费

解决方法

# pip install wechaty_plugin_contrib -i https://pypi.tuna.tsinghua.edu.cn/simple
from wechaty_plugin_contrib.message_controller import message_controller

class DingDongPlugin(WechatyPlugin):
    @message_controller.may_disable_message
    async def on_message(self, msg: Message) -> None:
        if msg.text() == "ding":
            await msg.say("dong")
            message_controller.disable_all_plugins(msg)