def sql_guilds_refresh(self): for guild in self.bot.client.state.guilds.copy().keys(): try: guild_object = self.bot.client.state.guilds.get(guild, None) if guild_object is not None: sql_guild = handle_sql( db_session.query(guilds).filter_by( guild_id=guild, ).first, ) if sql_guild is None: sql_guild = guilds( guild_id=guild, last_seen=datetime.now().isoformat(), name=guild_object.name, ) handle_sql(db_session.add, sql_guild) else: try: handle_sql( db_session.query(guilds).filter_by( guild_id=guild, ).update, { "last_seen": datetime.now().isoformat(), "name": guild_object.name }, ) except SQLexception as e: log.warning( "Failed to post server to SQL server in status: {}" .format(e.previous_exception)) else: handle_sql(db_session.flush) except CommandError as e: log.warning("Failed to call SQL server: {}".format(e.msg)) log.warning(str(e.original_exception)) break
def on_guild_update(self, event): try: guild = handle_sql( db_session.query(guilds).filter_by( guild_id=event.guild.id, ).first) if guild is None: guild = guilds( guild_id=event.guild.id, last_seen=datetime.now().isoformat(), name=event.guild.name, ) handle_sql(db_session.add, guild) handle_sql(db_session.flush) else: if guild.name != event.guild.name: handle_sql( db_session.query(guilds).filter_by( guild_id=event.guild.id, ).update, { "name": event.guild.name, "last_seen": datetime.now().isoformat(), }, ) handle_sql(db_session.flush) except CommandError as e: log.warning("Failed to update guild {} SQL entry: {}".format( event.guild.id, e.msg)) log.exception(e.original_exception)
def on_guild_join(self, event): if type(event.unavailable) is Unset: if handle_sql( db_session.query(guilds).filter_by( guild_id=event.guild.id).first, ) is None: guild = guilds( guild_id=event.guild.id, last_seen=datetime.now().isoformat(), name=event.guild.name, ) db_session.add(guild) handle_sql(db_session.flush) self.prefixes[event.guild.id] = self.command_prefix
def custom_prefix(self, event): if not event.author.bot: if ((not hasattr(event, "channel") or event.channel is None) and not isinstance(event.guild_id, Unset)): guild = getattr(event, "guild", None) if guild is None: event.guild = self.client.state.guilds.get( event.guild_id, None, ) if event.guild is None: event.guild = self.client.state.guilds[ event.guild_id] = api_loop( self.client.api.guilds_get, event.guild_id, ) event.channel = event.guild.channels.get( event.channel_id, None) if event.channel is None: event.channel = api_loop( self.client.api.channels_get, event.channel_id, ) elif ((not hasattr(event, "channel") or event.channel is None) and isinstance(event.guild_id, Unset)): event.channel = api_loop( self.client.api.channels_get, event.message.channel_id, ) if event.channel.is_dm: prefix = self.command_prefix else: prefix = self.prefixes.get(event.guild_id, None) if prefix is None: guild = handle_sql( db_session.query(guilds).filter_by( guild_id=event.guild_id, ).first, ) if guild is None: prefix = self.command_prefix self.prefixes[event.guild_id] = prefix guild = guilds( guild_id=event.guild_id, last_seen=datetime.now().isoformat(), prefix=prefix, ) handle_sql(db_session.add, guild) handle_sql(db_session.flush) else: preifx = guild.prefix self.prefixes[event.guild_id] = guild.prefix if event.message.content and event.message.content.startswith( prefix): prefix_len = len(prefix) if (len(event.message.content) > prefix_len and event.message.content[prefix_len] == " "): prefix_len += 1 message_dict = event.message.content[prefix_len:].split(" ") commands = [message_dict[0]] if len(message_dict) > 1: for word in message_dict[1:]: commands.append(commands[-1] + " " + word) commands = bot.triggers_set.intersection(commands) if commands: command = sorted(commands)[-1] event.args = event.message.content[prefix_len + len(command):].split() event.name = command event.msg = event.message try: bot.commands_dict[command].execute(event) except CommandError as e: api_loop(event.channel.send_message, str(e)) except Exception as e: self.exception_response(event, e)
def on_prefix_command(self, event, prefix=None): """ Miscellaneous Set a custom guild bot prefix (Manage Guild). This command will default to displaying the current prefix and ignore perms if no args are given. """ if not event.channel.is_dm: if prefix is None: guild = handle_sql( db_session.query(guilds).filter_by( guild_id=event.guild.id, ).first, ) if guild is None: prefix = self.command_prefix guild = guilds( guild_id=event.guild.id, last_seen=datetime.now().isoformat(), name=event.guild.name, ) handle_sql(db_session.add, guild) handle_sql(db_session.flush) else: prefix = guild.prefix return api_loop( event.channel.send_message, "Current prefix is ``{}``".format(prefix), ) user_perms = event.guild.get_member( event.author, ).permissions.to_dict() if user_perms["manage_guild"] or user_perms["administrator"]: if (event.guild.id not in self.cool_down["prefix"] or self.cool_down["prefix"][event.guild.id] <= time()): if handle_sql( db_session.query(guilds).filter_by( guild_id=event.guild.id).first, ) is None: guild = guilds( guild_id=event.guild.id, last_seen=datetime.now().isoformat(), name=event.guild.name, prefix=prefix, ) handle_sql(db_session.add, guild) else: handle_sql( db_session.query(guilds).filter_by( guild_id=event.guild.id).update, { "name": event.guild.name, "prefix": prefix }, ) handle_sql(db_session.flush) self.prefixes[event.guild.id] = prefix api_loop( event.channel.send_message, "Prefix changed to ``{}``".format(prefix), ) self.cool_down["prefix"][event.guild.id] = time() + 60 else: cooldown = self.cool_down["prefix"][event.guild.id] return api_loop( event.channel.send_message, "Cool down: {} seconds left.".format( round(Decimal(cooldown - time())), ), ) else: api_loop( event.channel.send_message, "You need to have the Guild Manage permission to use this command.", ) else: api_loop( event.channel.send_message, "This command can only be used in guilds.", )