Nidhogg/valheim_bot/bot_commands.py
2021-06-27 19:09:02 +01:00

172 lines
5.1 KiB
Python

from nio import AsyncClient, MatrixRoom, RoomMessageText
from valheim_bot.chat_functions import react_to_event, send_text_to_room
from valheim_bot.config import Config
from valheim_bot.storage import Storage
from valheim_bot.valheim import (
get_latest_update,
getnews,
get_latest_dev_news
)
class Command:
_LATEST_AVAILABLE_CMD = [
"build",
"news",
"devnews"
]
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("get"):
await self._get()
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 == "get":
text = "<span>What do you want to get ?</span>"
text += self._get_get_help(prefix="latest")
elif topic == "commands":
text = "<span>Available commands:<span><ul>"
text += "<li>echo</li>"
text += "<li>react</li>"
text += "<li>get</li>"
text += "</ul>"
else:
text = "Unknown help topic!"
await send_text_to_room(self.client, self.room.room_id, text)
def _get_get_help(self, prefix=""):
text = "<ul>"
for cmd in self._LATEST_AVAILABLE_CMD:
text += f"<li>{prefix} {cmd}</li>"
text += "</ul>"
return 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.",
)
async def _get(self):
"""Show the get help text"""
if not self.args:
text = "<span>What do you want to get ?</span>"
text += self._get_get_help(prefix="latest")
await send_text_to_room(self.client, self.room.room_id, text)
return
if len(self.args) < 2:
if self.args[0] == "latest":
text = "<span>Latest what ?</span>"
text += self._get_get_help()
await send_text_to_room(self.client, self.room.room_id, text)
return
self._unknown_command()
data = f"{self.args[0]} {self.args[1]}"
if data == "latest build":
await send_text_to_room(
self.client,
self.room.room_id,
str(await get_latest_update())
)
elif data == "latest news":
news = await getnews(count=1, maxlength=140)
newsitem = news.appnews.newsitems[0]
await self._send_newsitem(newsitem)
elif data == "latest devnews":
news = await get_latest_dev_news()
await self._send_newsitem(news)
else:
self._unknown_command()
def FormatNews(newsitem):
text = (
f"<h1>{newsitem.title}</h1>"
f"<p>{newsitem.contents}</p>"
f"<a href={newsitem.url}>Read the rest of the article</a>"
)
return text
async def _send_newsitem(self, newsitem):
await send_text_to_room(
self.client,
self.room.room_id,
Command.FormatNews(newsitem)
)