|
1 | 1 | import random
|
2 |
| -import json |
3 | 2 | import os
|
| 3 | +from pathlib import Path |
4 | 4 | import re
|
5 | 5 | import nonebot
|
6 |
| -from nonebot.typing import T_State |
7 |
| -from nonebot.params import State |
8 |
| -from nonebot import on_regex |
| 6 | +from nonebot import on_command, on_regex |
9 | 7 | from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, GROUP
|
| 8 | +try: |
| 9 | + import ujson as json |
| 10 | +except ModuleNotFoundError: |
| 11 | + import json |
10 | 12 |
|
11 |
| -_CRAZY_PATH = nonebot.get_driver().config.crazy_path |
12 |
| -CRAZY_PATH = os.path.join(os.path.dirname(__file__), "resource") if not _CRAZY_PATH else _CRAZY_PATH |
| 13 | +global_config = nonebot.get_driver().config |
| 14 | +if not hasattr(global_config, "crazy_path"): |
| 15 | + CRAZY_PATH = os.path.join(os.path.dirname(__file__), "resource") |
| 16 | +else: |
| 17 | + CRAZY_PATH = global_config.crazy_path |
13 | 18 |
|
14 |
| -# V我50 |
| 19 | +__crazy_vsrsion__ = "v0.2.1" |
| 20 | +plugin_notes = f''' |
| 21 | +KFC疯狂星期四 {__crazy_vsrsion__} |
| 22 | +[疯狂星期X] 随机输出KFC疯狂星期四文案 |
| 23 | +[狂乱X曜日] 随机输出KFC疯狂星期四文案'''.strip() |
| 24 | + |
| 25 | +plugin_help = on_command("疯狂星期四帮助", permission=GROUP, priority=15, block=True) |
15 | 26 | crazy = on_regex(r'疯狂星期.', permission=GROUP, priority=15, block=True)
|
| 27 | +crazy_jp = on_regex(r'狂乱.曜日', permission=GROUP, priority=15, block=True) |
| 28 | + |
| 29 | +@plugin_help.handle() |
| 30 | +async def _(bot: Bot, event: GroupMessageEvent): |
| 31 | + await plugin_help.finish(plugin_notes) |
16 | 32 |
|
17 | 33 | @crazy.handle()
|
18 |
| -async def _(bot: Bot, event: GroupMessageEvent, state: T_State=State()): |
19 |
| - iscrazy = re.search(r'(.*?)星期[一|二|三|四|五|六|日]', event.get_plaintext()) |
20 |
| - crazy_day = iscrazy.group(0)[-3:] if iscrazy is not None else None |
| 34 | +async def _(bot: Bot, event: GroupMessageEvent): |
| 35 | + iscrazy = re.search(r'疯狂星期[一|二|三|四|五|六|日]', event.get_plaintext()) |
| 36 | + crazy_day = iscrazy.group(0)[-1] if iscrazy is not None else None |
21 | 37 | if crazy_day is None:
|
22 | 38 | await crazy.finish("给个准确时间,OK?")
|
23 | 39 |
|
24 | 40 | # json数据存放路径
|
25 |
| - filePath = os.path.join(CRAZY_PATH, "post.json") |
| 41 | + filePath = Path(CRAZY_PATH) / "post.json" |
26 | 42 | # 将json对象加载到数组
|
27 |
| - kfc = json.load(open(filePath, 'r', encoding="UTF-8")).get('post') |
| 43 | + with open(filePath, "r", encoding="utf-8") as f: |
| 44 | + kfc = json.load(f).get("post") |
28 | 45 | # 随机选取数组中的一个对象
|
29 |
| - randomPost = random.choice(kfc) |
30 |
| - randomPost = randomPost.replace("星期四", crazy_day) |
31 |
| - await bot.send(event=event, message=randomPost) |
| 46 | + randomPost = random.choice(kfc).replace("星期四", "星期" + crazy_day).replace("周四", "周" + crazy_day) |
| 47 | + await crazy.finish(randomPost) |
| 48 | + |
| 49 | +@crazy_jp.handle() |
| 50 | +async def _(bot: Bot, event: GroupMessageEvent): |
| 51 | + iscrazy = re.search(r'狂乱[月|火|水|木|金|土|日]曜日', event.get_plaintext()) |
| 52 | + crazy_day = iscrazy.group(0)[-3] if iscrazy is not None else None |
| 53 | + |
| 54 | + if crazy_day is None: |
| 55 | + await crazy_jp.finish("给个准确时间,OK?") |
| 56 | + |
| 57 | + filePath = Path(CRAZY_PATH) / "post.json" |
| 58 | + with open(filePath, "r", encoding="utf-8") as f: |
| 59 | + kfc = json.load(f).get("post") |
| 60 | + |
| 61 | + randomPost = random.choice(kfc).replace("星期四", "星期" + weekday_table[crazy_day]).replace("周四", "周" + weekday_table[crazy_day]) |
| 62 | + await crazy_jp.finish(randomPost) |
| 63 | + |
| 64 | +weekday_table = { |
| 65 | + "月": "一", |
| 66 | + "火": "二", |
| 67 | + "水": "三", |
| 68 | + "木": "四", |
| 69 | + "金": "五", |
| 70 | + "土": "六", |
| 71 | + "日": "日" |
| 72 | +} |
0 commit comments