diff --git a/glados.py b/glados.py index 7dd2e65..7e1dd56 100644 --- a/glados.py +++ b/glados.py @@ -7,6 +7,8 @@ from matrix_client.api import MatrixRequestError import os import sys +from plugins.plugin_manager import PluginManager + if len(sys.argv) > 1: config = __import__(sys.argv[1]) else: @@ -14,9 +16,11 @@ else: class Bot: def __init__(self): - self.client = MatrixClient(config.BOT_SERVER) + self.client = MatrixClient(config.BOT_SERVER, config.BOT_TOKEN) try: - self.client.login_with_password(config.BOT_USERNAME, config.BOT_PASSWORD) + if not config.BOT_TOKEN: + self.client.login(config.BOT_USERNAME, config.BOT_PASSWORD) + print(self.client.token) except MatrixRequestError as e: print(e) if e.code == 403: @@ -24,6 +28,9 @@ class Bot: sys.exit(1) self.plugin_manager = PluginManager(self, config) + for plugin in ['help', 'say', 'admin', 'free_games']: + self.plugin_manager.load_plugin(plugin) + self.rooms = {} for room_id, room_data in config.rooms.items(): self.rooms[room_id] = self.client.join_room(room_id) @@ -33,15 +40,16 @@ class Bot: self.client.start_listener_thread() #return client.sync_thread + print("Bot ready, listening...") while True: input() def callback(self, room, event): - if "@" + config.BOT_USERNAME in event['sender']: - return + if "@" + config.BOT_USERNAME in event['sender']: + return - if event['type'] == "m.room.message": - self.plugin_manager.notify_message(room, event) + if event['type'] == "m.room.message": + self.plugin_manager.notify_message(room, event) def main(): bot = Bot() diff --git a/plugins/__pycache__/__init__.cpython-310.pyc b/plugins/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..379fb86 Binary files /dev/null and b/plugins/__pycache__/__init__.cpython-310.pyc differ diff --git a/plugins/__pycache__/admin.cpython-310.pyc b/plugins/__pycache__/admin.cpython-310.pyc new file mode 100644 index 0000000..ef17975 Binary files /dev/null and b/plugins/__pycache__/admin.cpython-310.pyc differ diff --git a/plugins/__pycache__/free_games.cpython-310.pyc b/plugins/__pycache__/free_games.cpython-310.pyc new file mode 100644 index 0000000..37b159d Binary files /dev/null and b/plugins/__pycache__/free_games.cpython-310.pyc differ diff --git a/plugins/__pycache__/help.cpython-310.pyc b/plugins/__pycache__/help.cpython-310.pyc new file mode 100644 index 0000000..e22b881 Binary files /dev/null and b/plugins/__pycache__/help.cpython-310.pyc differ diff --git a/plugins/__pycache__/plugin.cpython-310.pyc b/plugins/__pycache__/plugin.cpython-310.pyc new file mode 100644 index 0000000..1f19e76 Binary files /dev/null and b/plugins/__pycache__/plugin.cpython-310.pyc differ diff --git a/plugins/__pycache__/plugin_manager.cpython-310.pyc b/plugins/__pycache__/plugin_manager.cpython-310.pyc new file mode 100644 index 0000000..7f10978 Binary files /dev/null and b/plugins/__pycache__/plugin_manager.cpython-310.pyc differ diff --git a/plugins/__pycache__/say.cpython-310.pyc b/plugins/__pycache__/say.cpython-310.pyc new file mode 100644 index 0000000..20873a4 Binary files /dev/null and b/plugins/__pycache__/say.cpython-310.pyc differ diff --git a/plugins/admin.py b/plugins/admin.py index 5438001..bdafd6a 100644 --- a/plugins/admin.py +++ b/plugins/admin.py @@ -12,12 +12,12 @@ class Admin(Plugin): super(Admin, self).deactivate() @Plugin.command(admin=True) - def admin_reload_plugins(self, msg, args): + def admin_reload_plugins(self, room, event, args): """Reload plugins""" self.plugin_manager.reload_plugins() return "Plugins reloaded" @Plugin.command(admin=True) - def admin_test(self, msg, args): + def admin_test(self, room, event, args): """Simple test""" return "You are admin" diff --git a/plugins/free_games.py b/plugins/free_games.py new file mode 100644 index 0000000..cdccc03 --- /dev/null +++ b/plugins/free_games.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +from plugins.plugin import Plugin + +from bs4 import BeautifulSoup +import requests + + +class FreeGames(Plugin): + def activate(self): + super(FreeGames, self).activate() + + def deactivate(self): + super(FreeGames, self).deactivate() + + @Plugin.command() + def epic(self, room, event, args): + """Display Epic free games => !epic""" + r=requests.get('https://feed.phenx.de/lootscraper_epic_game.xml') + soup = BeautifulSoup(r.content, 'xml') + c_list = soup.find_all('entry') + for c in c_list: + link = c.find('link').get('href') + title = c.find('title') + content = "{}\n".format(title.text) + content = "{}\n\n".format(link) + for li in c.find_all('li'): + content += "- {}\n".format(li.text) + room.send_text(content) + diff --git a/plugins/help.py b/plugins/help.py index a31f218..f98abe2 100644 --- a/plugins/help.py +++ b/plugins/help.py @@ -25,7 +25,7 @@ class Help(Plugin): reply = ['Usage:'] for command_name in command_names: command = self.plugin_manager.commands[command_name] - if not self.plugin_manager.is_admin_command(command_name) or self.plugin_manager.is_sender_admin(msg): + if not self.plugin_manager.is_admin_command(command_name) or self.plugin_manager.is_sender_admin(event): reply.append('{0}: {1}'.format(command_name, command['function'].__doc__ or 'N/A')) room.send_text("\n".join(reply)) return True diff --git a/plugins/plugin_manager.py b/plugins/plugin_manager.py index 7a8d6c9..0e9c7dd 100644 --- a/plugins/plugin_manager.py +++ b/plugins/plugin_manager.py @@ -86,16 +86,7 @@ class PluginManager: room.send_text(reply) def is_sender_admin(self, event): - return False - """Checks if author is admin""" - if message['type'] == 'groupchat': - # We don't know the jid. Either there's a problem or it's an anonymous room - if message['from'].full not in self.bot.presences: - return False - jid = self.bot.presences[message['from'].full] - else: - jid = message['from'].full.split('/')[0] - return jid in self.config.ADMINS + return event['sender'] in self.config.ADMINS def is_admin_command(self, command): return command in self.commands and self.commands[command]['options'].get('admin', False) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..74a30b0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +beautifulsoup4==4.12.3 +certifi==2024.8.30 +charset-normalizer==3.4.0 +idna==3.10 +lxml==5.3.0 +matrix-client==0.4.0 +requests==2.32.3 +soupsieve==2.6 +urllib3==1.26.20