angel/main.py

68 lines
1.8 KiB
Python
Raw Normal View History

2025-04-07 15:57:52 -03:00
from angel import AngelBot, RegexCmd
from configparser import ConfigParser
from PythonSed import Sed
2024-02-16 01:56:08 +01:00
import re
import io
sed_parse = re.compile("(?<!\\\\)[/#]")
sed_cmd = re.compile("^s[/#].*[/#].*[/#]")
2025-04-07 15:57:52 -03:00
config = ConfigParser()
config.read("config.ini")
jid = config["angel"]["jid"]
password = config["angel"]["password"]
autojoin = config["angel"].get("autojoin", "").split()
nick = config["angel"]["nick"]
youtube_links = config["angel"].get("youtube_links", "").split()
2024-02-16 01:56:08 +01:00
2025-04-07 15:57:52 -03:00
invidious_instances = config["angel"].get(
"invidious_instances", ""
).split()
2024-02-16 01:56:08 +01:00
2025-04-07 15:57:52 -03:00
bot = AngelBot(jid, password, nick=nick, autojoin=autojoin,
youtube_links=youtube_links,
invidious_instances=invidious_instances)
2024-02-16 01:56:08 +01:00
2025-04-07 15:57:52 -03:00
@RegexCmd(bot, sed_cmd)
def sed_command(bot, msg, sender, mtype):
"""Process sed command."""
2024-02-16 01:56:08 +01:00
try:
2025-04-07 15:57:52 -03:00
text = msg["body"]
if not sed_cmd.match(text):
bot.messages[sender]["messages"].add(text)
return
sed_args = sed_parse.split(text)
if len(sed_args) < 4:
return
sed = Sed()
sed.load_string(text)
for message in bot.messages[sender]["messages"]:
if not re.search(sed_args[1], message):
continue
msg = io.StringIO(message)
res = "\n".join(sed.apply(msg, None))
bot.messages[sender]["messages"].add(res)
return bot.send_message(
mto=sender,
mbody=res,
mtype=mtype,
)
2024-02-16 01:56:08 +01:00
except Exception as e:
print(e)
2025-04-07 15:57:52 -03:00
# ping command
@RegexCmd(bot, re.compile(r"^ping$"))
def ping_command(bot, msg, sender, mtype):
"""Process ping command."""
bot.send_message(
mto=sender,
mbody="pong",
mtype=mtype,
2024-02-16 01:56:08 +01:00
)
2025-04-07 15:57:52 -03:00
bot.connect()
bot.process(forever=True)