Skip to content

Commit

Permalink
Merge pull request #15 from Reversedeer/dev
Browse files Browse the repository at this point in the history
更新v0.1.8
  • Loading branch information
Reversedeer authored Jul 23, 2023
2 parents 223fb2b + 4b9d9f9 commit 9d09d92
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 213 deletions.
67 changes: 34 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,6 @@ _✨ 基于NoneBot2实现的 监测QQ群事件 插件 ✨_
>
> 群成员荣誉变更时,发送变更通知
## 指令结构:

```
path = {
'chuo': ['戳一戳'],
'honor': ['群荣誉检测'],
'files': ['群文件检测'],
'del_user': ['群成员减少检测'],
'add_user': ['群成员增加检测'],
'admin': ['管理员变动检测'],
'red_package': ['运气王检测']
}
```



## 安装方式

### nb-cli安装(推荐)
Expand Down Expand Up @@ -79,39 +63,55 @@ pip install --upgrade nonebot-plugin-eventmonitor
| :-----: | :--: | :-----: | :----------: | :----------------: |
| chuo_cd | int | 0 | chuo_cd = 10 | 戳一戳的cd(选填) |

## 指令结构帮助:

```
usage = """
指令1:戳一戳(戳一戳bot获取文案)
指令2:群荣誉监测(检测群聊中龙王,群聊之火,快乐源泉的获得并发送提示,当 bot获得群荣誉时有特殊消息)
指令3:群文件检测(检测所有人发送群文件并发送提示)
指令4:群成员减少检测(当有人退群时,发送退群消息;当主人/superuser退群有特殊回复)
指令5:群成员增加检测(当有人入群时,发送入群欢迎,当bot首次入群会乞讨管理,当主人/superuser入群会有特殊回复)
指令6:管理员变动检测(当新增管理员或取消管理员时发送消息提示,当bot自身被上/下管理时有特殊回复)
指令7:运气王检测(检测抢红包检测后的运气王并发送提示消息)
"""
```

<details>
<summary><h2>更新日志</h2></summary>

- v0.1.7
- v0.1.8

