From 03f821c9201285ab88abf68c7b0613d026046eb8 Mon Sep 17 00:00:00 2001 From: Adriene Hutchins Date: Thu, 27 Feb 2020 14:09:44 -0500 Subject: [PATCH] Robust safesearch --- extensions/developer.py | 25 +++++++++++++++++++++ extensions/search.py | 48 ++++++++++++++++++++++++++++++++++------- main.py | 19 ++++++++++++++-- searxes.txt | 4 +--- 4 files changed, 83 insertions(+), 13 deletions(-) diff --git a/extensions/developer.py b/extensions/developer.py index c913159..a511b55 100644 --- a/extensions/developer.py +++ b/extensions/developer.py @@ -7,6 +7,8 @@ import discord from discord.ext import commands +import os +# import psutil import aiohttp import random @@ -85,6 +87,29 @@ class Developer(commands.Cog): await ctx.send(':zzz: **Restarting.**') exit() +# @commands.command() +# async def stats(self, ctx): +# mem = psutil.virtual_memory() +# currproc = psutil.Process(os.getpid()) +# total_ram = self.humanbytes(mem[0]) +# available_ram = self.humanbytes(mem[1]) +# usage = self.humanbytes(currproc.memory_info().rss) +# text = f""" +# ``` +# Total RAM: {total_ram} +# Available RAM: {available_ram} +# RAM used by bot: {usage} +# Number of bot commands: {len(ctx.bot.commands)} +# Number of extensions present: {len(ctx.bot.cogs)} +# Number of users: {len(ctx.bot.users)} +# ``` +# """ +# await ctx.send(text) + + @commands.command(hidden=True) + async def error(self, ctx): + 3 / 0 + async def cog_check(self, ctx): return commands.is_owner() diff --git a/extensions/search.py b/extensions/search.py index ebfbddf..472ce94 100644 --- a/extensions/search.py +++ b/extensions/search.py @@ -16,9 +16,32 @@ class Search(commands.Cog): self.request = bot.request self.instances = bot.instances - async def _search_logic(self, query: str, type: str = None): + async def _search_logic(self, query: str, is_nsfw: bool = False, type: str = None): """Provides search logic for all search commands.""" + # WARNING - This list includes slurs. + nono_words = [ + 'tranny', 'faggot', 'fag', + 'porn', 'cock', 'dick', + 'titty', 'boob', 'penis', + 'slut', 'cum', 'jizz', + 'semen', 'cooch', 'coochie', + 'pussy', 'penis', 'fetish', + 'bdsm' + ] + nono_sites = [ + 'xvideos', 'pornhub' + ] + + if not is_nsfw: + for i in nono_words: + if i in query: + return ( + "**Sorry!** That query included language " + "we cannot accept in a non-NSFW channel. " + "Please try again in an NSFW channel." + ) + # Choose an instance if self.instances == []: with open('searxes.txt') as f: @@ -26,19 +49,22 @@ class Search(commands.Cog): instance = random.sample(self.instances, k=1)[0] print(f"Attempting to use {instance}") - appinfo = await self.bot.application_info() - # Error Template error_msg = ("**An error occured!**\n\n" f"There was a problem with `{instance}`. Please try again later.\n" - f"_If problems with this instance persist, contact`{appinfo.owner}` to have it removed._") + f"_If problems with this instance persist, contact`{self.bot.appinfo.owner}` to have it removed._") # Create the URL to make an API call to call = f'{instance}/search?q={query}&format=json&language=en-US' # If a type is provided, add that type to the call URL if type: - call += f'?type={type}' + call += f'&type={type}' + + if is_nsfw: + call += f'&safesearch=0' + else: + call += f'&safesearch=1' # Make said API call try: @@ -59,6 +85,12 @@ class Search(commands.Cog): else: amt = len(results) + if not is_nsfw: + for r in results[0:7]: + for n in nono_sites: + if n in r['url']: + results.remove(r) + # Header msg = f"Showing **{amt}** results for `{query}`. \n\n" # Expanded Result @@ -77,7 +109,7 @@ class Search(commands.Cog): print(f"{e} with instance {instance}, trying again.") self.instances.remove(instance) # Weed the instance out - return await self._search_logic(query) # Recurse until good response + return await self._search_logic(query, is_nsfw) # Recurse until good response return msg @@ -90,7 +122,7 @@ class Search(commands.Cog): # Handling async with ctx.typing(): - msg = await self._search_logic(query) + msg = await self._search_logic(query, ctx.channel.is_nsfw()) await ctx.send(msg) @commands.Cog.listener() @@ -107,7 +139,7 @@ class Search(commands.Cog): term = ctx.message.content.replace(ctx.prefix, '', 1) term = term.lstrip(' ') # Does search - msg = await self._search_logic(term) + msg = await self._search_logic(term, ctx.channel.is_nsfw()) # Sends result await ctx.send(msg) diff --git a/main.py b/main.py index 0c31620..2786189 100644 --- a/main.py +++ b/main.py @@ -56,12 +56,12 @@ class Bot(commands.Bot): await super().start(*args, **kwargs) async def on_ready(self): - appinfo = await self.application_info() + self.appinfo = await self.application_info() msg = "CONNECTED!\n" msg += "-----------------------------\n" msg += f"ACCOUNT: {bot.user}\n" - msg += f"OWNER: {appinfo.owner}\n" + msg += f"OWNER: {self.appinfo.owner}\n" msg += "-----------------------------\n" print(msg) @@ -92,6 +92,21 @@ bot = Bot( case_insensitive=True) +@bot.command(aliases=['info', 'source', 'server']) +async def about(ctx): + '''Returns information about this bot.''' + appinfo = await bot.application_info() + + msg = f"**{bot.description}**\n" + msg += f"Created by **taciturasa#4365**, this instance by **{appinfo.owner}.**\n\n" + msg += "**Source Code:** __\n" + msg += "**Support Server:** __\n" + msg += "_Note: Please attempt to contact the hoster of any separate instances before this server._\n\n" + msg += f"_See **{ctx.prefix}** `help` for help, and `stats` for statistics._" + + await ctx.send(msg) + + @bot.listen() async def on_command_error(ctx, error): diff --git a/searxes.txt b/searxes.txt index b7bed3c..9104cab 100644 --- a/searxes.txt +++ b/searxes.txt @@ -2,6 +2,4 @@ https://haku.ahmia.fi/ https://search.nebulacentre.net/ https://searx.nakhan.net/ https://searx.orcadian.net/ -https://searx.prvcy.eu/ -https://searx.pwoss.org/ -https://suche.elaon.de/ \ No newline at end of file +https://searx.prvcy.eu/ \ No newline at end of file