Implment new commands
This commit is contained in:
parent
187e150c85
commit
04f2372ac3
4 changed files with 91 additions and 13 deletions
|
@ -2,7 +2,7 @@ import logging
|
|||
|
||||
from nio import AsyncClient, MatrixRoom, RoomMessageText
|
||||
|
||||
from nyx_bot.chat_functions import send_quote_image, send_text_to_room
|
||||
from nyx_bot.chat_functions import send_quote_image, send_text_to_room, send_user_image
|
||||
from nyx_bot.config import Config
|
||||
from nyx_bot.storage import Storage
|
||||
|
||||
|
@ -46,23 +46,21 @@ class Command:
|
|||
|
||||
async def process(self):
|
||||
"""Process the command"""
|
||||
if self.command.startswith("echo"):
|
||||
await self._echo()
|
||||
elif self.command.startswith("quote"):
|
||||
if self.command.startswith("quote"):
|
||||
await self._quote()
|
||||
elif self.command.startswith("send_avatar"):
|
||||
await self._send_avatar()
|
||||
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 _quote(self):
|
||||
await send_quote_image(self.client, self.room, self.event, self.reply_to)
|
||||
|
||||
async def _send_avatar(self):
|
||||
await send_user_image(self.client, self.room, self.event, self.reply_to)
|
||||
|
||||
async def _show_help(self):
|
||||
"""Show the help text"""
|
||||
if not self.args:
|
||||
|
@ -74,10 +72,8 @@ class Command:
|
|||
return
|
||||
|
||||
topic = self.args[0]
|
||||
if topic == "rules":
|
||||
text = "These are the rules!"
|
||||
elif topic == "commands":
|
||||
text = "Available commands: ..."
|
||||
if topic == "commands":
|
||||
text = "Available commands:\n\n* `quote`: Make a new quote image. This command must be used on a reply.\n* `send_avatar`: Send the avatar of the person being replied to. This command must be used on a reply."
|
||||
else:
|
||||
text = "Unknown help topic!"
|
||||
await send_text_to_room(self.client, self.room.room_id, text)
|
||||
|
|
|
@ -3,6 +3,7 @@ from io import BytesIO
|
|||
from typing import Optional, Union
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import magic
|
||||
from markdown import markdown
|
||||
from nio import (
|
||||
AsyncClient,
|
||||
|
@ -310,3 +311,81 @@ async def send_sticker_image(
|
|||
print("Image was sent successfully")
|
||||
except Exception:
|
||||
print(f"Image send of file {image} failed.")
|
||||
|
||||
|
||||
async def send_user_image(
|
||||
client: AsyncClient,
|
||||
room: MatrixRoom,
|
||||
event: RoomMessageText,
|
||||
reply_to: str,
|
||||
):
|
||||
"""Send a user's avatar to a room.
|
||||
|
||||
Arguments:
|
||||
---------
|
||||
client : Client
|
||||
room_id : str
|
||||
image : Image
|
||||
|
||||
"""
|
||||
if not reply_to:
|
||||
await send_text_to_room(
|
||||
client,
|
||||
room.room_id,
|
||||
"This command requires replying to a message.",
|
||||
True,
|
||||
False,
|
||||
event.event_id,
|
||||
True,
|
||||
)
|
||||
return
|
||||
target_response = await client.room_get_event(room.room_id, reply_to)
|
||||
target_event = target_response.event
|
||||
sender = target_event.sender
|
||||
sender_name = room.user_name(sender)
|
||||
sender_avatar = room.avatar_url(sender)
|
||||
image = None
|
||||
length = 0
|
||||
mimetype = None
|
||||
if sender_avatar:
|
||||
url = urlparse(sender_avatar)
|
||||
server_name = url.netloc
|
||||
media_id = url.path.replace("/", "")
|
||||
avatar_resp = await client.download(server_name, media_id)
|
||||
data = avatar_resp.body
|
||||
mimetype = magic.from_buffer(data, mime=True)
|
||||
bytesio = BytesIO(data)
|
||||
length = bytesio.getbuffer().nbytes
|
||||
image = Image(file=bytesio)
|
||||
|
||||
(width, height) = (image.width, image.height)
|
||||
|
||||
content = {
|
||||
"body": f"[Avatar of {sender_name}]",
|
||||
"info": {
|
||||
"size": length,
|
||||
"mimetype": mimetype,
|
||||
"thumbnail_info": {
|
||||
"mimetype": mimetype,
|
||||
"size": length,
|
||||
"w": width, # width in pixel
|
||||
"h": height, # height in pixel
|
||||
},
|
||||
"w": width, # width in pixel
|
||||
"h": height, # height in pixel
|
||||
"thumbnail_url": sender_avatar,
|
||||
},
|
||||
"msgtype": "m.image",
|
||||
"url": sender_avatar,
|
||||
}
|
||||
|
||||
if reply_to:
|
||||
content["m.relates_to"] = {"m.in_reply_to": {"event_id": reply_to}}
|
||||
|
||||
try:
|
||||
await client.room_send(
|
||||
room.room_id, message_type="m.room.message", content=content
|
||||
)
|
||||
print("Image was sent successfully")
|
||||
except Exception:
|
||||
print(f"Image send of file {image} failed.")
|
||||
|
|
|
@ -3,6 +3,7 @@ import os.path
|
|||
from asyncio import create_subprocess_exec
|
||||
from asyncio.subprocess import PIPE
|
||||
from html import escape
|
||||
from os import remove
|
||||
from tempfile import mkstemp
|
||||
|
||||
from wand.drawing import Drawing
|
||||
|
@ -68,6 +69,7 @@ async def make_quote_image(sender: str, text: str, avatar: Image):
|
|||
draw.composite("src_over", text_x, text_y, text_width, text_height, image)
|
||||
ret = Image(width=int(width), height=int(height))
|
||||
draw(ret)
|
||||
remove(imagefile)
|
||||
return ret
|
||||
|
||||
|
||||
|
|
1
setup.py
1
setup.py
|
@ -35,6 +35,7 @@ setup(
|
|||
"Markdown>=3.1.1",
|
||||
"PyYAML>=5.1.2",
|
||||
"Wand",
|
||||
"python-magic",
|
||||
],
|
||||
extras_require={
|
||||
"postgres": ["psycopg2>=2.8.5"],
|
||||
|
|
Loading…
Reference in a new issue