Pass context-wrapped timers to each module, add ModuleManager.BaseModule.on_load

This commit is contained in:
jesopo 2018-10-12 18:07:23 +01:00
parent cb94fa9ae4
commit 819f4e0680
12 changed files with 37 additions and 46 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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"],

View file

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

View file

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

View file

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

View file

@ -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" % (

View file

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

View file

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