Initial commit

This commit is contained in:
2026-09-07 01:25:32 +03:00
commit a71d4ce5f8
14 changed files with 249 additions and 0 deletions

101
logic.py Normal file
View File

@@ -0,0 +1,101 @@
"""Bot's callbacks implementation"""
import asyncio
import glob
import html
import traceback
from mab import MatrixBot
from mab.types import VideoFileProperties
from mab.filters.text import TextFilter, TextStartsWithFilter, TextEndsWithFilter
from nio import MatrixRoom, RoomMessageText
#
# PRIVATE DATA
#
_bot: MatrixBot
#
# CALLBACKS
#
async def command_help(room: MatrixRoom, text: RoomMessageText) -> None:
"""!help"""
if text.sender == _bot.get_client().user_id:
return
resp = "<strong>2026-matrix-down</strong><br>"
resp += "Этот бот используется для скачивания видео/аудио с хостингов, "
resp += "поддерживаемых <code>yt-dlp</code>. Просто отправьте ссылку в эту "
resp += "комнату, чтобы бот предложил варианты загрузки."
await _bot.send_text_to_room(room.room_id, resp, True)
async def command_http_https(room: MatrixRoom, text: RoomMessageText) -> None:
"""The message starts with `http://` or `https://`"""
if text.sender == _bot.get_client().user_id:
return
resp = "<strong>Сообщение начинается с <code>http://</code> или с <code>"
resp += "https://</code></strong>. Ещё не реализовано."
await _bot.send_text_to_room(room.room_id, resp, True)
async def command_test(room: MatrixRoom, text: RoomMessageText) -> None:
"""The message ends with ` TesT` (case matters)"""
if text.sender == _bot.get_client().user_id:
return
# glob patterns to use
glob_img = ["*.png", "*.jpg", ".jpeg", "*.gif", "*.webp"]
glob_vid = ["*.mp4", "*.avi", "*.mkv"]
# TODO
glob_aud = ["*.mp3", "*.m4a", ".wav", "*.ogg", "*.flac"]
# help message
resp = "<strong>Сообщение заканчивается на <code> TesT</code>.</strong><br>"
resp += "Сейчас будут отправлены все файлы из директории <code>test_media"
resp += "</code>, у которых следующие расширения:<br>"
resp += f"<ul><li>Изображения: {", ".join([f"<code>{g}</code>" for g in glob_img])}</li></ul>"
resp += f"<ul><li>Видео: {", ".join([f"<code>{g}</code>" for g in glob_vid])}</li></ul>"
await _bot.send_text_to_room(room.room_id, resp, is_html=True)
await asyncio.sleep(1)
# send images
for g in [f"test_media/{g}" for g in glob_img]:
imgs = glob.glob(g)
for file in imgs:
try:
await _bot.send_image_to_room(
room.room_id,
file,
caption=f"Картинка <code>{html.escape(file)}</code>",
is_html=True
)
except:
await _bot.send_text_to_room(room.room_id, traceback.format_exc(), False)
await asyncio.sleep(0.5)
# send videos
for g in [f"test_media/{g}" for g in glob_vid]:
imgs = glob.glob(g)
for file in imgs:
try:
await _bot.send_video_to_room(
room.room_id,
file,
caption=f"Видео <code>{html.escape(file)}</code>",
is_html=True
)
except:
await _bot.send_text_to_room(room.room_id, traceback.format_exc(), False)
await asyncio.sleep(0.5)
#
# PUBLIC
#
async def initialize(bot: MatrixBot) -> None:
"""This function initializes the bot. It does not start it."""
global _bot
_bot = bot
bot.add_event_callback(command_http_https, TextStartsWithFilter(["https://", "http://"]))
bot.add_event_callback(command_test, TextEndsWithFilter(" TesT", any_case=False))
bot.add_event_callback(command_help, TextFilter())
async def deinitizalize(bot: MatrixBot) -> None:
"""This function deinitializes the bot. It does not stop it."""
pass