|
| 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)) |
0 commit comments