refactor bot.commands and bot._callables
This commit is contained in:
parent
af08fe0f16
commit
6a067166fe
33
bot.py
33
bot.py
|
@ -76,12 +76,6 @@ class Fulvia(irc.IRCClient):
|
||||||
A class with some basic interactions for the bot's sqlite3 databse.
|
A class with some basic interactions for the bot's sqlite3 databse.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self._callables = {}
|
|
||||||
"""
|
|
||||||
A dictionary containing all callable functions loaded from
|
|
||||||
modules. Keys are the functions name.
|
|
||||||
"""
|
|
||||||
|
|
||||||
self._hooks = []
|
self._hooks = []
|
||||||
"""
|
"""
|
||||||
A list containing all function names to be hooked with every message
|
A list containing all function names to be hooked with every message
|
||||||
|
@ -120,7 +114,6 @@ class Fulvia(irc.IRCClient):
|
||||||
Find and load all of our modules.
|
Find and load all of our modules.
|
||||||
"""
|
"""
|
||||||
print(f"Loading modules...")
|
print(f"Loading modules...")
|
||||||
self._callables = {}
|
|
||||||
self._hooks = []
|
self._hooks = []
|
||||||
self.commands = {}
|
self.commands = {}
|
||||||
self._times = {}
|
self._times = {}
|
||||||
|
@ -152,20 +145,11 @@ class Fulvia(irc.IRCClient):
|
||||||
convenient table.
|
convenient table.
|
||||||
"""
|
"""
|
||||||
if hasattr(func, 'commands'):
|
if hasattr(func, 'commands'):
|
||||||
self._callables[func.__name__] = func
|
|
||||||
for cmd in func.commands:
|
for cmd in func.commands:
|
||||||
self.commands[cmd] = tools.Command(cmd)
|
self.commands[cmd] = func
|
||||||
self.commands[cmd]._func_name = func.__name__
|
|
||||||
self.commands[cmd].priv = func.priv
|
|
||||||
self.commands[cmd].doc = func._docs
|
|
||||||
if cmd in func.aliases:
|
|
||||||
self.commands[cmd].canonical = False
|
|
||||||
aliases = [a for a in func.commands if a != cmd]
|
|
||||||
self.commands[cmd].aliases = aliases
|
|
||||||
|
|
||||||
if func.hook:
|
if func.hook:
|
||||||
self._callables[func.__name__] = func
|
self._hooks.append(func)
|
||||||
self._hooks.append(func.__name__)
|
|
||||||
|
|
||||||
if func.rate or func.channel_rate or func.global_rate:
|
if func.rate or func.channel_rate or func.global_rate:
|
||||||
self._times[func.__name__] = {}
|
self._times[func.__name__] = {}
|
||||||
|
@ -187,12 +171,10 @@ class Fulvia(irc.IRCClient):
|
||||||
return
|
return
|
||||||
|
|
||||||
if hasattr(func, 'commands'):
|
if hasattr(func, 'commands'):
|
||||||
self._callables.pop(func.__name__)
|
|
||||||
for command in func.commands:
|
for command in func.commands:
|
||||||
self.commands.pop(command)
|
self.commands.pop(command)
|
||||||
|
|
||||||
if func.hook:
|
if func.hook:
|
||||||
self._callables.pop(func.__name__)
|
|
||||||
self._hooks.remove(func.__name__)
|
self._hooks.remove(func.__name__)
|
||||||
|
|
||||||
if func.rate or func.channel_rate or func.global_rate:
|
if func.rate or func.channel_rate or func.global_rate:
|
||||||
|
@ -268,19 +250,18 @@ class Fulvia(irc.IRCClient):
|
||||||
line = "<" + opSym + nick + ">" + " " + message
|
line = "<" + opSym + nick + ">" + " " + message
|
||||||
self.log(channel, line)
|
self.log(channel, line)
|
||||||
|
|
||||||
func_names = []
|
funcs = []
|
||||||
if message.startswith(self.prefix) and message != self.prefix:
|
if message.startswith(self.prefix) and message != self.prefix:
|
||||||
command, _, _ = message.partition(" ")
|
command = message.partition(" ")[0]
|
||||||
command = command.replace(self.prefix, "", 1)
|
command = command.replace(self.prefix, "", 1)
|
||||||
cmd = self.commands.get(command)
|
cmd = self.commands.get(command)
|
||||||
if not cmd:
|
if not cmd:
|
||||||
return
|
return
|
||||||
func_names.append(cmd._func_name)
|
funcs.append(cmd)
|
||||||
|
|
||||||
func_names += self._hooks
|
funcs += self._hooks
|
||||||
|
|
||||||
for func_name in func_names:
|
for func in funcs:
|
||||||
func = self._callables[func_name]
|
|
||||||
trigger = Trigger(user, channel, message, "PRIVMSG", self.config)
|
trigger = Trigger(user, channel, message, "PRIVMSG", self.config)
|
||||||
bot = FulviaWrapper(self, trigger)
|
bot = FulviaWrapper(self, trigger)
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,7 @@ def unload_module(bot, name):
|
||||||
bot.unregister_callable(obj)
|
bot.unregister_callable(obj)
|
||||||
|
|
||||||
del old_module
|
del old_module
|
||||||
|
delattr(sys.modules['modules'], name)
|
||||||
del sys.modules[name]
|
del sys.modules[name]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -236,7 +236,7 @@ def example(ex_input, ex_output=None):
|
||||||
|
|
||||||
def url_callback(url):
|
def url_callback(url):
|
||||||
"""
|
"""
|
||||||
Decore a function with a callback to the URL module.
|
Decorate a function with a callback to the URL module.
|
||||||
|
|
||||||
This URL will be added to the bot.url_callbacks dictionary in the bot's
|
This URL will be added to the bot.url_callbacks dictionary in the bot's
|
||||||
memory which the URL module will compare it's URL's against. If a key in
|
memory which the URL module will compare it's URL's against. If a key in
|
||||||
|
|
|
@ -26,8 +26,6 @@ def new_bot_name(number):
|
||||||
@user_joined(True)
|
@user_joined(True)
|
||||||
def adalwulf_(bot, trigger):
|
def adalwulf_(bot, trigger):
|
||||||
"""Renames adalwulf__."""
|
"""Renames adalwulf__."""
|
||||||
print(True)
|
|
||||||
print(trigger.nick)
|
|
||||||
if not trigger.nick.startswith('defaultnick'):
|
if not trigger.nick.startswith('defaultnick'):
|
||||||
return
|
return
|
||||||
names = bot.channels[trigger.channel].users
|
names = bot.channels[trigger.channel].users
|
||||||
|
|
|
@ -72,4 +72,4 @@ def f_unload(bot, trigger):
|
||||||
return bot.msg(f"Module '{name}' not loaded, try the 'load' command.")
|
return bot.msg(f"Module '{name}' not loaded, try the 'load' command.")
|
||||||
|
|
||||||
loader.unload_module(bot, name)
|
loader.unload_module(bot, name)
|
||||||
bot.msg(f"Module '{name}' unloaded.")
|
bot.msg(f"Module '{name}' unloaded.")
|
||||||
|
|
|
@ -187,20 +187,6 @@ class Channel(object):
|
||||||
self.privileges[new] = self.privileges.pop(old)
|
self.privileges[new] = self.privileges.pop(old)
|
||||||
|
|
||||||
|
|
||||||
class Command():
|
|
||||||
"""
|
|
||||||
A representation of a command and associated documentation and other
|
|
||||||
atributes.
|
|
||||||
"""
|
|
||||||
def __init__(self, name):
|
|
||||||
self.name = name
|
|
||||||
self._func_name = ""
|
|
||||||
self.priv = 0
|
|
||||||
self.doc = None
|
|
||||||
self.canonical = True
|
|
||||||
self.aliases = []
|
|
||||||
|
|
||||||
|
|
||||||
def configureHostMask(mask):
|
def configureHostMask(mask):
|
||||||
"""
|
"""
|
||||||
Returns a valid hostmask based on user input.
|
Returns a valid hostmask based on user input.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user