diff --git a/modules/channel_op.py b/modules/channel_op.py index a7070ba1..e789b4c4 100644 --- a/modules/channel_op.py +++ b/modules/channel_op.py @@ -101,7 +101,7 @@ class Module(ModuleManager.BaseModule): hostmask = self._ban_user(event["target"], True, event["server"].get_user(event["args_split"][0])) - self.bot.timers.add_persistent("unban", timeout, + self.timers.add_persistent("unban", timeout, server_id=event["server"].id, channel_name=event["target"].name, hostmask=hostmask) diff --git a/modules/coins.py b/modules/coins.py index 813ca779..87053001 100644 --- a/modules/coins.py +++ b/modules/coins.py @@ -1,5 +1,5 @@ import datetime, decimal, functools, math, re, secrets, time -from src import utils +from src import ModuleManager, utils SIDES = {"heads": 0, "tails": 1} DEFAULT_REDEEM_DELAY = 600 # 600 seconds, 10 minutes @@ -30,12 +30,11 @@ THIRD_COLUMN = list(range(1, 37))[2::3] REGEX_STREET = re.compile("street([1-9]|1[0-2])$") -class Module(object): - def __init__(self, bot, events, exports): - self.bot = bot - bot.timers.add("coin-interest", INTEREST_INTERVAL, +class Module(ModuleManager.BaseModule): + def on_load(self): + self.timers.add("coin-interest", INTEREST_INTERVAL, time.time()+self._until_next_hour()) - bot.timers.add("coin-lottery", LOTTERY_INTERVAL, + self.timers.add("coin-lottery", LOTTERY_INTERVAL, time.time()+self._until_next_6_hour()) def _until_next_hour(self, now=None): diff --git a/modules/database_backup.py b/modules/database_backup.py index 4f7e6395..55f18a13 100644 --- a/modules/database_backup.py +++ b/modules/database_backup.py @@ -1,17 +1,16 @@ import datetime, glob, os, shutil, time -from src import utils +from src import ModuleManager, utils BACKUP_INTERVAL = 60*60 # 1 hour BACKUP_COUNT = 5 -class Module(object): - def __init__(self, bot, events, exports): - self.bot = bot +class Module(ModuleManager.BaseModule): + def on_load(self): now = datetime.datetime.now() until_next_hour = 60-now.second until_next_hour += ((60-(now.minute+1))*60) - bot.timers.add("database-backup", BACKUP_INTERVAL, + self.timers.add("database-backup", BACKUP_INTERVAL, time.time()+until_next_hour) @utils.hook("timer.database-backup") diff --git a/modules/ducks.py b/modules/ducks.py index 25b3c718..cb6ff4af 100644 --- a/modules/ducks.py +++ b/modules/ducks.py @@ -1,7 +1,7 @@ import random from operator import itemgetter from time import time -from src import EventManager, utils +from src import EventManager, ModuleManager, utils DUCK_TAIL = "・゜゜・。。・゜゜" DUCK_HEAD = ["\_o< ", "\_O< ", "\_0< ", "\_\u00f6< ", "\_\u00f8< ", @@ -24,10 +24,8 @@ DUCK_MINIMUM_UNIQUE = 3 @utils.export("channelset", {"setting": "ducks-min-messages", "help": "Minimum messages between ducks spawning.", "validate": utils.int_or_none}) -class Module(object): - def __init__(self, bot, events, exports): - self.bot = bot - +class Module(ModuleManager.BaseModule): + def on_load(self): for server in self.bot.servers.values(): for channel in server.channels.values(): self.bootstrap(channel) diff --git a/modules/in.py b/modules/in.py index b490beba..76f5fcf3 100644 --- a/modules/in.py +++ b/modules/in.py @@ -17,7 +17,7 @@ class Module(ModuleManager.BaseModule): if seconds <= SECONDS_MAX: due_time = int(time.time())+seconds - self.bot.timers.add_persistent("in", seconds, due_time=due_time, + self.timers.add_persistent("in", seconds, due_time=due_time, target=event["target"].name, server_id=event["server"].id, nickname=event["user"].nickname, message=message) event["stdout"].write("Saved") diff --git a/modules/rest_api.py b/modules/rest_api.py index bfee0d6f..b8b3077a 100644 --- a/modules/rest_api.py +++ b/modules/rest_api.py @@ -3,7 +3,7 @@ import http.server, json, ssl, threading, uuid, urllib.parse import flask -from src import utils +from src import ModuleManager, utils _bot = None _events = None @@ -72,17 +72,15 @@ class Handler(http.server.BaseHTTPRequestHandler): @utils.export("botset", {"setting": "rest-api", "help": "Enable/disable REST API", "validate": utils.bool_or_none}) -class Module(object): - def __init__(self, bot, events, exports): - self.bot = bot +class Module(ModuleManager.BaseModule): + def on_load(self): global _bot - _bot = bot + _bot = self.bot - self.events = events global _events - _events = events + _events = self.events - if bot.get_setting("rest-api", False): + if self.bot.get_setting("rest-api", False): self.httpd = http.server.HTTPServer(("", 5000), Handler) self.httpd.socket = ssl.wrap_socket(self.httpd.socket, keyfile=self.bot.config["tls-api-key"], diff --git a/modules/scripts.py b/modules/scripts.py index 0c4ccb25..ae158546 100644 --- a/modules/scripts.py +++ b/modules/scripts.py @@ -1,11 +1,10 @@ import glob, json, os, subprocess -from src import IRCObject, utils +from src import IRCObject, ModuleManager, utils -class Module(object): - def __init__(self, bot, events, exports): - self.events = events - self._directory = os.path.join(bot.directory, "modules", "scripts") +class Module(ModuleManager.BaseModule): + def on_load(self): + self._directory = os.path.join(self.bot.directory, "modules", "scripts") self._hooks = [] self._load_scripts() diff --git a/modules/signals.py b/modules/signals.py index 8e57a9a0..627becfe 100644 --- a/modules/signals.py +++ b/modules/signals.py @@ -1,13 +1,11 @@ import signal -from src import Config, utils +from src import Config, ModuleManager, utils @utils.export("serverset", {"setting": "quit-quote", "help": "Set whether I pick a random quote to /quit with", "validate": utils.bool_or_none}) -class Module(object): - def __init__(self, bot, events, exports): - self.bot = bot - self.events = events +class Module(ModuleManager.BaseModule): + def on_load(self): signal.signal(signal.SIGINT, self.SIGINT) signal.signal(signal.SIGUSR1, self.SIGUSR1) diff --git a/modules/telegram.py b/modules/telegram.py index b836b2a8..0dd9bda7 100644 --- a/modules/telegram.py +++ b/modules/telegram.py @@ -9,13 +9,10 @@ from threading import Thread class Module(Thread): _name = "telegram" - def __init__(self, bot, events, exports): - key = bot.config.get("telegram-api-key") + def on_load(self): + key = self.bot.config.get("telegram-api-key") if not key: return - self.bot = bot - self.events = events - self.updater = telegram.ext.Updater(key) self.dispatcher = self.updater.dispatcher diff --git a/src/IRCBot.py b/src/IRCBot.py index d3e98b23..472c7054 100644 --- a/src/IRCBot.py +++ b/src/IRCBot.py @@ -14,7 +14,7 @@ class Bot(object): self._exports = exports self.log = log self.modules = modules - self.timers = timers + self._timers = timers self.start_time = time.time() self.lock = threading.Lock() @@ -100,7 +100,7 @@ class Bot(object): def get_poll_timeout(self): timeouts = [] - timeouts.append(self.timers.next()) + timeouts.append(self._timers.next()) timeouts.append(self.next_send()) timeouts.append(self.next_ping()) timeouts.append(self.next_read_timeout()) @@ -146,7 +146,7 @@ class Bot(object): while self.running: events = self.poll.poll(self.get_poll_timeout()) self.lock.acquire() - self.timers.call() + self._timers.call() self.cache.expire() for func in self._trigger_functions: @@ -192,7 +192,7 @@ class Bot(object): self.disconnect(server) reconnect_delay = self.config.get("reconnect-delay", 10) - self.timers.add("reconnect", reconnect_delay, + self._timers.add("reconnect", reconnect_delay, server_id=server.id) print("disconnected from %s, reconnecting in %d seconds" % ( diff --git a/src/ModuleManager.py b/src/ModuleManager.py index 6b097815..8f182c8d 100644 --- a/src/ModuleManager.py +++ b/src/ModuleManager.py @@ -27,6 +27,9 @@ class BaseModule(object): self.events = events self.exports = exports self.timers = timers + self.on_load() + def on_load(self): + pass class ModuleManager(object): def __init__(self, events, exports, timers, config, log, directory): diff --git a/start.py b/start.py index b7d7f2cb..17dec8d6 100755 --- a/start.py +++ b/start.py @@ -55,7 +55,7 @@ for server_id, alias in bot.database.servers.get_all(): if len(servers): bot._events.on("boot.done").call() - bot.timers.setup(bot.find_settings_prefix("timer-")) + timers.setup(bot.find_settings_prefix("timer-")) for server in servers: if not bot.connect(server):