async def load_all_extensions(self, reload=False): """Attempt to load all .py files in cogs/ as cog extensions. Return a dictionary which maps cog names to a boolean value (True = successfully loaded; False = not successfully loaded). """ succeeded = {} disabled_extensions = set() if not info.DEV: disabled_extensions.add('tests') for extension in get_extensions(disabled=disabled_extensions): try: if reload or extension not in self.cogs_loaded: self.load_extension(f'cogs.{extension}') l.info(f"Loaded extension '{extension}'") self.cogs_loaded.add(extension) succeeded[extension] = True except Exception as exc: l.error( f"Failed to load extension {extension!r} due to {type(exc).__name__}: {exc}" ) if hasattr(exc, 'original'): l.error( f"More details: {type(exc.original).__name__}: {exc.original}" ) succeeded[extension] = False if succeeded: l.info(LOG_SEP) return succeeded
async def load_all_extensions(self, reload=False): """ Attempts to load all .py files in cogs/ as cog extensions. Returns a dictionary which maps cog names to a boolean value (True = successfully loaded; False = not successfully loaded). """ succeeded = {} for extension in get_extensions(): try: if reload or extension not in self.cogs_loaded: self.load_extension(f'cogs.{extension}') l.info(f"Loaded extension '{extension}'") self.cogs_loaded.add(extension) succeeded[extension] = True except Exception as e: error = f"{extension}\n {type(e).__name__} : {e}" l.error(f"Failed to load extension '{error}'") succeeded[extension] = False if succeeded: l.info(LOG_SEP) return succeeded
import cogs import discord from configparser import ConfigParser from constants import info from discord.ext import commands from utils import bot parser = ConfigParser() parser.read("config.ini") discby = bot.Discby(command_prefix=commands.when_mentioned_or("ly!"), status=discord.Status.dnd, cogs=cogs.get_extensions() | {"jishaku"}, config=parser, description=info.ABOUT_TEXT) discby.run(parser["Auth"]["token"])
logging.getLogger("discord").setLevel(LOG_LEVEL_API) l.setLevel(LOG_LEVEL_BOT) try: with open("admin.txt") as f: owner_id = int(f.read()) except IOError: owner_id = None COMMAND_PREFIX = "!" bot = commands.Bot(command_prefix=commands.when_mentioned_or(COMMAND_PREFIX), case_insensitive=True, owner_id=owner_id, status=discord.Status.dnd) bot.needed_extensions = set(get_extensions()) bot.loaded_extensions = set() @bot.event async def on_ready(): l.info(f"Ready") await wait_until_loaded() await bot.change_presence(status=discord.Status.online) @bot.event async def on_connect(): l.info(f"Connected as {bot.user}") await wait_until_loaded() await bot.change_presence(status=discord.Status.idle)
@bot.event async def on_error(event_method, *args, **kwargs): _, exc, _ = sys.exc_info() l.error( f"'{str(exc)}' encountered during '{event_method}' (args: {args}; kwargs: {kwargs})" ) await report_error(None, exc, *args, bot=bot, event_method=event_method, **kwargs) @bot.listen() async def on_message(message): ch = message.channel is_private = isinstance(ch, discord.DMChannel) or isinstance( ch, discord.GroupChannel) l.info( f"[#{ch.id if is_private else ch.name}] {message.author.display_name}: {message.content}" ) if __name__ == '__main__': for extension in get_extensions(): bot.load_extension('cogs.' + extension) bot.run(TOKEN)