Ejemplo n.º 1
0
 def init_database(self):
     self.log.info('Connecting to Database...')
     self.db = Database(self, self.cfg.db)
     try:
         self.db.test.collection.find_one({})
     except pymongo.errors.ServerSelectionTimeoutError:
         self.log.error('A Connection To The Database Host Failed!')
         exit(errno.ETIMEDOUT)
     except pymongo.errors.OperationFailure:
         self.log.error('Database Access Operation Failed!')
         exit(errno.EACCES)
     self.log.info('Successfully Connected to Database')
Ejemplo n.º 2
0
 async def init_database(self):
     self.log.info('Connecting to Database...')
     self.db = Database(self, self.cfg.db)
     try:
         await self.db[self.db.db_cfg.database].collection.find_one({})
         await self.db.precache_settings()
     except ServerSelectionTimeoutError:
         self.log.error('A Connection To The Database Host Failed!')
         exit(errno.ETIMEDOUT)
     except OperationFailure:
         self.log.error('Database Access Operation Failed!')
         exit(errno.EACCES)
     self.log.info('Successfully Connected to Database')
Ejemplo n.º 3
0
 async def init_database(self):
     """
     Initializes the database connection to MongoDB.
     Also pre-caches potentially heavy resources from the DB.
     :return:
     """
     self.log.info('Connecting to Database...')
     self.db = Database(self, self.cfg.db)
     try:
         await self.db[self.db.db_nam].collection.find_one({})
         if self.cfg.cache.type not in ['redis', 'mixed']:
             await self.db.precache_settings()
             await self.db.precache_profiles()
             await self.db.precache_resources()
         set_color_cache_coll(self.db[self.db.db_nam].ColorCache)
     except ServerSelectionTimeoutError:
         self.log.error('A Connection To The Database Host Failed!')
         exit(errno.ETIMEDOUT)
     except OperationFailure:
         self.log.error('Database Access Operation Failed!')
         exit(errno.EACCES)
     self.log.info('Successfully Connected to Database')
Ejemplo n.º 4
0
class ApexSigma(client_class):
    def __init__(self):
        super().__init__()
        self.ready = False
        self.create_cache()
        self.init_logger()
        self.log.info('---------------------------------')
        self.init_config()
        self.log.info('---------------------------------')
        self.init_database()
        self.log.info('---------------------------------')
        self.init_cooldown()
        self.log.info('---------------------------------')
        self.init_music()
        self.log.info('---------------------------------')
        self.init_modules()

    @staticmethod
    def create_cache():
        if not os.path.exists('cache'):
            os.makedirs('cache')

    def init_logger(self):
        self.log = create_logger('Sigma')
        self.log.info('Logger Created')

    def init_config(self):
        self.log.info('Loading Configuration...')
        self.cfg = load_config()
        self.log.info('Core Configuration Data Loaded')

    def init_database(self):
        self.log.info('Connecting to Database...')
        self.db = Database(self, self.cfg.db)
        try:
            self.db.test.collection.find_one({})
        except pymongo.errors.ServerSelectionTimeoutError:
            self.log.error('A Connection To The Database Host Failed!')
            exit(errno.ETIMEDOUT)
        except pymongo.errors.OperationFailure:
            self.log.error('Database Access Operation Failed!')
            exit(errno.EACCES)
        self.log.info('Successfully Connected to Database')

    def init_cooldown(self):
        self.log.info('Loading Cooldown Controls...')
        self.cooldown = CooldownControl(self)
        self.log.info('Cooldown Controls Successfully Enabled')

    def init_music(self):
        self.log.info('Loading Music Controller...')
        self.music = MusicCore(self)
        self.log.info('Music Controller Initialized and Ready')

    def init_modules(self):
        self.log.info('Loading Sigma Modules')
        self.modules = PluginManager(self)

    def get_prefix(self, message):
        prefix = self.cfg.pref.prefix
        if message.guild:
            pfx_search = self.db.get_guild_settings(message.guild.id, 'Prefix')
            if pfx_search:
                prefix = pfx_search
        return prefix

    def run(self):
        try:
            self.log.info('Connecting to Discord Gateway...')
            super().run(self.cfg.dsc.token, bot=self.cfg.dsc.bot)
        except discord.LoginFailure:
            self.log.error('Invalid Token!')
            exit(errno.EPERM)

    async def on_connect(self):
        event_name = 'connect'
        if event_name in self.modules.events:
            for event in self.modules.events[event_name]:
                self.loop.create_task(event.execute())

    async def on_shard_ready(self, shard_id):
        self.log.info(f'Connection to Discord Shard #{shard_id} Established')
        event_name = 'shard_ready'
        if event_name in self.modules.events:
            for event in self.modules.events[event_name]:
                self.loop.create_task(event.execute(shard_id))

    async def on_ready(self):
        self.ready = True
        self.log.info('---------------------------------')
        self.log.info('Apex Sigma Fully Loaded and Ready')
        self.log.info('---------------------------------')
        self.log.info(
            f'User Account: {self.user.name}#{self.user.discriminator}')
        self.log.info(f'User Snowflake: {self.user.id}')
        self.log.info('---------------------------------')
        self.log.info('Launching On-Ready Modules...')
        event_name = 'ready'
        if event_name in self.modules.events:
            for event in self.modules.events[event_name]:
                self.loop.create_task(event.execute())
        self.log.info('All On-Read Module Loops Created')
        self.log.info('---------------------------------')

    async def on_message(self, message):
        if not message.author.bot:
            event_name = 'message'
            prefix = self.get_prefix(message)
            if message.content.startswith(prefix):
                args = message.content.split(' ')
                cmd = args.pop(0)[len(self.cfg.pref.prefix):].lower()
                if cmd in self.modules.alts:
                    cmd = self.modules.alts[cmd]
                if cmd in self.modules.commands:
                    self.loop.create_task(self.modules.commands[cmd].execute(
                        message, args))
            if event_name in self.modules.events:
                for event in self.modules.events[event_name]:
                    self.loop.create_task(event.execute(message))
            if self.user.mentioned_in(message):
                event_name = 'mention'
                if event_name in self.modules.events:
                    for event in self.modules.events[event_name]:
                        self.loop.create_task(event.execute(message))

    async def on_message_edit(self, before, after):
        if not before.author.bot:
            event_name = 'message_edit'
            if event_name in self.modules.events:
                for event in self.modules.events[event_name]:
                    self.loop.create_task(event.execute(before, after))

    async def on_member_join(self, member):
        if not member.bot:
            event_name = 'member_join'
            if event_name in self.modules.events:
                for event in self.modules.events[event_name]:
                    self.loop.create_task(event.execute(member))

    async def on_member_remove(self, member):
        if not member.bot:
            event_name = 'member_remove'
            if event_name in self.modules.events:
                for event in self.modules.events[event_name]:
                    self.loop.create_task(event.execute(member))

    async def on_member_update(self, before, after):
        if not before.bot:
            event_name = 'member_update'
            if event_name in self.modules.events:
                for event in self.modules.events[event_name]:
                    self.loop.create_task(event.execute(before, after))

    async def on_guild_join(self, guild):
        event_name = 'guild_join'
        if event_name in self.modules.events:
            for event in self.modules.events[event_name]:
                self.loop.create_task(event.execute(guild))

    async def on_guild_remove(self, guild):
        event_name = 'guild_remove'
        if event_name in self.modules.events:
            for event in self.modules.events[event_name]:
                self.loop.create_task(event.execute(guild))

    async def on_voice_state_update(self, member, before, after):
        event_name = 'voice_state_update'
        if event_name in self.modules.events:
            for event in self.modules.events[event_name]:
                self.loop.create_task(event.execute(member, before, after))
