init
This commit is contained in:
120
my_project_name/bot_commands.py
Normal file
120
my_project_name/bot_commands.py
Normal file
@@ -0,0 +1,120 @@
|
||||
from nio import AsyncClient, MatrixRoom, RoomMessageText
|
||||
|
||||
from my_project_name.chat_functions import react_to_event, send_text_to_room
|
||||
from my_project_name.config import Config
|
||||
from my_project_name.storage import Storage
|
||||
from my_project_name.caldav_handler import CaldavHandler
|
||||
|
||||
|
||||
class Command:
|
||||
def __init__(
|
||||
self,
|
||||
client: AsyncClient,
|
||||
store: Storage,
|
||||
config: Config,
|
||||
command: str,
|
||||
room: MatrixRoom,
|
||||
event: RoomMessageText,
|
||||
):
|
||||
"""A command made by a user.
|
||||
|
||||
Args:
|
||||
client: The client to communicate to matrix with.
|
||||
|
||||
store: Bot storage.
|
||||
|
||||
config: Bot configuration parameters.
|
||||
|
||||
command: The command and arguments.
|
||||
|
||||
room: The room the command was sent in.
|
||||
|
||||
event: The event describing the command.
|
||||
"""
|
||||
self.client = client
|
||||
self.store = store
|
||||
self.config = config
|
||||
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()
|
||||
if self.command.startswith("react"):
|
||||
await self._react()
|
||||
elif self.command.startswith("help"):
|
||||
await self._show_help()
|
||||
elif self.command.startswith("today"):
|
||||
await self._show_today()
|
||||
elif self.command.startswith("week"):
|
||||
await self._show_week()
|
||||
elif self.command.startswith("month"):
|
||||
await self._show_month()
|
||||
#else:
|
||||
# await self._unknown_command()
|
||||
|
||||
async def _show_today(self):
|
||||
handler = CaldavHandler()
|
||||
response = handler.print_today()
|
||||
if len(response) == 0:
|
||||
response = "today is nothing planned yet. riot or read theory"
|
||||
await send_text_to_room(self.client, self.room.room_id, response)
|
||||
|
||||
async def _show_week(self):
|
||||
handler = CaldavHandler()
|
||||
response = handler.print_week()
|
||||
await send_text_to_room(self.client, self.room.room_id, response)
|
||||
|
||||
async def _show_month(self):
|
||||
handler = CaldavHandler()
|
||||
response = handler.print_month()
|
||||
await send_text_to_room(self.client, self.room.room_id, response, markdown_convert=True)
|
||||
|
||||
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 _react(self):
|
||||
"""Make the bot react to the command message"""
|
||||
# React with a start emoji
|
||||
reaction = "⭐"
|
||||
await react_to_event(
|
||||
self.client, self.room.room_id, self.event.event_id, reaction
|
||||
)
|
||||
|
||||
# React with some generic text
|
||||
reaction = "(A)"
|
||||
await react_to_event(
|
||||
self.client, self.room.room_id, self.event.event_id, reaction
|
||||
)
|
||||
|
||||
async def _show_help(self):
|
||||
"""Show the help text"""
|
||||
if not self.args:
|
||||
text = (
|
||||
"Hello, I am kallauser MC's (A)wesome calendar bot <3! Use `help commands` to view "
|
||||
"available commands.\n"
|
||||
"Use `help rules` to view the rules"
|
||||
)
|
||||
await send_text_to_room(self.client, self.room.room_id, text)
|
||||
return
|
||||
|
||||
topic = self.args[0]
|
||||
if topic == "rules":
|
||||
text = "be nice to each other."
|
||||
elif topic == "commands":
|
||||
text = "Available commands: today, week, month"
|
||||
else:
|
||||
text = "I dont know what you are talking about.."
|
||||
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.",
|
||||
)
|
||||
Reference in New Issue
Block a user