-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
executable file
·106 lines (84 loc) · 3.73 KB
/
commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import importlib
import inspect
import glob
import sys
import gc
import os.path as path
from data.config import config
class command_module:
"""
Command module
Interprets commands and calls the appropriate methods to process and reply to them
"""
commands = []
plugins = {}
def __init__(self, irc):
"""
Set up command module
:param irc_client irc: IRC interface
"""
self.irc = irc
self.lastcommand = ""
self.setup_database()
self.load_plugins()
def setup_database(self):
"""
Set up database connection
We have no table to set up here, so not much to do
"""
self.dbconn = self.irc.db
self.db = self.dbconn.cursor()
def load_plugins(self):
"""
Load plugins
Plugins are python files in the `plugins/` folder. Any classes defined in those python files
that implement a "command" method are registered as plugins. They can then be called by users
as a chat command.
For example, if there's a plugin.py which contains a class named "hello" that has a `command` method, that
class will be instantiated, with a database cursor as the first constructor argument, and its `command()` method
called with `message`, `channel` and `user` as arguments when someone says "!hello".
"""
if self.plugins != {}:
self.plugins = {}
gc.collect()
# get all python files in the plugin folder
plugin_paths = glob.glob(path.dirname(__file__) + "/plugins/*.py")
plugin_files = [path.basename(file) for file in plugin_paths if path.isfile(file) is True]
for plugin_file in plugin_files:
# try to force reloading
module_name = "plugins." + plugin_file[:-3]
if module_name in sys.modules:
del sys.modules[module_name]
gc.collect()
# import the file and see if it has a class in it
try:
module = importlib.import_module(module_name)
importlib.reload(module)
except ModuleNotFoundError:
continue
plugin_classes = inspect.getmembers(module, inspect.isclass)
for plugin_class in plugin_classes:
# check if class has a "command" method
plugin_caller = getattr(plugin_class[1], "command", None)
# add that class as a hook that can be called!
if not inspect.isabstract(plugin_class[1]) and callable(plugin_caller):
self.plugins[plugin_class[0]] = plugin_class[1](self)
def process(self, message, channel, user):
"""
Process user input
Pretty much the most important method! If the method called for the command returns `True`, the
command will be saved as the last succesful command, and may be called again easily via `!2`.
:param string message: The message to process
:param string channel: The channel the message was said on
:param user user: User object
"""
command = message.split(" ")[0][len(config.command_prefix):]
if len(message) > 0 and message[:len(config.command_prefix)] == config.command_prefix:
done = False
if command == "2" and self.lastcommand != "":
self.process(self.lastcommand, channel, user)
elif command in self.plugins:
# plugin commands (could be anything!)
done = self.plugins[command].command(message, channel, user)
if done:
self.lastcommand = message