From 52801b4de1d63cd01191acf7fcee137977140ec0 Mon Sep 17 00:00:00 2001 From: lexicade Date: Fri, 27 Jan 2023 21:06:30 +0000 Subject: Project init --- cogs/Wiki.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 cogs/Wiki.py (limited to 'cogs/Wiki.py') diff --git a/cogs/Wiki.py b/cogs/Wiki.py new file mode 100644 index 0000000..7552d07 --- /dev/null +++ b/cogs/Wiki.py @@ -0,0 +1,51 @@ +from discord.ext import commands +import discord +import wikipedia +import fuzzywuzzy +import importlib +import utils +importlib.reload(utils) + + +class Wiki(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.command(aliases=[], application_command_meta=commands.ApplicationCommandMeta(options=[])) + @commands.bot_has_permissions(embed_links=True) + async def wiki(self, ctx, wiki_search): + """Query Wikipedia.""" + try: + wiki_result = wikipedia.WikipediaPage(wiki_search) + # print(wiki_result.summary) + wiki_summary = wiki_result.summary[:1900] + wiki_url = wiki_result.url + + emb = await utils.embed(ctx, f"{wiki_search}", f"{wiki_summary}", url=wiki_url) + await ctx.send(embed=emb) + + except wikipedia.DisambiguationError as e: + emb = await utils.embed(ctx, f"{wiki_search} may refer to:", "\n".join(e.options)) + await ctx.send(embed=emb) + + except wikipedia.PageError as e: + search_suggestions = wikipedia.search(wiki_search) + + if len(search_suggestions) > 0: + emb = await utils.embed(ctx, f"No results found for {wiki_search}.\nDid you mean: ", + "\n".join(search_suggestions)) + await ctx.send(embed=emb) + else: + emb = await utils.embed(ctx, f"No results found for {wiki_search}.", "") + await ctx.send(embed=emb) + + + +def setup(bot): + print("INFO: Loading [Wiki]... ", end="") + bot.add_cog(Wiki(bot)) + print("Done!") + + +def teardown(bot): + print("INFO: Unloading [Wiki]") -- cgit v1.2.3