Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/example/new_module/__init__.py

92 lines
2.5 KiB
Python
Raw Normal View History

2023-04-19 07:41:39 +00:00
import re
2023-03-22 14:40:21 +00:00
from core.builtins import Plain, Image, Bot
2023-03-04 08:51:56 +00:00
from core.component import module
2023-03-22 14:40:21 +00:00
from core.scheduler import IntervalTrigger
2023-03-04 08:51:56 +00:00
test = module('test')
2022-07-29 10:55:20 +00:00
2023-03-22 14:40:21 +00:00
@test.command()
async def _(session: Bot.MessageSession):
2023-03-22 14:40:21 +00:00
# >>> ~test
# <<< Hello World!
2023-09-01 14:38:32 +00:00
await session.send_message('Hello World!')
2022-07-29 10:55:20 +00:00
2023-03-22 14:40:21 +00:00
@test.command('say <word>')
async def _(session: Bot.MessageSession):
2023-03-22 14:40:21 +00:00
# >>> ~test say Hello World!
# <<< Hello World!
2022-07-29 10:55:20 +00:00
await session.finish(session.parsed_msg['<word>'])
2023-03-22 14:40:21 +00:00
@test.command('reply')
async def _(session: Bot.MessageSession):
2023-03-22 14:40:21 +00:00
# >>> ~test reply
2023-04-14 10:47:13 +00:00
# <<< Reply me!
# >>> Hello World! >> [Reply me!]
2023-03-22 14:40:21 +00:00
# <<< Hello World!
2023-09-01 14:38:32 +00:00
s = await session.wait_reply('Send a word')
await s.send_message(s.as_display())
2022-07-29 10:55:20 +00:00
2023-04-14 10:47:13 +00:00
@test.command('confirm')
async def _(session: Bot.MessageSession):
2023-04-14 10:47:13 +00:00
# >>> ~test confirm
# <<< Are you sure?
# >>> Yes
# <<< OK!
2023-09-01 14:38:32 +00:00
s = await session.wait_confirm('Are you sure?')
2023-04-14 10:47:13 +00:00
if s:
2023-09-01 14:38:32 +00:00
await s.send_message('OK!')
2023-04-14 10:47:13 +00:00
2023-03-22 14:40:21 +00:00
@test.command('image')
async def _(session: Bot.MessageSession):
2023-03-22 14:40:21 +00:00
# >>> ~test image
# <<< A picture: Image(url='https://http.cat/100.jpg')
2023-09-01 14:38:32 +00:00
await session.send_message([Plain('A picture:'), Image('https://http.cat/100.jpg')])
2023-03-22 14:40:21 +00:00
@test.regex(re.compile(r'\{\{(.*)}}'), mode='M') # re.match
async def _(session: Bot.MessageSession):
2023-03-22 14:40:21 +00:00
# >>> {{Hello World!}}
# <<< Hello World!
await session.finish(session.matched_msg.group(1))
@test.regex(re.compile(r'\[\[(.*)]]'), mode='A') # re.findall
async def _(session: Bot.MessageSession):
2023-03-22 14:40:21 +00:00
# >>> [[Hello]] [[World]]
# <<< Hello
# <<< World
2023-09-01 14:38:32 +00:00
await session.send_message(session.matched_msg[0])
2023-03-22 14:40:21 +00:00
await session.finish(session.matched_msg[1])
@test.schedule(IntervalTrigger(seconds=30))
async def _():
2023-03-22 14:44:26 +00:00
# Send a message to target which is enabled test module every 30 seconds
2023-03-22 14:40:21 +00:00
await Bot.FetchTarget.post_message('test', 'Hello World!')
2023-03-22 14:44:26 +00:00
@test.handle('test') # all in one handler, including command, regex and schedule
async def _(session: Bot.MessageSession):
2023-03-22 14:44:26 +00:00
# >>> ~test test
# <<< Hello World!
await session.finish('Hello World!')
@test.handle(re.compile(r'<(.*)>'), mode='A') # re.findall
async def _(session: Bot.MessageSession):
2023-03-22 14:44:26 +00:00
# >>> <Hello World!>
# <<< Hello World!
await session.finish(session.matched_msg[0])
@test.handle(IntervalTrigger(seconds=60))
async def _():
# Send a message to target which is enabled test module every 60 seconds
await Bot.FetchTarget.post_message('test', 'test')