summaryrefslogtreecommitdiff
path: root/src/main.py
blob: c531faaf354e43faadd01d71c67428866c2438ba (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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.remove_command("help")

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

@bot.command(name="Help", aliases=["help"])
async def help(ctx):
    await ctx.send('''```Sigmar Bot - format Sigmar! [explain | what's | whats]
        Flags:
        - !aos: Age of Sigmar wiki [default]
        - !fantasy: WHFB wiki
        - !40k: WH40K wiki
        - !noimage: don't include image in embed
        - !nolinks: don't include 'see also' links in embed```
    ''')

@bot.command(name="Explain", aliases=["explain", "What's", "what's", "whats"])
async def explain(ctx, *args):
    config["site"] = "https://ageofsigmar.lexicanum.com"
    api_root = "/mediawiki"

    include_image = True
    include_links = True

    args = list(args)
    for arg in args[:]:
        if arg.startswith("!"):
            print(arg)
            match arg.lower():
                case "!fantasy":
                    config["site"] = "https://whfb.lexicanum.com"
                case "!40k":
                    config["site"] = "https://wh40k.lexicanum.com"
                case "!aos":
                    config["site"] = "https://ageofsigmar.lexicanum.com"
                case "!experimental-wod":
                    config["site"] = "https://whitewolf.fandom.com"
                    api_root = ""
                case "!noimage":
                    include_image = False
                case "!nolinks":
                    include_links = False
            args.remove(arg)

    query = " ".join([x.replace('"', "") for x in args])

    try:
        search_content = scrape_lexicanum.get_search_response(config, api_root, 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 and include_image:
            embed.set_image(url=page_img_link)

        embed.add_field(name="Overview", value=page_content, inline=False)

        if include_links:
            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'])