Ejemplo n.º 5
0
class ApexSigma(client_class):
    def __init__(self):
        super().__init__()
        self.ready = False
        # State attributes before initialization.
        self.log = None
        self.cfg = None
        self.db = None
        self.cool_down = None
        self.music = None
        self.modules = None
        self.queue = QueueControl()
        self.launched = False
        self.cache = {}
        # Initialize startup methods and attributes.
        self.create_cache()
        self.init_logger()
        self.log.info('---------------------------------')
        self.init_config()
        self.log.info('---------------------------------')
        self.init_database()
        self.log.info('---------------------------------')
        self.init_cool_down()
        self.log.info('---------------------------------')
        self.init_music()
        self.log.info('---------------------------------')
        self.info = Information()
        self.init_modules(init=True)
        self.start_time = arrow.utcnow()
        self.message_count = 0
        self.command_count = 0

    @staticmethod
    def create_cache():
        if os.path.exists('cache'):
            shutil.rmtree('cache')
        os.makedirs('cache')

    def init_logger(self):
        self.log = create_logger('Sigma')
        self.log.info('Logger Created')

    def init_config(self):
        self.log.info('Loading Configuration...')
        self.cfg = init_cfg
        self.log.info(f'Running as a Bot: {self.cfg.dsc.bot}')
        self.log.info(f'Default Bot Prefix: {self.cfg.pref.prefix}')
        self.log.info('Core Configuration Data Loaded')

    def init_database(self):
        self.log.info('Connecting to Database...')
        self.db = Database(self, self.cfg.db)
        try:
            self.db.test.collection.find_one({})
        except pymongo.errors.ServerSelectionTimeoutError:
            self.log.error('A Connection To The Database Host Failed!')
            exit(errno.ETIMEDOUT)
        except pymongo.errors.OperationFailure:
            self.log.error('Database Access Operation Failed!')
            exit(errno.EACCES)
        self.log.info('Successfully Connected to Database')

    def init_cool_down(self):
        self.log.info('Loading Cool-down Controls...')
        self.cool_down = CooldownControl(self)
        self.log.info('Cool-down Controls Successfully Enabled')

    def init_music(self):
        self.log.info('Loading Music Controller...')
        self.music = MusicCore(self)
        self.log.info('Music Controller Initialized and Ready')

    def init_modules(self, init=False):
        if init:
            self.log.info('Loading Sigma Modules')
        self.modules = PluginManager(self, init)

    def get_prefix(self, message):
        prefix = self.cfg.pref.prefix
        if message.guild:
            pfx_search = self.db.get_guild_settings(message.guild.id, 'Prefix')
            if pfx_search:
                prefix = pfx_search
        return prefix

    def run(self):
        try:
            self.log.info('Connecting to Discord Gateway...')
            super().run(self.cfg.dsc.token, bot=self.cfg.dsc.bot)
        except discord.LoginFailure:
            self.log.error('Invalid Token!')
            exit(errno.EPERM)

    async def event_runner(self, event_name, *args):
        if event_name in self.modules.events:
            for event in self.modules.events[event_name]:
                # self.loop.create_task(event.execute(*args))
                task = event, *args
                await self.queue.queue.put(task)

    async def on_connect(self):
        event_name = 'connect'
        if event_name in self.modules.events:
            for event in self.modules.events[event_name]:
                self.loop.create_task(event.execute())

    async def on_shard_ready(self, shard_id):
        self.log.info(f'Connection to Discord Shard #{shard_id} Established')
        event_name = 'shard_ready'
        self.loop.create_task(self.event_runner(event_name, shard_id))

    async def on_ready(self):
        self.ready = True
        self.log.info('---------------------------------')
        self.log.info('Apex Sigma Fully Loaded and Ready')
        self.log.info('---------------------------------')
        self.log.info(
            f'User Account: {self.user.name}#{self.user.discriminator}')
        self.log.info(f'User Snowflake: {self.user.id}')
        self.log.info('---------------------------------')
        self.log.info('Launching On-Ready Modules...')
        self.loop.create_task(self.event_runner('ready'))
        if not self.launched:
            self.loop.create_task(self.event_runner('launch'))
            self.launched = True
        self.log.info('All On-Ready Module Loops Created')
        self.log.info('---------------------------------')

    def get_cmd_and_args(self, message, args, mention=False):
        args = list(filter(lambda a: a != '', args))
        if mention:
            if args:
                cmd = args.pop(0).lower()
            else:
                cmd = None
        else:
            cmd = args.pop(0)[len(self.get_prefix(message)):].lower()
        return cmd, args

    def clean_self_mentions(self, message):
        for mention in message.mentions:
            if mention.id == self.user.id:
                message.mentions.remove(mention)
                break

    async def on_message(self, message):
        self.message_count += 1
        if not message.author.bot:
            event_name = 'message'
            self.loop.create_task(self.event_runner(event_name, message))
            if self.user.mentioned_in(message):
                event_name = 'mention'
                self.loop.create_task(self.event_runner(event_name, message))
            prefix = self.get_prefix(message)
            if message.content.startswith(prefix):
                args = message.content.split(' ')
                cmd, args = self.get_cmd_and_args(message, args)
            elif message.content.startswith(self.user.mention):
                args = message.content.split(' ')[1:]
                self.clean_self_mentions(message)
                cmd, args = self.get_cmd_and_args(message, args, mention=True)
            elif message.content.startswith(f'<@!{self.user.id}>'):
                args = message.content.split(' ')[1:]
                cmd, args = self.get_cmd_and_args(message, args, mention=True)
            else:
                cmd = None
                args = []
            if cmd:
                if cmd in self.modules.alts:
                    cmd = self.modules.alts[cmd]
                if cmd in self.modules.commands:
                    command = self.modules.commands[cmd]
                    # self.loop.create_task(command.execute(message, args))
                    task = command, message, args
                    await self.queue.queue.put(task)

    async def on_message_edit(self, before, after):
        if not before.author.bot:
            event_name = 'message_edit'
            self.loop.create_task(self.event_runner(event_name, before, after))

    async def on_message_delete(self, message):
        if not message.author.bot:
            event_name = 'message_delete'
            self.loop.create_task(self.event_runner(event_name, message))

    async def on_member_join(self, member):
        if not member.bot:
            event_name = 'member_join'
            self.loop.create_task(self.event_runner(event_name, member))

    async def on_member_remove(self, member):
        if not member.bot:
            event_name = 'member_remove'
            self.loop.create_task(self.event_runner(event_name, member))

    async def on_member_update(self, before, after):
        if not before.bot:
            event_name = 'member_update'
            self.loop.create_task(self.event_runner(event_name, before, after))

    async def on_guild_join(self, guild):
        event_name = 'guild_join'
        self.loop.create_task(self.event_runner(event_name, guild))

    async def on_guild_remove(self, guild):
        event_name = 'guild_remove'
        self.loop.create_task(self.event_runner(event_name, guild))

    async def on_voice_state_update(self, member, before, after):
        event_name = 'voice_state_update'
        self.loop.create_task(
            self.event_runner(event_name, member, before, after))