summaryrefslogtreecommitdiff
path: root/cogs/Wiki.py
blob: 7552d07fd711cb10ee78e4e6e8f92f3b34315699 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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]")