Skip to content

Commit

Permalink
Merge pull request #21 from MerCuJerry/master
Browse files Browse the repository at this point in the history
在非必要情况下不触发记录群聊信息响应器 && 支持Nonebot 2.2.0
  • Loading branch information
BigOrangeQWQ authored Feb 19, 2024
2 parents 1f96d10 + 87fda44 commit 5943640
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
16 changes: 7 additions & 9 deletions nonebot_plugin_orangedice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from re import search, Match
from random import choice
from typing import Dict, Union
from nonebot import get_driver
from nonebot import get_plugin_config
from nonebot.params import Depends
from nonebot.matcher import Matcher
from nonebot.plugin import on_startswith, on_message, PluginMetadata
Expand Down Expand Up @@ -55,14 +55,13 @@
insane_list = on_startswith(".list", priority=4) # 获取所有疯狂表
temp_insane = on_startswith(".ti", priority=5) # 临时疯狂表
forever_insane = on_startswith(".li", priority=5) # 永久疯狂表

# -> 非阻断响应器
log_msg = on_message(priority=1, block=False) # 记录日志

# -> 数据相关
driver = get_driver()
plugin_config = Config.parse_obj(driver.config)
plugin_config = get_plugin_config(Config)
data = DataContainer()
# -> 非阻断响应器
async def log_msg_rule(event: GroupMessageEvent) -> bool:
return data.is_logging(event.group_id)
log_msg = on_message(rule=log_msg_rule, priority=1, block=False) # 记录日志


@roll.handle()
Expand Down Expand Up @@ -211,8 +210,7 @@ async def log_msg_handle(event: GroupMessageEvent):
group_id: int = event.group_id
msg: str = event.message.extract_plain_text()
name: str = get_name(event)
if data.is_logging(group_id):
data.log_add(group_id, f'[{name}] {msg}')
data.log_add(group_id, f'[{name}] {msg}')


@roll_p.handle()
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_orangedice/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pydantic import BaseModel, Extra
from pydantic import BaseModel

class Config(BaseModel, extra=Extra.ignore):
class Config(BaseModel):
cache_file: str = 'cache.txt'
sqlite_file: str = 'orange_dice.db'
12 changes: 7 additions & 5 deletions nonebot_plugin_orangedice/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

import json
from typing import List
from nonebot import get_driver
from nonebot import get_plugin_config
from sqlmodel import Field, SQLModel, create_engine,select, Session

from .config import Config


Expand All @@ -25,7 +24,7 @@ class GroupLOG(SQLModel, table=True):
class DataContainer:

def __init__(self) -> None:
config = Config.parse_obj(get_driver().config)
config = get_plugin_config(Config)
self.sqlite_file = config.sqlite_file
self.engine = create_engine(f"sqlite:///{self.sqlite_file}")
SQLModel.metadata.create_all(self.engine)
Expand Down Expand Up @@ -124,9 +123,12 @@ def log_add(self, group_id: int, msg: str):
statement = select(GroupLOG).where(GroupLOG.group_id == group_id)
log = session.exec(statement).first()
if log:
a = json.loads(log.msg)
try:
a = json.loads(log.msg)
except json.decoder.JSONDecodeError:
a = []
a.append(msg)
log.msg = str(a)
log.msg = json.dumps(a, ensure_ascii=False)
session.add(log)
else:
log = GroupLOG(group_id=group_id, log=False, msg=f'[{msg}]')
Expand Down

0 comments on commit 5943640

Please sign in to comment.