-
Notifications
You must be signed in to change notification settings - Fork 1
/
mahiru.py
69 lines (57 loc) · 1.76 KB
/
mahiru.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import json
import os
import random
import re
from datetime import datetime, timedelta
from apscheduler.triggers.cron import CronTrigger
from .utils import *
logger = get_logger('kusa')
CONFIG = load_config()
ADMIN = CONFIG['ADMIN']
MAHIRU = os.path.expanduser('~/.kusa/mahiru.json')
DEFAULT_DATA = {
'jobs': [],
}
class Mahiru:
# 不也挺好吗.jpg
Passive = False
Active = True
Request = False
def __init__(self, **kwargs):
logger.info('初始化Mahiru')
self.api = kwargs['bot_api']
self.MINUTE = random.randint(10, 30)
if not os.path.exists(MAHIRU):
dumpjson(DEFAULT_DATA, MAHIRU)
async def execute_async(self, message):
msg = message['raw_message'].strip()
if re.match('订阅闹钟(.+)', msg):
# TODO
pass
return None
def jobs(self):
trigger = CronTrigger(minute='*')
job = (trigger, self.mahiru)
return (job,)
async def mahiru(self):
mahirudata = loadjson(MAHIRU)
dtstr = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
for j in mahirudata['jobs']:
if self.check(j['condition']):
msg = re.sub('\$DATETIME', dtstr, j['message'])
if j['type'] == 'private':
await self.api.send_private_msg(
user_id=j['target'],
message=msg,
)
if j['type'] == 'group':
await self.api.send_group_msg(
group_id=j['target'],
message=msg,
)
def check(self, condition):
now = datetime.now()
h = now.hour
m = now.minute
hm = h * 100 + m
return hm in condition