summaryrefslogtreecommitdiff
path: root/src/main.py
blob: c773f4f34b7c6822c3a1db16ed87fbc892ab1c0b (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
52
53
54
55
56
57
58
59
60
61
62
63
64
from dotenv import dotenv_values
from discord.ext import commands

import discord
import os

import scrape_lexicanum

config = {
    **dotenv_values(".env"),
    **os.environ,
        }

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="Sigmar! ", intents=intents)

@bot.command()
async def ping(ctx):
    await ctx.send("pong")

@bot.command(name="Explain", aliases=["explain", "What's", "what's"])
async def explain(ctx, *args):
    args = list(args)
    if args[0].lower() == "whfb":
        args.pop(0)
        config["site"] = "https://whfb.lexicanum.com"
    elif args[0].lower() == "wh40k":
        args.pop(0)
        config["site"] = "https://wh40k.lexicanum.com"
    elif args[0].lower() == "aos":
        args.pop(0)
        config["site"] = "https://ageofsigmar.lexicanum.com"
    else:
        config["site"] = "https://ageofsigmar.lexicanum.com"
    
    query = " ".join([x.replace('"', "") for x in args])

    try:
        search_content = scrape_lexicanum.get_search_response(config, query)
        page_header, page_content, page_img_link = scrape_lexicanum.get_page_content(config, search_content[0])
        
        embed = discord.Embed(
                    title=page_header,
                    description=search_content.pop(0),
                    color=discord.Colour.blurple(),
                )

        string_results = "  ".join(str(x) for x in search_content)

        if page_img_link:
            embed.set_image(url=page_img_link)

        embed.add_field(name="Overview", value=page_content, inline=False)
        embed.add_field(name="You May Have Meant", value=string_results, inline=False)

        await ctx.send(embed=embed)
    except scrape_lexicanum.WikiError as e:
        await ctx.send(f"{e}")
    except Exception as e:
        print(f"Could not complete explanation: {e}")
        await ctx.send("Something has gone most terribly wrong...")

bot.run(config['token'])