matrix-alertbot/bot_commands.py

66 lines
2.1 KiB
Python
Raw Normal View History

2019-09-25 14:26:29 +02:00
from chat_functions import send_text_to_room
class Command(object):
def __init__(self, client, store, config, command, room, event):
2019-09-25 14:26:29 +02:00
"""A command made by a user
Args:
client (nio.AsyncClient): The client to communicate to matrix with
store (Storage): Bot storage
config (Config): Bot configuration parameters
2019-09-25 14:26:29 +02:00
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
2019-10-26 01:40:05 +02:00
self.config = config
2019-09-25 14:26:29 +02:00
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.",
)