3

qqbot插件-闪照转发

 1 year ago
source link: https://hx-w.github.io/article/51135/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
qqbot插件-闪照转发 - CarOL的小站

qqbot插_

贺翔/CarOL 2021年4月6日 晚上

1.2k 字

11 分钟

100 次

本文最后更新于:22 天前

利用改框架,可以实现破解闪照并转发到指定qq号的功能

qq的特殊消息都可以表示为CQ码(纪念原来的酷Q),CQ码文档可以参考go-cqhttp文档

我们需要的闪照的CQ码是这样的格式:

[CQ:image,type=flash,file=xxxx.image]
去掉type=flash,即可表示为正常的图片。

所以反闪照插件的功能思路就是:

  1. 利用正则表达式:\[CQ:image,type=flash,file=.*?\]捕获Bot接收到的所有闪照CQ码
  2. 解析出发送者,所在群号以及CQ码的全部信息
  3. 将CQ码中的type=flash,去掉,再私聊转发给预设的qq号即可

NoneBot2中提供了on_regex装饰器,可以直接正则监视所有满足条件的消息,

而NoneBot1并未提供类似on_regex的正则匹配装饰器,所以需要用on_message监听所有消息,再对消息进行正则过滤。

为了减少计算压力,只监听群聊中的消息即可。

import re
from nonebot import get_bot

target_user = 765892480

pattens = re.compile(r"\[CQ:image,type=flash,file=.*?\]")

bot = get_bot()

@bot.on_message("group")
async def AntiFlashImage(event):
raw_info = (await bot.get_msg(message_id=event.message_id))
raw_message = raw_info["raw_message"]
if re.match(pattens, raw_message):
image_ = raw_message.replace('type=flash,', '')
new_message = (
f"在群({raw_info['group_id']})中捕获闪照\n"
f"发送者:{raw_info['sender']['nickname']}({raw_info['sender']['user_id']})\n"
f"{image_}"
)
await bot.send_private_msg(user_id=target_user, message=new_message)

群聊发送闪图

Bot私聊转发原图


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK