From 2714afc074d22cf9032b50a77733dd16e8c48756 Mon Sep 17 00:00:00 2001 From: gpkvt Date: Sat, 13 Aug 2022 08:30:09 +0200 Subject: [PATCH] Improved handling of missing config values --- tts.py | 60 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/tts.py b/tts.py index 9e0565d..52c92cc 100644 --- a/tts.py +++ b/tts.py @@ -77,8 +77,8 @@ class IRC: try: self.irc.send(bytes("JOIN " + channel + "\r\n", "UTF-8")) except ConnectionResetError: - logging.warning('JOIN was refused, will try again in 5 seconds.') - time.sleep(5) + logging.warning('JOIN was refused, will try again in 30 seconds.') + time.sleep(30) logging.warning('Please check your credentials, if this error persists.') self.irc.send(bytes("JOIN " + channel + "\r\n", "UTF-8")) @@ -535,40 +535,40 @@ def load_config(): for section in cfg: try: logging.debug('Fetching config: %s', section) - conf['IRC_CHANNEL'] = cfg['irc']['channel'] - conf['IRC_USERNAME'] = cfg['irc']['username'] - conf['IRC_OAUTH_TOKEN'] = cfg['irc']['oauth_token'] - conf['IRC_SERVER'] = cfg['irc']['server'] or "irc.chat.twitch.tv" - conf['IRC_CLEARMSG_TIMEOUT'] = cfg['irc']['clearmsg_timeout'] or 60 + conf['IRC_CHANNEL'] = cfg.get('irc', {}).get('channel', False) + conf['IRC_USERNAME'] = cfg.get('irc', {}).get('username', False) + conf['IRC_OAUTH_TOKEN'] = cfg.get('irc', {}).get('oauth_token', False) + conf['IRC_SERVER'] = cfg.get('irc', {}).get('server', "irc.chat.twitch.tv") + conf['IRC_CLEARMSG_TIMEOUT'] = cfg.get('irc', {}).get('clearmsg_timeout', 60) - conf['IRC_SUBONLY'] = cfg['bot']['subonly'] or False - conf['IRC_MODONLY'] = cfg['bot']['modonly'] or False - conf['IRC_TTS_LEN'] = cfg['bot']['message_length'] or 200 - conf['TTS_STARTENABLED'] = cfg['bot']['start_enabled'] or False + conf['IRC_SUBONLY'] = cfg.get('bot', {}).get('subonly', False) + conf['IRC_MODONLY'] = cfg.get('bot', {}).get('modonly', False) + conf['IRC_TTS_LEN'] = cfg.get('bot', {}).get('message_length', 200) + conf['TTS_STARTENABLED'] = cfg.get('bot', {}).get('start_enabled', False) - conf['LOG_LEVEL'] = cfg['log']['level'] or "INFO" - conf['HTTP_PORT'] = cfg['http']['port'] or 80 - conf['HTTP_BIND'] = cfg['http']['bind'] or "localhost" + conf['LOG_LEVEL'] = cfg.get('log', {}).get('level', "INFO") + conf['HTTP_PORT'] = cfg.get('http', {}).get('port', 80) + conf['HTTP_BIND'] = cfg.get('http', {}).get('bind', "localhost") conf['MESSAGE'] = {} - conf['MESSAGE']['TOFF'] = cfg['messages']['toff'] or "TTS is now disabled." - conf['MESSAGE']['TON'] = cfg['messages']['ton'] or "TTS is now active." - conf['MESSAGE']['TOO_LONG'] = cfg['messages']['too_long'] or "Sorry, your message is too long." - conf['MESSAGE']['DISABLED'] = cfg['messages']['disabled'] or "Sorry, TTS is disabled." - conf['MESSAGE']['DENIED'] = cfg['messages']['denied'] or "Sorry, you're not allowed to use TTS." - conf['MESSAGE']['SUBONLY'] = cfg['messages']['subonly'] or "Sorry, TTS is sub-only." - conf['MESSAGE']['MODONLY'] = cfg['messages']['modonly'] or "Sorry, TTS is mod-only." - conf['MESSAGE']['READY'] = cfg['messages']['ready'] or "TTS bot is ready." - conf['MESSAGE']['WHITELISTONLY'] = cfg['messages']['whitelist'] or False - conf['MESSAGE']['SAYS'] = cfg['messages']['says'] or "says" + conf['MESSAGE']['TOFF'] = cfg.get('messages', {}).get('toff', "TTS is now disabled.") + conf['MESSAGE']['TON'] = cfg.get('messages', {}).get('ton', "TTS is now active.") + conf['MESSAGE']['TOO_LONG'] = cfg.get('messages', {}).get('too_long', "Sorry, your message is too long.") + conf['MESSAGE']['DISABLED'] = cfg.get('messages', {}).get('disabled', "Sorry, TTS is disabled.") + conf['MESSAGE']['DENIED'] = cfg.get('messages', {}).get('denied', "Sorry, you're not allowed to use TTS.") + conf['MESSAGE']['SUBONLY'] = cfg.get('messages', {}).get('subonly', "Sorry, TTS is sub-only.") + conf['MESSAGE']['MODONLY'] = cfg.get('messages', {}).get('modonly', "Sorry, TTS is mod-only.") + conf['MESSAGE']['READY'] = cfg.get('messages', {}).get('ready', "TTS bot is ready.") + conf['MESSAGE']['WHITELISTONLY'] = cfg.get('messages', {}).get('whitelist', False) + conf['MESSAGE']['SAYS'] = cfg.get('messages', {}).get('says', "says") - conf['MESSAGE']['VOTESTART'] = cfg['messages']['votestart'] or "Quickvote started. Send #yourchoice to participate." - conf['MESSAGE']['VOTEEND'] = cfg['messages']['voteend'] or "Quickvote ended. The results are:" - conf['MESSAGE']['VOTENOBODY'] = cfg['messages']['votenobody'] or "Nobody casted a vote. :(" - conf['MESSAGE']['VOTERESULT'] = cfg['messages']['voteresult'] or "Voting has ended. The result is:" - conf['MESSAGE']['VOTES'] = cfg['messages']['votes'] or "Stimmen" + conf['MESSAGE']['VOTESTART'] = cfg.get('messages', {}).get('votestart', "Quickvote started. Send #yourchoice to participate.") + conf['MESSAGE']['VOTEEND'] = cfg.get('messages', {}).get('voteend', "Quickvote ended. The results are:") + conf['MESSAGE']['VOTENOBODY'] = cfg.get('messages', {}).get('votenobody', "Nobody casted a vote. :(") + conf['MESSAGE']['VOTERESULT'] = cfg.get('messages', {}).get('voteresult', "Voting has ended. The result is:") + conf['MESSAGE']['VOTES'] = cfg.get('messages', {}).get('votes', "Stimmen") - conf['USERMAP'] = cfg['usermapping'] or [] + conf['USERMAP'] = cfg.get('usermapping', []) if 'whitelist' in cfg: conf['WHITELIST'] = True