async def wordFilter_set_exact(self, ctx, *, new_filter=None): """Sets the exact filter.""" new_filter = await self.new_filter_format(ctx, new_filter) cursor.execute("UPDATE message_filter SET filterExact=? WHERE guild=?", (new_filter.replace(" ", ""), ctx.guild.id)) connection.commit() await ctx.send("Filter set.") current_filter = cursor.execute( "SELECT * from message_filter WHERE guild=?", (ctx.guild.id, )).fetchone() functions.update_filter(self.bot, current_filter)
async def wordFilter_toggle(self, ctx): """Toggles whether the filter is on or not.""" enabled = cursor.execute( "SELECT * FROM message_filter WHERE guild = ?", (ctx.guild.id, )).fetchone()[1] enabled = 1 if enabled == 0 else 0 cursor.execute("UPDATE message_filter SET enabled=? WHERE guild=?", (enabled, ctx.guild.id)) connection.commit() current_filter = cursor.execute( "SELECT * from message_filter WHERE guild=?", (ctx.guild.id, )).fetchone() functions.update_filter(self.bot, current_filter) await ctx.send( f"Filter now {'enabled' if enabled == 1 else 'disabled'}.")
async def wordFilter_remove(self, ctx, *words): """Removes specified words/phrases from filter. You can specify multiple words with spaces, to remove something that includes a space you must encase it in ". To remove a wildcard, prefix the word with `*`, for example `[p]filter remove *mario luigi` would remove mario from the wildcard filter and luigi from the exact. For example `[p]filter add "mario and luigi"` would remove `mario and luigi`""" guildFilter = cursor.execute( "SELECT * FROM message_filter WHERE guild = ?", (ctx.guild.id, )).fetchone() wildFilter = guildFilter[2].split(";") exactFilter = guildFilter[3].split(";") if not exactFilter: exactFilter = [] if not wildFilter: wildFilter = [] if "" in exactFilter: exactFilter.remove("") if "" in wildFilter: wildFilter.remove("") notFoundWords = [] for word in words: if word.startswith("*"): try: wildFilter.remove(word[1:]) except: notFoundWords.append(word) else: try: exactFilter.remove(word) except: notFoundWords.append(word) wildFilter = ";".join(wildFilter) exactFilter = ";".join(exactFilter) cursor.execute( "UPDATE message_filter SET filterWildCard=?, filterExact=? WHERE guild=?", (wildFilter, exactFilter, ctx.guild.id)) connection.commit() current_filter = cursor.execute( "SELECT * from message_filter WHERE guild=?", (ctx.guild.id, )).fetchone() functions.update_filter(self.bot, current_filter) await ctx.send( f"Removed from filter. {'The following words were not found so not removed: ' if notFoundWords else ''}{' '.join(notFoundWords) if notFoundWords else ''}" )
async def wordFilter_add(self, ctx, *words): """Adds specified words/phrases to filter. You can specify multiple words with spaces, to add something that includes a space you must encase it in ". To add a wildcard, prefix the word with `*`, for example `[p]filter add *mario luigi` would add mario to the wildcard filter and luigi to the exact. For example `[p]filter add "mario and luigi"` would filter `mario and luigi` only and not `mario`, `and` or `luigi` separately. Filter words must not contain characters other than letters or spaces and exact words cannot contain spaces.""" guildFilter = cursor.execute( "SELECT * FROM message_filter WHERE guild = ?", (ctx.guild.id, )).fetchone() wildFilter = guildFilter[2].split(";") exactFilter = guildFilter[3].split(";") if not exactFilter: exactFilter = [] if not wildFilter: wildFilter = [] if "" in exactFilter: exactFilter.remove("") if "" in wildFilter: wildFilter.remove("") for word in words: word = re.sub("[^\w *]|_", "", word) for w in word.split(";"): if len(word) == 1: continue if word.startswith("*"): wildFilter.append(word.replace("*", "")) else: exactFilter.append(word.replace(" ", "").replace("*", "")) wildFilter = ";".join(wildFilter) exactFilter = ";".join(exactFilter) cursor.execute( "UPDATE message_filter SET filterWildCard=?, filterExact=? WHERE guild=?", (wildFilter, exactFilter, ctx.guild.id)) connection.commit() current_filter = cursor.execute( "SELECT * from message_filter WHERE guild=?", (ctx.guild.id, )).fetchone() functions.update_filter(self.bot, current_filter) await ctx.send("Added to filter.")
) cursor.execute( "CREATE TABLE IF NOT EXISTS tags (guild INTEGER PRIMARY KEY, role INTEGER, tags TEXT NOT NULL)" ) cursor.execute( "CREATE TABLE IF NOT EXISTS name_filtering (guild INTEGER PRIMARY KEY, enabled INTEGER)" ) con.commit() #load filters into bot variable bot.guild_filters = {} filters = cursor.execute("SELECT * FROM message_filter").fetchall() filter_tuple = namedtuple("filter_tuple", ["enabled", "wildcard", "exact"]) for guild_filter in filters: functions.update_filter(bot, guild_filter) #startup @bot.event async def on_ready(): bot.startTime = time.time() bot.restart = False bot.args = sys.argv appinfo = await bot.application_info() logging.info(f"Bot started! Hello {str(appinfo.owner)}") logging.info(f"I'm connected as {str(bot.user)} - {bot.user.id}!") logging.info( f"In {len(bot.guilds)} guilds overlooking {len(list(bot.get_all_channels()))} channels and {len(list(bot.get_all_members()))} users." )