aboutsummaryrefslogtreecommitdiff
path: root/code/validate_config.py
blob: d4d978612d763a996598f4abaa3e0aa8e738ee19 (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
92
93
94
95
96
97
98
99
100
import configparser
import re

from global_variables import LOG


pattern_1 = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"
pattern_2 = "^([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"


def validate_config(file_contents):
    config = configparser.ConfigParser()
    config.read_string(file_contents)

    errors = 0

    try:
        # Validate TOKEN
        if not config["BOT_INFO"]["TOKEN"]:
            LOG.critical("TOKEN has not been set.")
            errors += 1
        # Validate BOT_COLOR
        if not config["BOT_INFO"]["BOT_COLOR"]:
            LOG.critical("BOT_COLOR has not been set.")
            errors += 1

        elif not bool(
            re.match(pattern_1, config["BOT_INFO"]["BOT_COLOR"])
        ) and not bool(re.match(pattern_2, config["BOT_INFO"]["BOT_COLOR"])):
            LOG.critical("BOT_COLOR is not a valid hex color.")
            errors += 1
        # Validate FEEDBACK_CHANNEL_ID
        if not config["BOT_INFO"]["FEEDBACK_CHANNEL_ID"]:
            LOG.critical("FEEDBACK_CHANNEL_ID has not been set.")
            errors += 1

        elif len(config["BOT_INFO"]["FEEDBACK_CHANNEL_ID"]) != 19:
            LOG.critical("FEEDBACK_CHANNEL_ID is not a valid Discord channel ID.")
            errors += 1
        # Validate BUG_CHANNEL_ID
        if not config["BOT_INFO"]["BUG_CHANNEL_ID"]:
            LOG.critical("BUG_CHANNEL_ID has not been set.")
            errors += 1

        elif len(config["BOT_INFO"]["BUG_CHANNEL_ID"]) != 19:
            LOG.critical("BUG_CHANNEL_ID is not a valid Discord channel ID.")
            errors += 1

        # Validate LAVALINK
        # Validate HOST
        if not config["LAVALINK"]["HOST"]:
            LOG.critical("HOST has not been set.")
            errors += 1
        # Validate PORT
        if not config["LAVALINK"]["PORT"]:
            LOG.critical("PORT has not been set.")
            errors += 1
        # Validate PASSWORD
        if not config["LAVALINK"]["PASSWORD"]:
            LOG.critical("HOST has not been set.")
            errors += 1

        if errors > 0:
            LOG.critical("Configuration checks failed. Correct your config.ini file and run again.")
            exit()

        else:
            LOG.info("Configuration checks passed. Starting bot.")

    except KeyError:
        LOG.critical(
            "You are missing at least one of the configuration options from your config.ini file. In order to regenerate this file with all of the proper options, please delete it and re-run the `bot.py` file."
        )
        exit()


def create_config():
    try:
        with open("config.ini", "r") as f:
            file_contents = f.read()
            validate_config(file_contents)

    except FileNotFoundError:
        config = configparser.ConfigParser()
        config["BOT_INFO"] = {
            "TOKEN": "",
            "BOT_COLOR": "",
            "FEEDBACK_CHANNEL_ID": "",
            "BUG_CHANNEL_ID": "",
        }

        config["LAVALINK"] = {"HOST": "", "PORT": "", "PASSWORD": ""}

        with open("config.ini", "w") as configfile:
            config.write(configfile)

        LOG.error(
            "Configuration file `config.ini` has been generated. Please fill out all of the necessary information. Refer to the docs for information on what a specific configuration option is."
        )
        exit()