-
Notifications
You must be signed in to change notification settings - Fork 7
/
__init__.py
346 lines (325 loc) · 13.6 KB
/
__init__.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import traceback
from hoshino import Service, priv, get_bot, logger
import os
import hoshino
import nonebot
from .get_morning import *
from .get_night import *
from .charge import *
sv = Service('good_morning', bundle='早安晚安')
sv_help = '''== 命令 ==
[早安] 早安喵
[晚安] 晚安喵
[我的作息] 看看自己的作息
[群友作息] 看看今天几个人睡觉或起床了
[早安晚安配置] 查看超级管理员设置的配置
== 限超级管理员的设置 ==
[早安晚安初始化] 首次使用请初始化
= 配置(详情看文档) =
[早安开启 xx] 开启某个配置
[早安关闭 xx] 关闭某个配置
[早安设置 xx x] 设置数值
[晚安开启 xx] 开启某个配置
[晚安关闭 xx] 关闭某个配置
[晚安设置 xx x] 设置数值'''.strip()
def get_sex_str(sex,setting):
if not setting:
if sex == 'male':
sex_str = '少年'
elif sex == 'female':
sex_str = '少女'
else:
sex_str = '群友'
else:
sex_str = '美少女'
return sex_str
async def get_groups():
'''
获取群列表,支持多cq端
'''
bot = nonebot.get_bot()
gl = []
for sid in hoshino.get_self_ids():
gl_ = await bot.get_group_list(self_id=sid)
for g in gl_:
if g not in gl:
gl.append(g)
return gl
#帮助界面
@sv.on_fullmatch('早安晚安帮助')
async def help(bot, ev):
await bot.send(ev, sv_help)
@sv.scheduled_job('cron', hour='2', minute='59')
async def create_json_daily():
try:
group_list = await get_groups()
all_num = len(group_list)
num = 0
for each_g in group_list:
group_id = each_g['group_id']
_current_dir = os.path.join(os.path.dirname(__file__), 'data', f'{group_id}.json')
if not os.path.exists(_current_dir):
data = {
"today_count": {
"morning": 0,
"night": 0
}
}
with open(_current_dir, "w", encoding="UTF-8") as f:
json.dump(data, f, ensure_ascii=False, indent=4)
num += 1
if num:
x_num = all_num - num
msg = f'检测到{all_num}个群中:\n- {x_num}个群信息已存在\n- {num}个群信息不存在\n现已为信息不存在的群成功创建文件!'
else:
# 配置信息均已存在
return
except:
msg = '早安晚安初始化失败!'
logger.exception(msg)
@sv.on_fullmatch('早安晚安初始化')
async def create_json(bot, ev):
if not priv.check_priv(ev, priv.SUPERUSER):
msg = '很抱歉您没有权限进行此操作,该操作仅限维护组'
await bot.send(ev, msg)
return
try:
group_list = await get_groups()
all_num = len(group_list)
num = 0
for each_g in group_list:
group_id = each_g['group_id']
_current_dir = os.path.join(os.path.dirname(__file__), 'data', f'{group_id}.json')
if not os.path.exists(_current_dir):
data = {
"today_count": {
"morning": 0,
"night": 0
}
}
with open(_current_dir, "w", encoding="UTF-8") as f:
json.dump(data, f, ensure_ascii=False, indent=4)
num += 1
if num:
x_num = all_num - num
msg = f'检测到{all_num}个群中:\n- {x_num}个群信息已存在\n- {num}个群信息不存在]\n现已为信息不存在的群成功创建文件!'
else:
msg = f'检测到{all_num}个群的配置信息均已存在,无需再次初始化'
except:
msg = '早安晚安初始化失败!'
traceback.print_exc()
await bot.send(ev, msg)
@sv.on_fullmatch('早安',"早","早上好","起床")
async def good_morning(bot, ev):
user_id = ev.user_id
group_id = ev.group_id
mem_info = await bot.get_group_member_info(group_id = group_id, user_id = user_id)
sex = mem_info['sex']
sex_str = get_sex_str(sex,setting)
msg = get_morning_msg(group_id, user_id, sex_str)
await bot.send(ev, msg)
@sv.on_fullmatch('晚安',"睡觉","睡觉觉","上床睡觉")
async def good_night(bot, ev):
user_id = ev.user_id
group_id = ev.group_id
mem_info = await bot.get_group_member_info(group_id = group_id, user_id = user_id)
sex = mem_info['sex']
sex_str = get_sex_str(sex,setting)
msg = get_night_msg(group_id, user_id, sex_str)
await bot.send(ev, msg)
# 23:59清除一天的早安晚安计数
@sv.scheduled_job('cron', hour='23', minute='59')
async def reset_data():
bot = get_bot()
group_list = await get_groups()
for each_g in group_list:
group_id = each_g['group_id']
current_dir = os.path.join(os.path.dirname(__file__), 'data', f'{group_id}.json')
if os.path.exists(current_dir):
file = open(current_dir, 'r', encoding = 'UTF-8')
data = json.load(file)
data['today_count']['morning'] = 0
data['today_count']['night'] = 0
with open(current_dir, "w", encoding="UTF-8") as f:
f.write(json.dumps(data, ensure_ascii=False, indent=4))
@sv.on_fullmatch('我的作息')
async def my_status(bot, ev):
user_id = ev.user_id
group_id = ev.group_id
current_dir = os.path.join(os.path.dirname(__file__), 'data', f'{group_id}.json')
file = open(current_dir, 'r', encoding = 'UTF-8')
data = json.load(file)
if str(user_id) in list(data.keys()):
get_up_time = data[str(user_id)]['get_up_time']
sleep_time = data[str(user_id)]['sleep_time']
morning_count = data[str(user_id)]['morning_count']
night_count = data[str(user_id)]['night_count']
msg = f'[CQ:at,qq={user_id}]您的作息数据如下:'
msg = msg + f'\n最近一次起床时间为{get_up_time}'
msg = msg + f'\n最近一次睡觉时间为{sleep_time}'
msg = msg + f'\n一共起床了{morning_count}次'
msg = msg + f'\n一共睡觉了{night_count}次'
else:
msg = '您还没有睡觉起床过呢!暂无数据'
await bot.send(ev, msg)
@sv.on_fullmatch('群友作息')
async def group_status(bot, ev):
group_id = ev.group_id
current_dir = os.path.join(os.path.dirname(__file__), 'data', f'{group_id}.json')
file = open(current_dir, 'r', encoding = 'UTF-8')
data = json.load(file)
moring_count = data['today_count']['morning']
night_count = data['today_count']['night']
msg = f'今天已经有{moring_count}位群友起床了,{night_count}群友睡觉了'
await bot.send(ev, msg)
# 配置管理
@sv.on_fullmatch('早安晚安配置')
async def config_settings(bot, ev):
msg = get_current_json()
await bot.send(ev, msg)
# morning
mor_switcher = {
'时限': 'get_up_intime',
'多重起床': 'multi_get_up',
'超级亢奋': 'super_get_up'
}
@sv.on_prefix('早安开启')
async def morning_enable(bot, ev):
if not priv.check_priv(ev, priv.SUPERUSER):
msg = '很抱歉您没有权限进行此操作,该操作仅限维护组'
await bot.send(ev, msg)
return
mor_server = ev.message.extract_plain_text()
if mor_server not in list(mor_switcher.keys()):
msg = f'在早安设置中未找到"{mor_server}",请确保输入正确,目前可选值有:' + str(list(mor_switcher.keys()))
await bot.send(ev, msg)
return
server = mor_switcher[mor_server]
msg = change_settings('morning', server, True)
await bot.send(ev, msg)
@sv.on_prefix('早安关闭')
async def morning_disable(bot, ev):
if not priv.check_priv(ev, priv.SUPERUSER):
msg = '很抱歉您没有权限进行此操作,该操作仅限维护组'
await bot.send(ev, msg)
return
mor_server = ev.message.extract_plain_text()
if mor_server not in list(mor_switcher.keys()):
msg = f'在早安设置中未找到"{mor_server}",请确保输入正确,目前可选值有:' + str(list(mor_switcher.keys()))
await bot.send(ev, msg)
return
server = mor_switcher[mor_server]
msg = change_settings('morning', server, False)
await bot.send(ev, msg)
@sv.on_prefix('早安设置')
async def morning_set(bot, ev):
if not priv.check_priv(ev, priv.SUPERUSER):
msg = '很抱歉您没有权限进行此操作,该操作仅限维护组'
await bot.send(ev, msg)
return
alltext = ev.message.extract_plain_text()
args = alltext.split(' ')
if args[0] not in list(mor_switcher.keys()):
msg = f'在早安设置中未找到"{args[0]}",请确保输入正确,目前可选值有:' + str(list(mor_switcher.keys()))
await bot.send(ev, msg)
return
server = mor_switcher[args[0]]
if server == 'get_up_intime':
try:
early_time = int(args[1])
late_time = int(args[2])
except:
msg = '获取参数错误,请确保你输入了正确的命令,样例参考:\n[早安设置 时限 1 18] 即1点到18点期间可以起床,数字会自动强制取整'
await bot.send(ev, msg)
return
if early_time < 0 or early_time > 24 or late_time < 0 or late_time > 24:
msg = '错误!您设置的时间未在0-24之间,要求:0 <= 时间 <= 24'
await bot.send(ev, msg)
return
msg = change_set_time('morning', server, early_time, late_time)
else:
try:
interval = int(args[1])
except:
msg = '获取参数错误,请确保你输入了正确的命令,样例参考:\n[早安设置 多重起床 6] 即最小间隔6小时,数字会自动强制取整'
await bot.send(ev, msg)
return
if interval < 0 or interval > 24:
msg = '错误!您设置的时间间隔未在0-24之间,要求:0 <= 时间 <= 24'
await bot.send(ev, msg)
return
msg = change_set_time('morning', server, interval)
await bot.send(ev, msg)
# night
nig_switcher = {
'时限': 'sleep_intime',
'多重睡觉': 'multi_sleep',
'超级睡眠': 'super_sleep'
}
@sv.on_prefix('晚安开启')
async def night_enable(bot, ev):
if not priv.check_priv(ev, priv.SUPERUSER):
msg = '很抱歉您没有权限进行此操作,该操作仅限维护组'
await bot.send(ev, msg)
return
nig_server = ev.message.extract_plain_text()
if nig_server not in list(nig_switcher.keys()):
msg = f'在晚安设置中未找到"{nig_server}",请确保输入正确,目前可选值有:' + str(list(nig_switcher.keys()))
await bot.send(ev, msg)
return
server = nig_switcher[nig_server]
msg = change_settings('night', server, True)
await bot.send(ev, msg)
@sv.on_prefix('晚安关闭')
async def night_disable(bot, ev):
if not priv.check_priv(ev, priv.SUPERUSER):
msg = '很抱歉您没有权限进行此操作,该操作仅限维护组'
await bot.send(ev, msg)
return
nig_server = ev.message.extract_plain_text()
if nig_server not in list(nig_switcher.keys()):
msg = f'在早安设置中未找到"{nig_server}",请确保输入正确,目前可选值有:' + str(list(nig_switcher.keys()))
await bot.send(ev, msg)
return
server = nig_switcher[nig_server]
msg = change_settings('night', server, False)
await bot.send(ev, msg)
@sv.on_prefix('晚安设置')
async def night_set(bot, ev):
if not priv.check_priv(ev, priv.SUPERUSER):
msg = '很抱歉您没有权限进行此操作,该操作仅限维护组'
await bot.send(ev, msg)
return
alltext = ev.message.extract_plain_text()
args = alltext.split(' ')
if args[0] not in list(nig_switcher.keys()):
msg = f'在早安设置中未找到"{args[0]}",请确保输入正确,目前可选值有:' + str(list(nig_switcher.keys()))
await bot.send(ev, msg)
return
server = nig_switcher[args[0]]
if server == 'sleep_intime':
try:
early_time = int(args[1])
late_time = int(args[2])
except:
msg = '获取参数错误,请确保你输入了正确的命令,样例参考:\n[晚安设置 时限 18 6] 即18点到第二天6点期间可以起床,数字会自动强制取整,注意第二个数字一定是第二天的时间'
await bot.send(ev, msg)
return
if early_time < 0 or early_time > 24 or late_time < 0 or late_time > 24:
msg = '错误!您设置的时间未在0-24之间,要求:0 <= 时间 <= 24'
await bot.send(ev, msg)
return
msg = change_set_time('night', server, early_time, late_time)
else:
try:
interval = int(args[1])
except:
msg = '获取参数错误,请确保你输入了正确的命令,样例参考:\n[晚安设置 多重睡觉 6] 即最小间隔6小时,数字会自动强制取整'
await bot.send(ev, msg)
return
if interval < 0 or interval > 24:
msg = '错误!您设置的时间间隔未在0-24之间,要求:0 <= 时间 <= 24'
await bot.send(ev, msg)
return
msg = change_set_time('night', server, interval)
await bot.send(ev, msg)