You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
3.7 KiB
121 lines
3.7 KiB
from nio import AsyncClient, MatrixRoom, RoomMessageText
|
|
|
|
import datetime
|
|
import calendar
|
|
|
|
from molly.chat_functions import react_to_event, send_text_to_room
|
|
from molly.config import Config
|
|
from molly.storage import Storage
|
|
|
|
|
|
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()
|
|
elif self.command.startswith("react"):
|
|
await self._react()
|
|
elif self.command.startswith("help"):
|
|
await self._show_help()
|
|
elif self.command.startswith("!"):
|
|
await self._new_org_todo()
|
|
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 _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 = "Some text"
|
|
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 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 _new_org_todo(self):
|
|
"""Given a plaintext string, return org-formatted todo line."""
|
|
due_today = get_orgmode_deadline_string()
|
|
formatted_string = " ".join(self.args)
|
|
new_string = f"* TODO {formatted_string}"
|
|
await send_text_to_room(self.client, self.room.room_id, f"creating new todo with {new_string}")
|
|
with open(self.config.orgmode_refile_path, "a") as orgfile:
|
|
orgfile.write(f"{new_string}\n{due_today}\n")
|
|
|
|
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.",
|
|
)
|
|
|
|
|
|
def get_orgmode_deadline_string():
|
|
"""Return an orgmode formatted todo deadline string, for example:
|
|
DEADLINE: <2021-08-14 Sat>
|
|
"""
|
|
date = datetime.date.today().isoformat()
|
|
weekday = calendar.day_name[datetime.date.today().weekday()]
|
|
deadline_string = f"DEADLINE: <{date} {weekday[0:3]}>"
|
|
|
|
return deadline_string
|