This commit is contained in:
groug 2025-04-23 08:10:40 +00:00
parent b774e0d04c
commit ad4ec4d3a9
13 changed files with 57 additions and 19 deletions

View File

@ -7,6 +7,8 @@ from matrix_client.api import MatrixRequestError
import os import os
import sys import sys
from plugins.plugin_manager import PluginManager
if len(sys.argv) > 1: if len(sys.argv) > 1:
config = __import__(sys.argv[1]) config = __import__(sys.argv[1])
else: else:
@ -14,9 +16,11 @@ else:
class Bot: class Bot:
def __init__(self): def __init__(self):
self.client = MatrixClient(config.BOT_SERVER) self.client = MatrixClient(config.BOT_SERVER, config.BOT_TOKEN)
try: 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: except MatrixRequestError as e:
print(e) print(e)
if e.code == 403: if e.code == 403:
@ -24,6 +28,9 @@ class Bot:
sys.exit(1) sys.exit(1)
self.plugin_manager = PluginManager(self, config) self.plugin_manager = PluginManager(self, config)
for plugin in ['help', 'say', 'admin', 'free_games']:
self.plugin_manager.load_plugin(plugin)
self.rooms = {} self.rooms = {}
for room_id, room_data in config.rooms.items(): for room_id, room_data in config.rooms.items():
self.rooms[room_id] = self.client.join_room(room_id) self.rooms[room_id] = self.client.join_room(room_id)
@ -33,15 +40,16 @@ class Bot:
self.client.start_listener_thread() self.client.start_listener_thread()
#return client.sync_thread #return client.sync_thread
print("Bot ready, listening...")
while True: while True:
input() input()
def callback(self, room, event): def callback(self, room, event):
if "@" + config.BOT_USERNAME in event['sender']: if "@" + config.BOT_USERNAME in event['sender']:
return return
if event['type'] == "m.room.message": if event['type'] == "m.room.message":
self.plugin_manager.notify_message(room, event) self.plugin_manager.notify_message(room, event)
def main(): def main():
bot = Bot() bot = Bot()

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -12,12 +12,12 @@ class Admin(Plugin):
super(Admin, self).deactivate() super(Admin, self).deactivate()
@Plugin.command(admin=True) @Plugin.command(admin=True)
def admin_reload_plugins(self, msg, args): def admin_reload_plugins(self, room, event, args):
"""Reload plugins""" """Reload plugins"""
self.plugin_manager.reload_plugins() self.plugin_manager.reload_plugins()
return "Plugins reloaded" return "Plugins reloaded"
@Plugin.command(admin=True) @Plugin.command(admin=True)
def admin_test(self, msg, args): def admin_test(self, room, event, args):
"""Simple test""" """Simple test"""
return "You are admin" return "You are admin"

30
plugins/free_games.py Normal file
View File

@ -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)

View File

@ -25,7 +25,7 @@ class Help(Plugin):
reply = ['Usage:'] reply = ['Usage:']
for command_name in command_names: for command_name in command_names:
command = self.plugin_manager.commands[command_name] 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')) reply.append('{0}: {1}'.format(command_name, command['function'].__doc__ or 'N/A'))
room.send_text("\n".join(reply)) room.send_text("\n".join(reply))
return True return True

View File

@ -86,16 +86,7 @@ class PluginManager:
room.send_text(reply) room.send_text(reply)
def is_sender_admin(self, event): def is_sender_admin(self, event):
return False return event['sender'] in self.config.ADMINS
"""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
def is_admin_command(self, command): def is_admin_command(self, command):
return command in self.commands and self.commands[command]['options'].get('admin', False) return command in self.commands and self.commands[command]['options'].get('admin', False)

9
requirements.txt Normal file
View File

@ -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