This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
generated from anoadragon453/nio-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_commands.py
62 lines (50 loc) · 1.99 KB
/
bot_commands.py
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
from chat_functions import send_text_to_room
class Command(object):
def __init__(self, client, store, command, room, event):
"""A command made by a user
Args:
client (nio.AsyncClient): The client to communicate to matrix with
store (Storage): Bot storage
command (str): The command and arguments
room (nio.rooms.MatrixRoom): The room the command was sent in
event (nio.events.room_events.RoomMessageText): The event describing the command
"""
self.client = client
self.store = store
self.command = command
self.room = room
self.event = event
self.args = self.command.split()[1:]
async def process(self):
"""Process the command"""
if self.command.startswith("echo"):
await self._echo()
elif self.command.startswith("help"):
await self._show_help()
else:
await self._unknown_command()
async def _echo(self):
"""Echo back the command's arguments"""
response = " ".join(self.args)
await send_text_to_room(self.client, self.room.room_id, response)
async def _show_help(self):
"""Show the help text"""
if not self.args:
text = ("Hello, I am a bot made with matrix-nio! Use `help commands` to view "
"available commands.")
await send_text_to_room(self.client, self.room.room_id, text)
return
topic = self.args[0]
if topic == "rules":
text = "These are the rules!"
elif topic == "commands":
text = "Available commands"
else:
text = "Unknown help topic!"
await send_text_to_room(self.client, self.room.room_id, text)
async def _unknown_command(self):
await send_text_to_room(
self.client,
self.room.room_id,
f"Unknown command '{self.command}'. Try the 'help' command for more information.",
)