Added check for config values

This commit is contained in:
gpkvt 2022-08-11 10:26:33 +02:00
parent b27b51b52f
commit 3ae4eecd04
1 changed files with 35 additions and 22 deletions

57
tts.py
View File

@ -302,31 +302,32 @@ def load_config():
for section in cfg: for section in cfg:
try: try:
logging.debug('Fetching config: '+str(section)) logging.debug('Fetching config: '+str(section))
conf['IRC_CHANNEL'] = cfg['irc']['channel']
conf['IRC_USERNAME'] = cfg['irc']['username'] conf['IRC_CHANNEL'] = cfg['irc']['channel'] or False
conf['IRC_OAUTH_TOKEN'] = cfg['irc']['oauth_token'] conf['IRC_USERNAME'] = cfg['irc']['username'] or False
conf['IRC_SERVER'] = cfg['irc']['server'] conf['IRC_OAUTH_TOKEN'] = cfg['irc']['oauth_token'] or False
conf['IRC_CLEARMSG_TIMEOUT'] = cfg['irc']['clearmsg_timeout'] conf['IRC_SERVER'] = cfg['irc']['server'] or "irc.chat.twitch.tv"
conf['IRC_CLEARMSG_TIMEOUT'] = cfg['irc']['clearmsg_timeout'] or 10
conf['IRC_SUBONLY'] = cfg['bot']['subonly'] conf['IRC_SUBONLY'] = cfg['bot']['subonly'] or False
conf['IRC_MODONLY'] = cfg['bot']['modonly'] conf['IRC_MODONLY'] = cfg['bot']['modonly'] or False
conf['IRC_TTS_LEN'] = cfg['bot']['message_length'] conf['IRC_TTS_LEN'] = cfg['bot']['message_length'] or 200
conf['LOG_LEVEL'] = cfg['log']['level'] conf['LOG_LEVEL'] = cfg['log']['level'] or "INFO"
conf['HTTP_PORT'] = cfg['http']['port'] conf['HTTP_PORT'] = cfg['http']['port'] or 80
conf['HTTP_BIND'] = cfg['http']['bind'] conf['HTTP_BIND'] = cfg['http']['bind'] or "localhost"
conf['MESSAGE'] = {} conf['MESSAGE'] = {}
conf['MESSAGE']['TOO_LONG'] = cfg['messages']['too_long'] conf['MESSAGE']['TOO_LONG'] = cfg['messages']['too_long'] or "Sorry, your message is too long"
conf['MESSAGE']['DISABLED'] = cfg['messages']['disabled'] conf['MESSAGE']['DISABLED'] = cfg['messages']['disabled'] or "Sorry, TTS is disabled."
conf['MESSAGE']['DENIED'] = cfg['messages']['denied'] conf['MESSAGE']['DENIED'] = cfg['messages']['denied'] or "Sorry, you're not allowed to use TTS."
conf['MESSAGE']['SUBONLY'] = cfg['messages']['subonly'] conf['MESSAGE']['SUBONLY'] = cfg['messages']['subonly'] or "Sorry, TTS is sub-only."
conf['MESSAGE']['MODONLY'] = cfg['messages']['modonly'] conf['MESSAGE']['MODONLY'] = cfg['messages']['modonly'] or "Sorry, TTS is mod-only."
conf['MESSAGE']['READY'] = cfg['messages']['ready'] conf['MESSAGE']['READY'] = cfg['messages']['ready'] or "TTS bot is ready."
conf['MESSAGE']['WHITELISTONLY'] = cfg['messages']['whitelist'] conf['MESSAGE']['WHITELISTONLY'] = cfg['messages']['whitelist'] or False
conf['MESSAGE']['SAYS'] = cfg['messages']['says'] conf['MESSAGE']['SAYS'] = cfg['messages']['says'] or "says"
conf['USERMAP'] = cfg['usermapping'] conf['USERMAP'] = cfg['usermapping'] or []
if 'whitelist' in cfg: if 'whitelist' in cfg:
conf['WHITELIST'] = True conf['WHITELIST'] = True
@ -334,7 +335,7 @@ def load_config():
else: else:
conf['WHITELIST'] = False conf['WHITELIST'] = False
except KeyError as e: except KeyError:
logging.exception('Your config file is invalid, please check and try again.') logging.exception('Your config file is invalid, please check and try again.')
sys.exit(254) sys.exit(254)
@ -343,6 +344,15 @@ def load_config():
logging.debug('Whitelist:') logging.debug('Whitelist:')
logging.debug(conf['WHITELIST_USER']) logging.debug(conf['WHITELIST_USER'])
if not conf['IRC_CHANNEL']:
raise ValueError('Please add your twitch channel to config.yml.')
if not conf['IRC_USERNAME']:
raise ValueError('Please add the bots username to config.yml.')
if not conf['IRC_OAUTH_TOKEN']:
raise ValueError('Please add the bots oauth-token to config.yml.')
if not conf['IRC_OAUTH_TOKEN'].startswith('oauth:'):
raise ValueError('Your oauth-token is invalid, it has to start with: "oauth:"')
return conf return conf
conf = {} conf = {}
@ -351,12 +361,15 @@ msg_queue_raw = []
msg_queue = {} msg_queue = {}
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(module)s %(threadName)s %(levelname)s: %(message)s') logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(module)s %(threadName)s %(levelname)s: %(message)s')
sys.tracebacklimit = 0
def main(): def main():
conf = load_config() conf = load_config()
lastreload = datetime.datetime.now() lastreload = datetime.datetime.now()
logging.getLogger().setLevel(conf['LOG_LEVEL']) logging.getLogger().setLevel(conf['LOG_LEVEL'])
if conf['LOG_LEVEL'] == 'DEBUG':
sys.tracebacklimit = 5
logging.info("Starting Webserver") logging.info("Starting Webserver")
httpd = socketserver.TCPServer((conf['HTTP_BIND'], conf['HTTP_PORT']), HTTPserv) httpd = socketserver.TCPServer((conf['HTTP_BIND'], conf['HTTP_PORT']), HTTPserv)
httpd.allow_reuse_port = True httpd.allow_reuse_port = True