Robust safesearch
This commit is contained in:
parent
94311dd499
commit
03f821c920
|
@ -7,6 +7,8 @@
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
import os
|
||||||
|
# import psutil
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
@ -85,6 +87,29 @@ class Developer(commands.Cog):
|
||||||
await ctx.send(':zzz: **Restarting.**')
|
await ctx.send(':zzz: **Restarting.**')
|
||||||
exit()
|
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):
|
async def cog_check(self, ctx):
|
||||||
return commands.is_owner()
|
return commands.is_owner()
|
||||||
|
|
||||||
|
|
|
@ -16,9 +16,32 @@ class Search(commands.Cog):
|
||||||
self.request = bot.request
|
self.request = bot.request
|
||||||
self.instances = bot.instances
|
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."""
|
"""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
|
# Choose an instance
|
||||||
if self.instances == []:
|
if self.instances == []:
|
||||||
with open('searxes.txt') as f:
|
with open('searxes.txt') as f:
|
||||||
|
@ -26,19 +49,22 @@ class Search(commands.Cog):
|
||||||
instance = random.sample(self.instances, k=1)[0]
|
instance = random.sample(self.instances, k=1)[0]
|
||||||
print(f"Attempting to use {instance}")
|
print(f"Attempting to use {instance}")
|
||||||
|
|
||||||
appinfo = await self.bot.application_info()
|
|
||||||
|
|
||||||
# Error Template
|
# Error Template
|
||||||
error_msg = ("**An error occured!**\n\n"
|
error_msg = ("**An error occured!**\n\n"
|
||||||
f"There was a problem with `{instance}`. Please try again later.\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
|
# Create the URL to make an API call to
|
||||||
call = f'{instance}/search?q={query}&format=json&language=en-US'
|
call = f'{instance}/search?q={query}&format=json&language=en-US'
|
||||||
|
|
||||||
# If a type is provided, add that type to the call URL
|
# If a type is provided, add that type to the call URL
|
||||||
if type:
|
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
|
# Make said API call
|
||||||
try:
|
try:
|
||||||
|
@ -59,6 +85,12 @@ class Search(commands.Cog):
|
||||||
else:
|
else:
|
||||||
amt = len(results)
|
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
|
# Header
|
||||||
msg = f"Showing **{amt}** results for `{query}`. \n\n"
|
msg = f"Showing **{amt}** results for `{query}`. \n\n"
|
||||||
# Expanded Result
|
# Expanded Result
|
||||||
|
@ -77,7 +109,7 @@ class Search(commands.Cog):
|
||||||
print(f"{e} with instance {instance}, trying again.")
|
print(f"{e} with instance {instance}, trying again.")
|
||||||
|
|
||||||
self.instances.remove(instance) # Weed the instance out
|
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
|
return msg
|
||||||
|
|
||||||
|
@ -90,7 +122,7 @@ class Search(commands.Cog):
|
||||||
|
|
||||||
# Handling
|
# Handling
|
||||||
async with ctx.typing():
|
async with ctx.typing():
|
||||||
msg = await self._search_logic(query)
|
msg = await self._search_logic(query, ctx.channel.is_nsfw())
|
||||||
await ctx.send(msg)
|
await ctx.send(msg)
|
||||||
|
|
||||||
@commands.Cog.listener()
|
@commands.Cog.listener()
|
||||||
|
@ -107,7 +139,7 @@ class Search(commands.Cog):
|
||||||
term = ctx.message.content.replace(ctx.prefix, '', 1)
|
term = ctx.message.content.replace(ctx.prefix, '', 1)
|
||||||
term = term.lstrip(' ')
|
term = term.lstrip(' ')
|
||||||
# Does search
|
# Does search
|
||||||
msg = await self._search_logic(term)
|
msg = await self._search_logic(term, ctx.channel.is_nsfw())
|
||||||
# Sends result
|
# Sends result
|
||||||
await ctx.send(msg)
|
await ctx.send(msg)
|
||||||
|
|
||||||
|
|
19
main.py
19
main.py
|
@ -56,12 +56,12 @@ class Bot(commands.Bot):
|
||||||
await super().start(*args, **kwargs)
|
await super().start(*args, **kwargs)
|
||||||
|
|
||||||
async def on_ready(self):
|
async def on_ready(self):
|
||||||
appinfo = await self.application_info()
|
self.appinfo = await self.application_info()
|
||||||
|
|
||||||
msg = "CONNECTED!\n"
|
msg = "CONNECTED!\n"
|
||||||
msg += "-----------------------------\n"
|
msg += "-----------------------------\n"
|
||||||
msg += f"ACCOUNT: {bot.user}\n"
|
msg += f"ACCOUNT: {bot.user}\n"
|
||||||
msg += f"OWNER: {appinfo.owner}\n"
|
msg += f"OWNER: {self.appinfo.owner}\n"
|
||||||
msg += "-----------------------------\n"
|
msg += "-----------------------------\n"
|
||||||
|
|
||||||
print(msg)
|
print(msg)
|
||||||
|
@ -92,6 +92,21 @@ bot = Bot(
|
||||||
case_insensitive=True)
|
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:** _<https://github.com/taciturasa/searchbot-discord>_\n"
|
||||||
|
msg += "**Support Server:** _<https://discord.gg/4BpReNV>_\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()
|
@bot.listen()
|
||||||
async def on_command_error(ctx, error):
|
async def on_command_error(ctx, error):
|
||||||
|
|
||||||
|
|
|
@ -3,5 +3,3 @@ https://search.nebulacentre.net/
|
||||||
https://searx.nakhan.net/
|
https://searx.nakhan.net/
|
||||||
https://searx.orcadian.net/
|
https://searx.orcadian.net/
|
||||||
https://searx.prvcy.eu/
|
https://searx.prvcy.eu/
|
||||||
https://searx.pwoss.org/
|
|
||||||
https://suche.elaon.de/
|
|
Loading…
Reference in New Issue