more modules work

This commit is contained in:
2021-04-09 22:18:52 +02:00
parent 2ae965df4a
commit ed3a3c5595
4 changed files with 48 additions and 31 deletions

View File

@@ -14,3 +14,6 @@ root_channel_name = Random Channel
; Interval in sec between channel checks ; Interval in sec between channel checks
check_timer_interval = 5 check_timer_interval = 5
; Additional game channels specified with steam AppIDs, comma seperated
game_channels_permament =

View File

@@ -5,16 +5,17 @@ from threading import Timer
import requests import requests
from mumo_module import (commaSeperatedIntegers, MumoModule) from mumo_module import (commaSeperatedIntegers, MumoModule)
from tools.Utils import find_channel_with_name, get_empty_channels
# noinspection PyPep8Naming
class autochannel(MumoModule): class autochannel(MumoModule):
default_config = {'autochannel': ( default_config = {'autochannel': (
('servers', commaSeperatedIntegers, []), ('servers', commaSeperatedIntegers, []),
('spare_channel', int, 1), ('spare_channel', int, 1),
('top_games_limit', int, 10), ('top_games_limit', int, 10),
('root_channel_name', str, "Random Channel"), ('root_channel_name', str, "Random Channel"),
('check_timer_interval', int, 5) ('check_timer_interval', int, 5),
('game_channels_permament', str, "")
) )
} }
@@ -70,12 +71,7 @@ class autochannel(MumoModule):
pass pass
def init_channels(self, server): def init_channels(self, server):
channels = server.getChannels() self.root = find_channel_with_name(self.root_channel_name, server, 0)
if not self.root:
for cid, channel in channels.items():
if channel.name == self.root_channel_name and channel.parent == 0:
self.root = channel
if not self.root: if not self.root:
new_root_cid = server.addChannel(self.root_channel_name, 0) new_root_cid = server.addChannel(self.root_channel_name, 0)
@@ -83,24 +79,8 @@ class autochannel(MumoModule):
self.check_channel(server) self.check_channel(server)
@staticmethod
def get_user_for_channel(cid, server):
users = server.getUsers()
users_in_channel = {}
for uid, user in users.items():
if user.channel == cid:
users_in_channel[uid] = user
return users_in_channel
def check_channel(self, server): def check_channel(self, server):
empty_channels = [] empty_channels = get_empty_channels(server, self.root.id)
channels = server.getChannels()
for cid, channel in channels.items():
if channel.parent == self.root.id and len(self.get_user_for_channel(cid, server)) == 0:
empty_channels.append(cid)
for cid in empty_channels[1:]: for cid in empty_channels[1:]:
server.removeChannel(cid) server.removeChannel(cid)

View File

@@ -23,7 +23,6 @@ steam_genre_regex = re.compile("Genre:</b>.*?<a.*?>(.+?)</a>")
# AMAZON # AMAZON
amazon_regex = re.compile("ama?zo?n\.(?:de|com)/.*(?:dp|gp/product)/([A-Z0-9]{10})(?:(?:/+)|$)?") amazon_regex = re.compile("ama?zo?n\.(?:de|com)/.*(?:dp|gp/product)/([A-Z0-9]{10})(?:(?:/+)|$)?")
# TODO: Make regex more general (de|com)
# Commands # Commands
move_regex = re.compile("^moveall (.*)$") move_regex = re.compile("^moveall (.*)$")

35
tools/Utils.py Normal file
View File

@@ -0,0 +1,35 @@
def find_channel_with_name(name: str, server, parent: int = None):
channels = server.getChannels()
for cid, channel in channels.items():
if parent:
if channel.name == name and channel.parent == parent:
return channel
else:
if channel.name == name:
return channel
return None
def get_empty_channels(server, parent: int = None):
empty_channels = []
channels = server.getChannels()
for cid, channel in channels.items():
if parent:
if channel.parent == parent and len(get_user_for_channel(cid, server)) == 0:
empty_channels.append(cid)
return empty_channels
def get_user_for_channel(cid, server):
users = server.getUsers()
users_in_channel = {}
for uid, user in users.items():
if user.channel == cid:
users_in_channel[uid] = user
return users_in_channel