示例#1
0
    def _load_from_module_spec(self, spec, key):
        # precondition: key not in self.__extensions
        lib = importlib.util.module_from_spec(spec)
        sys.modules[key] = lib
        try:
            spec.loader.exec_module(lib)
        except Exception as e:
            del sys.modules[key]
            raise commands.ExtensionFailed(key, e) from e

        try:
            setup = getattr(lib, 'setup')
        except AttributeError:
            del sys.modules[key]
            raise commands.NoEntryPointError(key)

        try:
            setup(self)
        except Exception as e:
            del sys.modules[key]
            self._remove_module_references(lib.__name__)
            self._call_module_finalizers(lib, key)
            raise commands.ExtensionFailed(key, e) from e
        else:
            self.__extensions[key] = lib
 def setup(self, bot, name=None):
     if name is None:
         raise commands.ExtensionFailed(message='Group cog not given name.')
     self.cog_name = name
     self.name = name
     self.bot = bot
     if len(bot.guilds) == 0:
         raise commands.ExtensionFailed(message='Bot has no guilds.')
     self.guild = bot.get_guild(bot.guilds[0].id)
示例#3
0
 async def on_ready(self):
     print(f'Logged in as {self.bot.user.name} - {self.bot.user.id}')
     if len(self.bot.guilds) == 0:
         raise commands.ExtensionFailed(message='Bot has no guilds.')
     guild = self.bot.get_guild(self.bot.guilds[0].id)
     for c in guild.categories:
         self.bot.add_cog(make_cog(c.name)(self.bot))
 def _set_guild(self, val):
     self.__guild = val
     member_role = discord.utils.get(self.guild.roles,
                                     name=self.member_role_name)
     gm_role = discord.utils.get(self.guild.roles, name=self.gm_role_name)
     cat = discord.utils.get(self.guild.categories, name=self.group_name)
     if not member_role:
         raise commands.ExtensionFailed(message='No member role for group.')
     if not gm_role:
         raise commands.ExtensionFailed(message='No GM role for group.')
     if not cat:
         raise commands.ExtensionFailed(
             message='No category channel for group.')
     self.member_role_id = member_role.id
     self.gm_role_id = gm_role.id
     self.group_id = cat.id
示例#5
0
 async def gm_update(self, ctx: commands.Context):
     """[optionally force] updates the module automatically."""
     await ctx.message.delete(delay=30)
     await ctx.channel.trigger_typing()
     url = "https://github.com/dragdev-studios/guildmanager"
     cmd = f"python -m pip install git+{url} --upgrade --user"
     res = os.system(cmd)
     if res not in [0, 256]:
         return await ctx.send(
             f"Something went wrong while updating (cmd returned code other than 0(win32) or 256(linux). Please"
             f" update manually with command `{cmd}`. `returned: {res}`",
             delete_after=30)
     else:
         await asyncio.sleep(
             8
         )  # testing reveals on average its 7 seconds on a 93mbps download speed to update.
         try:
             self.bot.reload_extension("guildmanager.cog")
         except Exception as e:
             await ctx.send(
                 f"Error reloading updated module: `{str(e)}`. Traceback has been raised. If this issue"
                 f" persists, please open an issue at {url}/issues/new.\n\nSee: "
                 f"<{url}/issues/6>",
                 delete_after=30)
             raise commands.ExtensionFailed("guildmanager", e) from e
         else:
             return await ctx.send(f"Successfully reloaded.",
                                   delete_after=10)
示例#6
0
    def _load_from_module_spec(self, lib, key):
        try:
            if not hasattr(lib, 'Cog'):
                if not hasattr(lib, 'setup'):
                    raise discord.ClientException

                setup = lib.setup
            elif not hasattr(lib.Cog, 'setup'):
                raise discord.ClientException
            else:
                setup = lib.Cog.setup
        except discord.ClientException:
            del sys.modules[key]
            raise commands.NoEntryPointError(key)

        try:
            setup(self)
        except Exception as e:
            self._remove_module_references(lib.__name__)
            self._call_module_finalizers(lib, key)
            raise commands.ExtensionFailed(key, e) from e
        else:
            self._BotBase__extensions[key] = lib
示例#7
0
 async def cog_load(self) -> None:
     try:
         self.conn = await asyncpg.connect(SERVER_LOGS_URL)
     except Exception as e:
         raise commands.ExtensionFailed(self.qualified_name, original=e)