2021-01-23 23:40:49 +01:00
|
|
|
import discord
|
|
|
|
from discord.ext import commands
|
|
|
|
from decorators import help
|
|
|
|
from enums.help_categories import Category
|
|
|
|
from functions.scraping import google_search
|
|
|
|
|
|
|
|
|
|
|
|
class Google(commands.Cog):
|
|
|
|
def __init__(self, client):
|
|
|
|
self.client = client
|
|
|
|
|
|
|
|
# Don't allow any commands to work when locked
|
|
|
|
def cog_check(self, ctx):
|
|
|
|
return not self.client.locked
|
|
|
|
|
2021-01-24 13:08:58 +01:00
|
|
|
@commands.command(name="Google", aliases=["Gtfm", "Search"], usage="[Query]", case_insensitive=True)
|
2021-01-23 23:40:49 +01:00
|
|
|
@help.Category(Category.Other)
|
|
|
|
async def google(self, ctx, *query):
|
2021-01-24 13:08:58 +01:00
|
|
|
if not query:
|
|
|
|
return await ctx.reply("Je hebt geen query opgegeven.", mention_author=True)
|
|
|
|
|
2021-01-23 23:40:49 +01:00
|
|
|
results, status = google_search(" ".join(query))
|
|
|
|
|
|
|
|
if results is None:
|
|
|
|
return await ctx.send("Er ging iets fout (Response {})".format(status))
|
|
|
|
|
2021-01-24 13:14:44 +01:00
|
|
|
# Filter out all Nones
|
2021-01-24 13:08:58 +01:00
|
|
|
elements = list(filter(lambda x: x is not None, results))
|
|
|
|
|
|
|
|
embed = discord.Embed(colour=discord.Colour.blue())
|
|
|
|
embed.set_author(name="Google Search")
|
|
|
|
|
|
|
|
# Empty list of results
|
|
|
|
if len(elements) == 0:
|
|
|
|
embed.description = "Geen resultaten gevonden."
|
|
|
|
return await ctx.reply(embed=embed, mention_author=False)
|
|
|
|
|
2021-01-24 13:14:44 +01:00
|
|
|
# Cut excess results out
|
|
|
|
if len(elements) > 10:
|
|
|
|
elements = elements[:10]
|
|
|
|
|
2021-01-24 13:08:58 +01:00
|
|
|
links = []
|
|
|
|
|
|
|
|
for index, (link, title) in enumerate(elements):
|
|
|
|
links.append("{}: [{}]({})".format(index + 1, title, link))
|
|
|
|
|
|
|
|
embed.description = "\n".join(links)
|
|
|
|
|
|
|
|
await ctx.reply(embed=embed, mention_author=False)
|
|
|
|
|
2021-01-23 23:40:49 +01:00
|
|
|
|
|
|
|
def setup(client):
|
|
|
|
client.add_cog(Google(client))
|