-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample_plugin.py
More file actions
131 lines (93 loc) · 3.17 KB
/
example_plugin.py
File metadata and controls
131 lines (93 loc) · 3.17 KB
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
from arclet.entari import (
Session,
MessageChain,
MessageCreatedEvent,
Plugin,
command,
filter_,
metadata,
keeping,
scheduler,
local_data,
Entari,
)
from arclet.entari.event.command import CommandOutput
from arclet.entari.filter import Interval
metadata(__file__)
plug = Plugin.current()
@plug.use("::startup")
async def prepare(app: Entari):
await app.cache.set("foo", ["something will expire in 5m"])
print(">> example: Preparing")
@plug.use("::cleanup")
async def cleanup():
print(">> example: Cleanup")
@plug.dispatch(MessageCreatedEvent)
@filter_.public
async def _(session: Session):
if session.content == "test":
resp = await session.send("This message will recall in 5s...", at_sender=True)
@scheduler.invoke(5)
async def _():
await session.message_delete(resp[0].id)
disp_message = plug.dispatch(MessageCreatedEvent)
@filter_.public.derive(filter_.to_me).derive
async def filter_content(session: Session):
return str(session.content) == "aaa"
@disp_message.on().if_(filter_content)
async def _(session: Session):
return await session.send("Filter: public message, to me, and content is 'aaa'")
@disp_message
@filter_.public & filter_.to_me & filter_(lambda sess: str(sess.content) != "aaa")
async def _(session: Session):
return await session.send("Filter: public message, to me, but content is not 'aaa'")
@command.on("add {a} {b}")
def add(a: int, b: int):
return f"{a + b =}"
add.propagate(Interval(2, limit_prompt="太快了"))
kept_data = keeping("foo", [], dispose=lambda x: x.clear())
@command.on("append {data}")
async def append(app: Entari, data: str):
kept_data.append(data)
if foo := await app.cache.get("foo"):
kept_data.extend(foo)
return f"Appended {data}"
@command.on("show")
async def show(session: Session):
res = await command.execute("echo 123")
await session.send_message(f"Execute `echo 123` Result: {res}")
return f"Data: {kept_data}"
TEST = 7
print([*Plugin.current()._scope.subscribers])
print(Plugin.current().subplugins)
print(local_data.get_temp_dir())
print(plug.config)
#
# @plug.use("::before_send")
# async def send_hook1(event):
# event.message += "的说"
#
#
# @plug.use("::before_send")
# async def send_hook2(message: MessageChain):
# # return message + "喵"
# message += "喵"
@plug.use("::config_reload")
async def config_reload():
print(">> Config Reloaded")
@plug.use("::plugin/loaded_success")
async def loaded_success(event):
print(f">> Plugin {event.plugin_id} Loaded Successfully")
@plug.use("::plugin/unloaded")
async def unloaded(event):
print(f">> Plugin {event.plugin_id} Unloaded")
@plug.dispatch(CommandOutput)
async def output_hook(event: CommandOutput):
content = event.content
return f"{event.type.title()}:\n{content}"
# @scheduler.cron("* * * * *")
# async def broadcast(app: Entari):
# for account in app.accounts.values():
# channels = [channel for guild in (await account.guild_list()).data for channel in (await account.channel_list(guild.id)).data]
# for channel in channels:
# await account.send_message(channel, "Hello, World!")