Skip to content

Commit aaa7961

Browse files
committed
upd: zhihu plugin
1 parent bab20d5 commit aaa7961

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

anon/plugin/plugin.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ def prevent_after(self, event: Event) -> bool:
8080
return (isinstance(event, MessageEvent)
8181
and self.match_cmd(event.msg.text_only))
8282

83-
async def on_cmd(self, event: MessageEvent, args: list):
83+
async def on_cmd(self, event: MessageEvent, args: list) -> bool:
84+
"""
85+
若出现异常,应返回 True,此时会自动回复插件用法
86+
"""
8487
pass
8588

8689
async def on_event(self, event: Event):
@@ -114,11 +117,16 @@ def __init__(self, *args):
114117
self.register_plugin(plugin)
115118
logger.info('PluginManager initialized.')
116119

120+
@staticmethod
121+
async def cmd_wrapper(plugin: Plugin, event: MessageEvent):
122+
if await plugin.on_cmd(event, event.msg.text_only.split()):
123+
await event.reply(plugin.usage, quote=False)
124+
117125
def broad_cast(self, event: Event):
118126
for plugin in self.plugins:
119127
if not plugin.default_filter(event) and not plugin.event_filter(event):
120128
if isinstance(event, MessageEvent) and plugin.match_cmd(event.msg.text_only):
121-
task = asyncio.create_task(plugin.on_cmd(event, event.msg.text_only.split()))
129+
task = asyncio.create_task(self.cmd_wrapper(plugin, event))
122130
else:
123131
task = asyncio.create_task(plugin.on_event(event))
124132
self.tasks.add(task)

docs/cmd_manager.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ async def date(event: MessageEvent, args):
2323

2424
1. 在匹配到对应命令后,`PluginManager` 会阻止消息向后优先级的插件传递,意味着每条匹配的消息最多只会有一个处理函数被调用。
2525
2. 这意味着重名命令,总会用最先注册的插件处理。
26-
3. 如果你仍想让消息继续向下传递,则你应手动创建插件,例如:
26+
3. on_cmd 若返回 True 值,则会自动回复此插件的 Usage,这在错误处理中应该有用。
27+
4. 如果你仍想让消息继续向下传递,则你应手动创建插件,例如:
2728

2829
```python
2930
from anon.event import MessageEvent, Event

plugins/example/zhihu.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ async def get_zhihu_hot_list(date_str):
4848

4949
class ZhihuPlugin(Plugin):
5050
bot: Bot
51-
cron = '10 8-22/2 * * *'
51+
cron = '10 8-20/4 * * *'
5252
group: int
5353
storage: Storage
54+
brif = '定时知乎热榜'
55+
usage = 'Usage: zhihu set/get <num>\n设置知乎热榜 topn'
56+
keywords = ['zhihu']
5457

5558
async def on_load(self):
5659
self.bot = Bot()
@@ -71,7 +74,7 @@ async def on_cron(self):
7174
hot_list = await get_zhihu_hot_list(date_str)
7275
top_n = self.storage['topn']
7376
if hot_list:
74-
# 将热榜切分为每10个一组,应对手机QQ一条消息超过10个就无法渲染超链接的bug
77+
# 将热榜切分为每 10 个一组,应对手机 QQ 一条消息超过 10 个就无法渲染超链接的 bug
7578
chunk_size = 10
7679
for i in range(0, len(hot_list[:top_n]), chunk_size):
7780
logger.info(f'第{i + 1}组, chunk_size={chunk_size}, len(hot_list[:top_n])={len(hot_list[:top_n])}')
@@ -84,10 +87,19 @@ async def on_cron(self):
8487
except Exception as e:
8588
logger.error(f"发送消息时出错: {e}")
8689

87-
async def on_event(self, event: MessageEvent):
88-
if event.raw.startswith('s/zhihu'):
89-
self.storage['topn'] = int(event.raw[7:].strip())
90-
await event.reply(f'Zhihu topn set to: {self.storage["topn"]}')
90+
async def on_cmd(self, event: MessageEvent, args) -> bool:
91+
if len(args) < 2:
92+
return True
93+
94+
if args[1] == 'get':
95+
await event.reply(f'当前 topn 设置为: {self.storage["topn"]}')
96+
else:
97+
try:
98+
n = int(args[2])
99+
self.storage['topn'] = n
100+
await event.reply(f'知乎 topn 设置为: {n}')
101+
except:
102+
return True
91103

92104

93105
# 注册插件

0 commit comments

Comments
 (0)