Skip to content

Commit 97a51f2

Browse files
author
Rhodesia
committed
plugin support
1 parent 05d4aba commit 97a51f2

File tree

5 files changed

+125
-9
lines changed

5 files changed

+125
-9
lines changed

comet/bot.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ def __init__(self, cfg):
99
commands.Bot.__init__(self, command_prefix=when_mentioned_or(cfg['discord']['prefix']))
1010
LoggingClass.__init__(self)
1111

12-
self.cfg = cfg
12+
self.cfg = cfg
13+
1314

1415
async def on_ready(self):
1516
# Ready!

launcher.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import sys
22

3-
import ruamel.yaml as yaml
4-
3+
import utils.clark as clark
54
from comet.bot import Comet
5+
from ruamel.yaml import YAMLError
66
from comet.logging import setup_logging
77

88
# Load configuration file.
99
try:
10-
with open('config.yml', 'r') as config_file:
11-
try:
12-
cfg = yaml.safe_load(config_file.read())
13-
except yaml.YAMLError as exc:
14-
print('Error loading configuration:', exc, file=sys.stderr)
15-
sys.exit(1)
10+
cfg = clark.load('config.yml')
11+
except YAMLError as exc:
12+
print('Error loading configuration:', exc, file=sys.stderr)
13+
sys.exit(1)
1614
except FileNotFoundError:
1715
print('A `config.yaml` file was not found.', file=sys.stderr)
1816
sys.exit(1)
@@ -22,4 +20,9 @@
2220

2321
# Make a Comet and run it.
2422
comet = Comet(cfg)
23+
try:
24+
comet.load_extension("plugins.core")
25+
except Exception as e:
26+
print("failed to load core: {}".format(e))
27+
exit(0)
2528
comet.run(cfg['discord']['token'])

plugins/core.py

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#Plugin system inspired from one of my earlier creations, Knarrenhienz -C
2+
3+
4+
#imports the checks function
5+
from utils import checks
6+
import utils.clark as clark
7+
from ruamel.yaml import YAMLError
8+
9+
from discord.ext import commands
10+
11+
class Core:
12+
"""Core functions of the bot, includes plugins"""
13+
def __init__(self, bot):
14+
self.bot = bot
15+
print("loading plugins...")
16+
try:
17+
self.plugins = clark.load('config/plugins.yml')
18+
except YAMLError as exc:
19+
raise Exception('Invalid yaml, wtf!')
20+
except FileNotFoundError:
21+
print("file not found, creating new plugin config!")
22+
self.plugins = {}
23+
clark.save(self.plugins, "config/plugins.yml")
24+
for i in self.plugins:
25+
try:
26+
self.bot.load_extension("plugins." + i)
27+
print("[ \033[0;32mdone\033[0;0m ] %s loaded sucessfuly" % i)
28+
except Exception as e:
29+
self.plugins.remove(i)
30+
print("[ \033[1;31mfail\033[0;0m ] {}: {}".format(i, e))
31+
clark.save(self.plugins, "config/plugins.yml")
32+
print("[ \033[1;96mwarn\033[0;0m ] {} unloaded due to errors, load it when it works again".format(i))
33+
34+
print("%i plugins loaded" % len(self.plugins))
35+
36+
@checks.is_dev()
37+
@commands.command(name="plugins")
38+
async def loaded(self, ctx):
39+
"""lists loaded plugins"""
40+
await ctx.send("```\nPlugins:\n{}```".format(', '.join(self.plugins)))
41+
42+
@checks.is_dev()
43+
@commands.command()
44+
async def reload(self, ctx, name):
45+
"""reloads a plugin"""
46+
if name == "core":
47+
await ctx.send("you cant just reload the core!")
48+
return
49+
try:
50+
self.bot.unload_extension("plugins." + name)
51+
print("unloaded "+name)
52+
except Exception as e:
53+
await ctx.send("failed to reload plugin: {}".format(e))
54+
try:
55+
self.bot.load_extension("plugins." + name)
56+
print("reloaded "+name+" sucessfully")
57+
await ctx.send("plugin reloaded sucessfuly")
58+
except Exception as e:
59+
await ctx.send("failed to load plugin: {}".format(e))
60+
if name in self.plugins:
61+
self.plugins.remove(name)
62+
await ctx.send("I see that {} is in your loaded plugins, I will unload it for now since it has errors".format(name))
63+
clark.save(self.plugins, "config/plugins.yml")
64+
@checks.is_dev()
65+
@commands.command()
66+
async def load(self, ctx, name):
67+
"""loads a plugin"""
68+
if name == "core":
69+
await ctx.send("core is already loaded by default!")
70+
return
71+
if name in self.plugins:
72+
await ctx.send("plugin is already loaded")
73+
return
74+
try:
75+
self.bot.load_extension("plugins." + name)
76+
self.plugins.append(name)
77+
clark.save(self.plugins, "config/plugins.yml")
78+
await ctx.send("plugin loaded sucessfuly")
79+
except Exception as e:
80+
await ctx.send("failed to load plugin: {}".format(e))
81+
82+
@checks.is_dev()
83+
@commands.command()
84+
async def unload(self, ctx, name):
85+
"""unloads a plugin"""
86+
if name == "core":
87+
await ctx.send("unloading the core is disabled")
88+
return
89+
try:
90+
self.bot.unload_extension("plugins." + name)
91+
self.plugins.remove(name)
92+
clark.save(self.plugins, "config/plugins.yml")
93+
await ctx.send("plugin unloaded sucessfuly")
94+
except Exception as e:
95+
await ctx.send("failed to load plugin: {}".format(e))
96+
def setup(bot):
97+
bot.add_cog(Core(bot))

utils/checks.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from discord.ext import commands
2+
from __main__ import cfg
3+
def is_dev():
4+
return commands.check(lambda ctx: check_dev(ctx))
5+
def check_dev(ctx):
6+
return ctx.message.author.id == cfg['discord']['ownerid']

utils/clark.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#RIP CLARK KENT
2+
import ruamel.yaml as yaml
3+
def load(path):
4+
with open(path, 'r') as f:
5+
x = yaml.safe_load(f)
6+
return x
7+
def save(pointer, path):
8+
with open(path, 'w') as f:
9+
yaml.dump(pointer, f)

0 commit comments

Comments
 (0)