- 新增所有功能开关[#issue5](https://github.com/Reversedeer/nonebot_plugin_eventmonitor/issues/9)

- 增加权限控制
- 修复潜在的bug
- 🚨增加功能开关指令:event状态/event配置
- 🐛修复群文件不能检测bug(少写一个字母qwq)
- 优化目录结构

- v0.1.6
- 修复bug
- v0.1.7
- 🚨新增所有功能开关[#issue5](https://github.com/Reversedeer/nonebot_plugin_eventmonitor/issues/9)

- 🚨新增权限控制
- 🐛修复潜在的bug
- v0.1.6
- 🐛修复bug
- v0.1.5
- 🐛修复获取superusers数值bug
- 优化配置文件 [#issue4](https://github.com/Reversedeer/nonebot_plugin_eventmonitor/issues/6)
- 删除冗余代码
- 修复获取superusers数值bug

- v0.1.3
- 修复配置文件bug
- 🐛修复配置文件bug
- v0.1.2
- 增加了戳一戳的文案
- 🚨增加戳一戳的文案

- 修改一些bug
- 🐛修复bug
- v0.1.1

- 好像改了什么..忘了...
- 🐛修复bug
- v0.1.0
- 🚨新增戳一戳加了cd(本人觉得功能鸡肋)
- 🐛修复管理员变动时API报错问题[#issue1](https://github.com/Reversedeer/nonebot_plugin_eventmonitor/issues/1)
- 抛弃原有的配置模式
- 给戳一戳加了cd(本人觉得功能鸡肋)
- 修复管理员变动时API报错问题[#issue1](https://github.com/Reversedeer/nonebot_plugin_eventmonitor/issues/1)
- v0.0.6
- 修复了大量的bug
- 🐛修复了大量的bug
</details>

## 关于 ISSUE
Expand All @@ -134,6 +134,7 @@ pip install --upgrade nonebot-plugin-eventmonitor

## 其他插件

[舔狗日记](https://github.com/Reversedeer/nonebot_plugin_dog)

[使用API的chatGPTQQ聊天机器人](https://github.com/Reversedeer/nonebot_plugin_chatGPT_openai)

[舔狗日记](https://github.com/Reversedeer/nonebot_plugin_dog)
178 changes: 18 additions & 160 deletions nonebot_plugin_eventmonitor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,171 +1,29 @@
import nonebot
from nonebot.rule import Rule
from nonebot.plugin import on_notice, on_command
from nonebot.permission import SUPERUSER
from nonebot.adapters.onebot.v11.permission import GROUP_OWNER, GROUP_ADMIN
from nonebot.adapters.onebot.v11 import (
Bot, Event, Message,
PokeNotifyEvent,
HonorNotifyEvent,
GroupUploadNoticeEvent,
GroupDecreaseNoticeEvent,
GroupIncreaseNoticeEvent,
GroupAdminNoticeEvent,
LuckyKingNotifyEvent,
MessageSegment,
GroupMessageEvent
)
import contextlib

from .stamp import chuo_send_msg
from .honour import monitor_rongyu
from .admin import del_user_bye, add_user_wecome, admin_changer
from .utils import *


# 获取戳一戳状态
async def _is_poke(event: Event) -> bool:
return isinstance(event, PokeNotifyEvent) and event.is_tome()
# 获取群荣誉变更
async def _is_rongyu(event: Event) -> bool:
return isinstance(event, HonorNotifyEvent)
# 获取文件上传
async def _is_checker(event: Event) -> bool:
return isinstance(event, GroupUploadNoticeEvent)
# 群成员减少
async def _is_del_user(event: Event) -> bool:
return isinstance(event, GroupDecreaseNoticeEvent)
# 群成员增加
async def _is_add_user(event: Event) -> bool:
return isinstance(event, GroupIncreaseNoticeEvent)
# 管理员变动
async def _is_admin_change(event: Event) -> bool:
return isinstance(event, GroupAdminNoticeEvent)
# 红包运气王
async def _is_red_packet(event: Event) -> bool:
return isinstance(event, LuckyKingNotifyEvent)

# 戳一戳
chuo = on_notice(Rule(_is_poke), priority=50, block=True)
# 群荣誉
qrongyu = on_notice(Rule(_is_rongyu), priority=50, block=True)
# 群文件
files = on_notice(Rule(_is_checker), priority=50, block=True)
# 群员减少
del_user = on_notice(Rule(_is_del_user), priority=50, block=True)
# 群员增加
add_user = on_notice(Rule(_is_add_user), priority=50, block=True)
# 群管理
group_admin = on_notice(Rule(_is_admin_change), priority=50, block=True)
# 红包
red_packet = on_notice(Rule(_is_red_packet), priority=50, block=True)

switch_command = on_command("开启", aliases={"关闭"}, permission=SUPERUSER | GROUP_ADMIN | GROUP_OWNER,
priority=1, block=True)

#初始化配置配置文件
@driver.on_bot_connect
async def _():
await init(g_temp)
await config_check()


@chuo.handle()
async def send_chuo(event: PokeNotifyEvent):
if not (await check_chuo(g_temp, str(event.group_id))):
await chuo.finish(notAllow, at_sender=True)
uid = event.get_user_id() # 获取用户id
try:
cd = event.time - chuo_CD_dir[uid] # 计算cd
except KeyError:
cd = chuo_cd + 1 # 没有记录则cd为cd_time+1
if (
cd > chuo_cd
or event.get_user_id() in nonebot.get_driver().config.superusers
): # 记录cd
chuo_CD_dir.update({uid: event.time})
rely_msg = await chuo_send_msg()
await chuo.finish(message=Message(rely_msg))

@qrongyu.handle() #群荣誉变化
async def send_rongyu(event: HonorNotifyEvent, bot: Bot):
if not (await check_honor(g_temp, str(event.group_id))):
await qrongyu.finish(notAllow, at_sender=True)
bot_qq = int(bot.self_id)
rely_msg = await monitor_rongyu(event.honor_type, event.user_id, bot_qq)
await qrongyu.finish(message=Message(rely_msg))

@files.handle() #上传群文件
async def handle_first_receive(event: GroupUploadNoticeEvent):
if not (await check_file(g_temp, str(event.group_id))):
await files.finish(notAllow, at_sender=True)
rely = MessageSegment.at(event.user_id) + '\n' + \
MessageSegment.image(f'https://q4.qlogo.cn/headimg_dl?dst_uin={event.user_id}&spec=640') + \
'\n 上传了新文件,感谢你一直为群里做贡献喵~' + MessageSegment.face(175)
await files.finish(message=rely)

@del_user.handle() #退群事件
async def user_bye(event: GroupDecreaseNoticeEvent):
if not (await check_del_user(g_temp, str(event.group_id))):
await del_user.finish(notAllow, at_sender=True)
rely_msg = await del_user_bye(event.time, event.user_id)
await del_user.finish(message=Message(rely_msg))


@add_user.handle() #入群事件
async def user_welcome(event: GroupIncreaseNoticeEvent, bot: Bot):
if not (await check_add_user(g_temp, str(event.group_id))):
await add_user.finish(notAllow, at_sender=True)
bot_qq = int(bot.self_id)
rely_msg = await add_user_wecome(event.time, event.user_id, bot_qq)
await add_user.finish(message=Message(rely_msg))


@group_admin.handle() #管理员变动
async def admin_chance(event: GroupAdminNoticeEvent, bot: Bot):
if not (await check_admin(g_temp, str(event.group_id))):
await group_admin.finish(notAllow, at_sender=True)
bot_qq = int(bot.self_id)
rely_msg = await admin_changer(event.sub_type, event.user_id, bot_qq)
await group_admin.finish(message=Message(rely_msg))

@red_packet.handle() #红包运气王
async def hongbao(event: LuckyKingNotifyEvent):
if not (await check_red_package(g_temp, str(event.group_id))):
await red_packet.finish(notAllow, at_sender=True)
rely_msg = MessageSegment.at(event.user_id) + "\n" + "本次红包运气王为:" + MessageSegment.at(event.target_id)
await red_packet.finish(message=rely_msg)


def get_command_type(command: str) -> str:
"""根据指令内容获取开关类型"""
return next(
(
key
for key, keywords in path.items()
if any(keyword in command for keyword in keywords)
),
"",
)

@switch_command.handle()
async def switch(event: GroupMessageEvent):
# 获取开关指令的参数,即用户输入的指令内容
command = str(event.get_message()).strip()
# 获取群组ID
gid = str(event.group_id)
# 判断指令是否包含"开启"或"关闭"关键字
if "开启" in command or "开始" in command:
if key := get_command_type(command):
g_temp[gid][key] = True
write_group_data(g_temp)
name = get_function_name(key)
await switch_command.finish(f"{name}功能已开启喵")
elif "禁止" in command or "关闭" in command:
if key := get_command_type(command):
g_temp[gid][key] = False
write_group_data(g_temp)
name = get_function_name(key)
await switch_command.finish(f"{name}功能已禁用喵")

def get_function_name(key: str) -> str:
"""根据关键词获取对应功能名称"""
return path[key][0]
with contextlib.suppress(Exception):
from nonebot.plugin import PluginMetadata

__plugin_meta__ = PluginMetadata(
name="eventmonitor",
description="监控群事件的插件,支持戳一戳,成员变动,群荣誉变化等提示的插件",
usage=utils.usage,
type="application",
homepage="https://github.com/Reversedeer/nonebot_plugin_eventmonitor",
supported_adapters={"onebot.v11"},
extra={
"author": "Reversedeer",
"version": "0.1.8",
"priority": 50,
},
)
35 changes: 27 additions & 8 deletions nonebot_plugin_eventmonitor/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,60 @@

async def admin_changer(sub_type, user_id, bot_qq) :
admin_msg = ""


# 根据管理员变动类型选择不同的消息
if sub_type == "set":
# 如果用户ID等于机器人的QQ号,返回特定消息
admin_msg = (
"我也是管理啦,你们要小心喵~"
if user_id == bot_qq
else f"🚔 管理员变动\n恭喜[CQ:at,qq={user_id}]喜提本群管理喵~"
else f"🚔 管理员变动\n恭喜@{user_id}喜提本群管理喵~"
)
elif sub_type == "unset":
# 如果用户ID等于机器人的QQ号,返回特定消息
admin_msg = (
"呜呜,别下咱管理呀QwQ,喵呜~"
if user_id == bot_qq
else f"🚔 管理员变动\n[CQ:at,qq={user_id}]痛失本群管理喵~"
else f"🚔 管理员变动\n@{user_id}痛失本群管理喵~"
)

return admin_msg


async def del_user_bye(add_time, user_id):
global del_user_msg
del_time = datetime.fromtimestamp(add_time)

# 检查用户ID是否在超级用户列表superusers中
if user_id in superusers:
# 如果是超级用户,生成特定的离开消息
del_user_msg = f"⌈{del_time}\n@{user_id}恭送主人离开喵~"
print(superusers)
else:
del_user_msg = f"✈️ 成员变动⌈{del_time}\nQQ号为:{user_id}的小可爱退群喵~" \
# 如果不是超级用户,生成通用的离开消息,包含用户的QQ号和头像图片
del_user_msg = f"✈️ 成员变动 ✈️ \n时间: ⌈{del_time}\nQQ号为:{user_id}的小可爱退群喵~" \
f"[CQ:image,file=https://q4.qlogo.cn/headimg_dl?dst_uin={user_id}&spec=640]"

return del_user_msg


async def add_user_wecome(add_time, user_id, bot_qq):
global groups_all, add_user_msg
add_time = datetime.fromtimestamp(add_time)
global groups_all, add_user_msg
# 将时间戳转换为datetime类型的时间add_time
add_time = datetime.fromtimestamp(add_time)
# 判断用户ID是否等于机器人的QQ号
if user_id == bot_qq:
# 如果是机器人自己加入群组,生成特定的欢迎消息
add_user_msg = f"本喵被邀进入贵群喵~\n" \
f"火速上个管理喵~"
# 判断用户ID是否在超级用户列表superusers中
elif user_id in superusers:
add_user_msg = f"✨ 成员变动 ✨\n@{user_id}[CQ:at,qq={user_id}]欢迎主人进群喵~[CQ:face,id=175]"
# 如果是超级用户加入群组,生成特定的欢迎消息,包含用户ID和CQ表情
add_user_msg = f"✨ 成员变动 ✨\n@{user_id}欢迎主人进群喵~[CQ:face,id=175]"
else:
add_user_msg = f"✨ 成员变动 ✨\n欢迎[CQ:at,qq={user_id}]的加入喵~\n加入时间:⌈{add_time}⌋,请在群内积极发言喵~" \
# 如果是普通用户加入群组,生成通用的欢迎消息,包含用户ID、加入时间和用户头像图片的链接
add_user_msg = f"✨ 成员变动 ✨\n欢迎@{user_id}的加入喵~\n加入时间:⌈{add_time}⌋,请在群内积极发言喵~" \
f"[CQ:image,file=https://q4.qlogo.cn/headimg_dl?dst_uin={user_id}&spec=640]"

return add_user_msg

Loading

0 comments on commit 9d09d92

Please sign in to comment.