68 lines
1.8 KiB
Python
68 lines
1.8 KiB
Python
from angel import AngelBot, RegexCmd
|
|
from configparser import ConfigParser
|
|
from PythonSed import Sed
|
|
import re
|
|
import io
|
|
|
|
sed_parse = re.compile("(?<!\\\\)[/#]")
|
|
sed_cmd = re.compile("^s[/#].*[/#].*[/#]")
|
|
|
|
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()
|
|
|
|
invidious_instances = config["angel"].get(
|
|
"invidious_instances", ""
|
|
).split()
|
|
|
|
bot = AngelBot(jid, password, nick=nick, autojoin=autojoin,
|
|
youtube_links=youtube_links,
|
|
invidious_instances=invidious_instances)
|
|
|
|
@RegexCmd(bot, sed_cmd)
|
|
def sed_command(bot, msg, sender, mtype):
|
|
"""Process sed command."""
|
|
try:
|
|
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,
|
|
)
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
# 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,
|
|
)
|
|
|
|
|
|
bot.connect()
|
|
bot.process(forever=True)
|