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

95 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
2022-07-29 10:55:20 +00:00
from core.builtins.message import MessageSession
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()
2022-07-29 10:55:20 +00:00
async def _(session: MessageSession):
2023-03-22 14:40:21 +00:00
# >>> ~test
# <<< Hello World!
2022-07-29 10:55:20 +00:00
await session.sendMessage('Hello World!')
2023-03-22 14:40:21 +00:00
@test.command('say <word>')
2022-07-29 10:55:20 +00:00
async def _(session: 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')
2022-07-29 10:55:20 +00:00
async def _(session: 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!
2022-07-29 10:55:20 +00:00
s = await session.waitReply('Send a word')
await s.sendMessage(s.asDisplay())
2023-04-14 10:47:13 +00:00
@test.command('confirm')
async def _(session: MessageSession):
# >>> ~test confirm
# <<< Are you sure?
# >>> Yes
# <<< OK!
s = await session.waitConfirm('Are you sure?')
if s:
await s.sendMessage('OK!')
2023-03-22 14:40:21 +00:00
@test.command('image')
2022-07-29 10:55:20 +00:00
async def _(session: MessageSession):
2023-03-22 14:40:21 +00:00
# >>> ~test image
# <<< A picture: Image(url='https://http.cat/100.jpg')
2022-07-29 10:55:20 +00:00
await session.sendMessage([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: MessageSession):
# >>> {{Hello World!}}
# <<< Hello World!
await session.finish(session.matched_msg.group(1))
@test.regex(re.compile(r'\[\[(.*)]]'), mode='A') # re.findall
async def _(session: MessageSession):
# >>> [[Hello]] [[World]]
# <<< Hello
# <<< World
2023-03-22 14:47:32 +00:00
await session.sendMessage(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: MessageSession):
# >>> ~test test
# <<< Hello World!
await session.finish('Hello World!')
@test.handle(re.compile(r'<(.*)>'), mode='A') # re.findall
async def _(session: MessageSession):
# >>> <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')