aboutsummaryrefslogtreecommitdiff
path: root/code/validate_config.py
blob: ffcdb764ef1c2b008d137b5652fbf0a7e87fa740 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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 BONUS_COOLDOWN
        if not config['BOT_INFO']['BONUS_COOLDOWN']:
            LOG.critical("BONUS_COOLDOWN has not been set.")
            errors += 1

        else:
            try:
                int(config['BOT_INFO']['BONUS_COOLDOWN'])
            except ValueError:
                LOG.critical("BONUS_COOLDOWN must be an integer value.")
                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 BUG_CHANNEL_ID
        if not config['BOT_INFO']['BUG_CHANNEL_ID']:
            LOG.critical("BUG_CHANNEL_ID has not been set.")
            errors += 1

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

        else:
            try:
                int(config['BOT_INFO']['BUG_CHANNEL_ID'])
            except ValueError:
                LOG.critical("BUG_CHANNEL_ID should be an integer value, not a string.")
                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(str(config['BOT_INFO']['FEEDBACK_CHANNEL_ID'])) != 19:
            LOG.critical("FEEDBACK_CHANNEL_ID is not a valid Discord text channel ID.")
            errors += 1

        else:
            try:
                int(config['BOT_INFO']['FEEDBACK_CHANNEL_ID'])
            except ValueError:
                LOG.critical("FEEDBACK_CHANNEL_ID should be an integer value, not a string.")
                errors += 1
        # Validate API_KEY
        if not config['CRYPTO_COMPARE']['API_KEY']:
            LOG.critical("API_KEY has not been set.")
            errors += 1
        # Validate USERNAME
        if not config['POSTGRESQL']['USERNAME']:
            LOG.critical("USERNAME has not been set.")
            errors += 1
        # Validate PASSWORD
        if not config['POSTGRESQL']['PASSWORD']:
            LOG.critical("PASSWORD has not been set.")
            errors += 1
        # Validate HOST
        if not config['POSTGRESQL']['HOST']:
            LOG.critical("HOST has not been set.")
            errors += 1
        # Validate PORT
        if not config['POSTGRESQL']['PORT']:
            LOG.critical("PORT has not been set.")
            errors += 1
        # Validate DATABASE
        if not config['POSTGRESQL']['DATABASE']:
            LOG.critical("DATABASE has not been set.")
            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

        # Validate SPOTIFY
        # Validate CLIENT_ID
        if not config['SPOTIFY']['CLIENT_ID']:
            LOG.critical("CLIENT_ID has not been set.")
            errors += 1
        # Validate CLIENT_SECRET
        if not config['SPOTIFY']['CLIENT_SECRET']:
            LOG.critical("CLIENT_SECRET has not been set.")
            errors += 1

        if errors > 0:
            LOG.info(f"Program exiting with {errors} critical {'errors' if errors > 1 else 'error'}")
            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': '',
            'BONUS_COOLDOWN': '',
            'BOT_COLOR': '',
            'BUG_CHANNEL_ID': '',
            'FEEDBACK_CHANNEL_ID': ''
        }

        config['CRYPTO_COMPARE'] = {
            'API_KEY': ''
        }

        config['POSTGRESQL'] = {
            'USERNAME': '',
            'PASSWORD': '',
            'HOST': '',
            'PORT': '',
            'DATABASE': ''
        }

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

        config['SPOTIFY'] = {
            'CLIENT_ID': '',
            'CLIENT_SECRET': ''
        }

        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()