From c65e3b86b1a1861911a78d4780f93ef9fd3293e6 Mon Sep 17 00:00:00 2001 From: gpkvt Date: Sat, 27 Aug 2022 14:04:04 +0200 Subject: [PATCH] Renamed conf variable --- tts.py | 322 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 161 insertions(+), 161 deletions(-) diff --git a/tts.py b/tts.py index 63ff883..073b382 100644 --- a/tts.py +++ b/tts.py @@ -1,6 +1,6 @@ #!/usr/bin/python3 # -*- coding: utf-8 -*- -# pylint: disable=line-too-long,too-many-lines +# pylint: disable=bare-except """ TwitchTTS @@ -53,7 +53,7 @@ class IRC: self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.tts_denied = [] self.tts_allowed = [] - self.tts_status = conf['TTS_STARTENABLED'] + self.tts_status = CONF['TTS_STARTENABLED'] self.quickvote_status = False self.pick_status = False self.votemsg = False @@ -63,8 +63,8 @@ class IRC: self.picknumber = 1 self.pickcount = 0 - if 'WHITELIST_USER' in conf: - self.tts_allowed = conf['WHITELIST_USER'] + if 'WHITELIST_USER' in CONF: + self.tts_allowed = CONF['WHITELIST_USER'] def connect(self, server, port, channel, botnick, botpass): """ Connect to Twitch IRC servers """ @@ -179,12 +179,12 @@ class IRC: self.Commands.tts(self, message, tags) return - if msg.startswith('!wiki') and conf['FEATURE']['WIKI']: + if msg.startswith('!wiki') and CONF['FEATURE']['WIKI']: logging.debug("!wiki command detected") self.Commands.wiki(self, tags, msg) return - if conf['FEATURE']['QUOTE']: + if CONF['FEATURE']['QUOTE']: if msg.startswith('!addquote'): logging.debug("!addquote command detected") self.Commands.addquote(self, tags, msg) @@ -237,23 +237,23 @@ class IRC: user = tags['user'] if 'broadcaster' in badges or 'moderator' in badges: - if msg.startswith('!ping') and conf['FEATURE']['PING']: + if msg.startswith('!ping') and CONF['FEATURE']['PING']: logging.debug("Ping check received.") - self.sendmsg(conf['IRC_CHANNEL'], "@"+str(user), "Pong!") + self.sendmsg(CONF['IRC_CHANNEL'], "@"+str(user), "Pong!") - elif msg.startswith('!version') and conf['FEATURE']['VERSION']: + elif msg.startswith('!version') and CONF['FEATURE']['VERSION']: logging.debug("!version command detected") - self.sendmsg(conf['IRC_CHANNEL'], "@"+str(user), VERSION) + self.sendmsg(CONF['IRC_CHANNEL'], "@"+str(user), VERSION) - elif msg.startswith('!pick') and conf['FEATURE']['PICK']: + elif msg.startswith('!pick') and CONF['FEATURE']['PICK']: logging.debug("!pick command detected") self.Commands.pick(self, msg) - elif msg.startswith('!random') and conf['FEATURE']['RANDOM']: + elif msg.startswith('!random') and CONF['FEATURE']['RANDOM']: logging.info('!random command detected') self.Commands.random(self, msg) - elif msg.startswith('!quickvote') and conf['FEATURE']['QUOTE']: + elif msg.startswith('!quickvote') and CONF['FEATURE']['QUOTE']: logging.info("!quickvote command detected") self.Commands.quickvote(self, msg) @@ -278,19 +278,19 @@ class IRC: msg_queue.clear() msg_queue_raw.clear() self.tts_status = False - self.sendmsg(conf['IRC_CHANNEL'], "@"+str(user), conf['MESSAGE']['TOFF']) + self.sendmsg(CONF['IRC_CHANNEL'], "@"+str(user), CONF['MESSAGE']['TOFF']) elif msg.startswith('!ton'): logging.info('TTS is now turned on') msg_queue.clear() msg_queue_raw.clear() self.tts_status = True - self.sendmsg(conf['IRC_CHANNEL'], "@"+str(user), conf['MESSAGE']['TON']) + self.sendmsg(CONF['IRC_CHANNEL'], "@"+str(user), CONF['MESSAGE']['TON']) def check_subonly(self, tags): """ subonly """ - if not conf['IRC_SUBONLY']: + if not CONF['IRC_SUBONLY']: return False subscriber = tags['subscriber'] @@ -302,13 +302,13 @@ class IRC: return False logging.debug('TTS is sub-only') - self.sendmsg(conf['IRC_CHANNEL'], "@"+str(user), conf['MESSAGE']['SUBONLY']) + self.sendmsg(CONF['IRC_CHANNEL'], "@"+str(user), CONF['MESSAGE']['SUBONLY']) return True def check_modonly(self, tags): """ modonly """ - if not conf['IRC_MODONLY']: + if not CONF['IRC_MODONLY']: return False badges = tags['badges'] @@ -319,13 +319,13 @@ class IRC: return False logging.debug('TTS is mod-only') - self.sendmsg(conf['IRC_CHANNEL'], "@"+str(user), conf['MESSAGE']['MODONLY']) + self.sendmsg(CONF['IRC_CHANNEL'], "@"+str(user), CONF['MESSAGE']['MODONLY']) return True def check_tts_disabled(self, user): """ Check if TTS is disabled """ if not self.tts_status: - self.sendmsg(conf['IRC_CHANNEL'], "@"+str(user), conf['MESSAGE']['DISABLED']) + self.sendmsg(CONF['IRC_CHANNEL'], "@"+str(user), CONF['MESSAGE']['DISABLED']) return True logging.debug('TTS is enabled') @@ -334,8 +334,8 @@ class IRC: def check_msg_too_long(self, message, user): """ Check if message is too long """ - if message['length'] > conf['IRC_TTS_LEN']: - self.sendmsg(conf['IRC_CHANNEL'], "@"+str(user), conf['MESSAGE']['TOO_LONG']) + if message['length'] > CONF['IRC_TTS_LEN']: + self.sendmsg(CONF['IRC_CHANNEL'], "@"+str(user), CONF['MESSAGE']['TOO_LONG']) return True logging.debug('Check length: Message is ok') @@ -345,7 +345,7 @@ class IRC: """ Check if user is on denied list """ if user in self.tts_denied: logging.info("%s is not allowed to use TTS", user) - self.sendmsg(conf['IRC_CHANNEL'], "@"+str(user), conf['MESSAGE']['DENIED']) + self.sendmsg(CONF['IRC_CHANNEL'], "@"+str(user), CONF['MESSAGE']['DENIED']) return True logging.debug("%s is allowed to use TTS", user) @@ -354,12 +354,12 @@ class IRC: def check_whitelist(self, user): """ Check Whitelist """ - if conf['WHITELIST']: + if CONF['WHITELIST']: if user not in self.tts_allowed: logging.debug("tts_allowed: %s", self.tts_allowed) self.sendmsg( - conf['IRC_CHANNEL'], - "@"+str(user), conf['MESSAGE']['WHITELISTONLY'] + CONF['IRC_CHANNEL'], + "@"+str(user), CONF['MESSAGE']['WHITELISTONLY'] ) return False return True @@ -491,8 +491,8 @@ class IRC: date = time.strftime("%d.%m.%Y") try: - token = conf['IRC_OAUTH_TOKEN'].replace('oauth:','') - login = conf['IRC_CHANNEL'].replace('#','') + token = CONF['IRC_OAUTH_TOKEN'].replace('oauth:','') + login = CONF['IRC_CHANNEL'].replace('#','') api_endpoint = "https://api.twitch.tv/helix/users?login="+str(login) headers = { 'Content-type': 'application/x-form-urlencoded', @@ -514,7 +514,7 @@ class IRC: game = data['data'][0]['game_name'] quote = f"#{nol}: \"{quote[1]}\" -{username}/{game} ({date})\n" - except: # pylint: disable=bare-except + except: logging.warning('Could not get metadata for quote') quote = f"#{nol}: \"{quote[1]}\" -{username} ({date})\n" @@ -522,7 +522,7 @@ class IRC: with open("quotes.txt", "ab") as file: file.write(quote.encode('utf-8')) - msg = f"{conf['MESSAGE']['QUOTE_ADDED_PREFIX']} #{nol} {conf['MESSAGE']['QUOTE_ADDED_SUFFIX']}" + msg = f"{CONF['MESSAGE']['QUOTE_ADDED_PREFIX']} #{nol} {CONF['MESSAGE']['QUOTE_ADDED_SUFFIX']}" raw_msg = { "TTS": True, @@ -530,25 +530,25 @@ class IRC: "badges": True, "subscriber": True, "msgid": True, - "user": conf['IRC_USERNAME'], - "length": conf['IRC_TTS_LEN'], + "user": CONF['IRC_USERNAME'], + "length": CONF['IRC_TTS_LEN'], "queuetime": datetime.datetime.now(), "timestamp": str(time.time_ns()) } msg_queue[raw_msg['timestamp']] = [raw_msg['user'], raw_msg['msg']] - IRC.sendmsg(self, conf['IRC_CHANNEL'], "@"+str(user), msg) + IRC.sendmsg(self, CONF['IRC_CHANNEL'], "@"+str(user), msg) def wiki(self, tags, msg): """ !wiki command """ try: user = tags['user'] - wikipedia.set_lang(conf['WIKI_LANG']) + wikipedia.set_lang(CONF['WIKI_LANG']) msg = msg.replace('!wiki', '').strip() wikiresult = wikipedia.summary(msg, sentences=3) - IRC.sendmsg(self, conf['IRC_CHANNEL'], "@"+str(user), wikiresult) - IRC.sendmsg(self, conf['IRC_CHANNEL'], "@"+str(user), wikipedia.page(msg).url) + IRC.sendmsg(self, CONF['IRC_CHANNEL'], "@"+str(user), wikiresult) + IRC.sendmsg(self, CONF['IRC_CHANNEL'], "@"+str(user), wikipedia.page(msg).url) wikiresult = wikiresult.replace('==', '') @@ -559,16 +559,16 @@ class IRC: "subscriber": True, "msgid": True, "user": 'wikipedia', - "length": conf['IRC_TTS_LEN'], + "length": CONF['IRC_TTS_LEN'], "queuetime": datetime.datetime.now(), "timestamp": str(time.time_ns()) } msg_queue[raw_msg['timestamp']] = [raw_msg['user'], raw_msg['msg']] except wikipedia.exceptions.DisambiguationError: - IRC.sendmsg(self, conf['IRC_CHANNEL'], "@"+str(user), conf['MESSAGE']['WIKI_TOO_MANY']) - except: # pylint: disable=bare-except - IRC.sendmsg(self, conf['IRC_CHANNEL'], "@"+str(user), conf['MESSAGE']['WIKI_NO_RESULT']) + IRC.sendmsg(self, CONF['IRC_CHANNEL'], "@"+str(user), CONF['MESSAGE']['WIKI_TOO_MANY']) + except: + IRC.sendmsg(self, CONF['IRC_CHANNEL'], "@"+str(user), CONF['MESSAGE']['WIKI_NO_RESULT']) def quote(self, tags, msg = False): """ !smartquote command @@ -627,8 +627,8 @@ class IRC: if not 'quote' in vars(): logging.info('No quote found.') - quote = conf['MESSAGE']['QUOTE_NOT_FOUND'] - IRC.sendmsg(self, conf['IRC_CHANNEL'], "", quote) + quote = CONF['MESSAGE']['QUOTE_NOT_FOUND'] + IRC.sendmsg(self, CONF['IRC_CHANNEL'], "", quote) return False if not isinstance(quote, str): @@ -647,7 +647,7 @@ class IRC: else: logging.info('Sending quote to TTS') logging.debug("Quote: %s", quote) - IRC.sendmsg(self, conf['IRC_CHANNEL'], "", quote) + IRC.sendmsg(self, CONF['IRC_CHANNEL'], "", quote) quote = quote.rsplit('(', 1)[0] @@ -657,8 +657,8 @@ class IRC: "badges": True, "subscriber": True, "msgid": True, - "user": conf['IRC_USERNAME'], - "length": conf['IRC_TTS_LEN'], + "user": CONF['IRC_USERNAME'], + "length": CONF['IRC_TTS_LEN'], "queuetime": datetime.datetime.now(), "timestamp": str(time.time_ns()) } @@ -676,7 +676,7 @@ class IRC: try: delay = msg.split(' ')[1] - except: # pylint: disable=bare-except + except: delay = False if delay: @@ -703,7 +703,7 @@ class IRC: splitmsg = msg.split(" ") username, *mappingname = splitmsg mappingname = ' '.join(mappingname) - except: # pylint: disable=bare-except + except: username = False mappingname = False @@ -734,36 +734,36 @@ class IRC: converted_picks = [str(element) for element in picks] joined_picks = " ".join(converted_picks) - except: # pylint: disable=bare-except + except: logging.error("There was an error during picking.") joined_picks = False if joined_picks: raw_msg = { "TTS": True, - "msg": conf['MESSAGE']['PICKRESULT'] +" "+ str(joined_picks), + "msg": CONF['MESSAGE']['PICKRESULT'] +" "+ str(joined_picks), "badges": True, "subscriber": True, "msgid": True, - "user": conf['IRC_USERNAME'], - "length": conf['IRC_TTS_LEN'], + "user": CONF['IRC_USERNAME'], + "length": CONF['IRC_TTS_LEN'], "queuetime": datetime.datetime.now(), "timestamp": str(time.time_ns()) } msg_queue[raw_msg['timestamp']] = [raw_msg['user'], raw_msg['msg']] IRC.sendmsg(self, - conf['IRC_CHANNEL'], "", - conf['MESSAGE']['PICKRESULT'] + CONF['IRC_CHANNEL'], "", + CONF['MESSAGE']['PICKRESULT'] ) IRC.sendmsg(self, - conf['IRC_CHANNEL'], "*", + CONF['IRC_CHANNEL'], "*", joined_picks ) else: IRC.sendmsg(self, - conf['IRC_CHANNEL'], "*", - conf['MESSAGE']['PICKNONE'] + CONF['IRC_CHANNEL'], "*", + CONF['MESSAGE']['PICKNONE'] ) self.pick_status = False @@ -783,8 +783,8 @@ class IRC: logging.info("Will pick %s participants", self.picknumber) IRC.sendmsg(self, - conf['IRC_CHANNEL'], "@chat", - conf['MESSAGE']['PICKSTART'] + CONF['IRC_CHANNEL'], "@chat", + CONF['MESSAGE']['PICKSTART'] ) return @@ -802,23 +802,23 @@ class IRC: if self.pollcount == 0: logging.info("Nobody voted") - IRC.sendmsg(self, conf['IRC_CHANNEL'], "@chat", conf['MESSAGE']['VOTEEND']) - IRC.sendmsg(self, conf['IRC_CHANNEL'], "*", conf['MESSAGE']['VOTENOBODY']) + IRC.sendmsg(self, CONF['IRC_CHANNEL'], "@chat", CONF['MESSAGE']['VOTEEND']) + IRC.sendmsg(self, CONF['IRC_CHANNEL'], "*", CONF['MESSAGE']['VOTENOBODY']) raw_msg = { "TTS": True, - "msg": conf['MESSAGE']['VOTENOBODY'], + "msg": CONF['MESSAGE']['VOTENOBODY'], "badges": True, "subscriber": True, "msgid": True, - "user": conf['IRC_USERNAME'], - "length": conf['IRC_TTS_LEN'], + "user": CONF['IRC_USERNAME'], + "length": CONF['IRC_TTS_LEN'], "queuetime": datetime.datetime.now(), "timestamp": str(time.time_ns()) } msg_queue[raw_msg['timestamp']] = [raw_msg['user'], raw_msg['msg']] - logging.info('The result is: %s', conf['MESSAGE']['VOTENOBODY']) + logging.info('The result is: %s', CONF['MESSAGE']['VOTENOBODY']) logging.debug('Votemsg: %s', msg) self.quickvote_status = False @@ -828,31 +828,31 @@ class IRC: logging.info("Counting votes") count = 0 count = Counter(self.poll.values()).most_common(5) - IRC.sendmsg(self, conf['IRC_CHANNEL'], "@chat", conf['MESSAGE']['VOTEEND']) + IRC.sendmsg(self, CONF['IRC_CHANNEL'], "@chat", CONF['MESSAGE']['VOTEEND']) logging.debug(count) raw_msg = { "TTS": True, - "msg": conf['MESSAGE']['VOTERESULT'] +" "+ str(count[0][0].replace('#','')), + "msg": CONF['MESSAGE']['VOTERESULT'] +" "+ str(count[0][0].replace('#','')), "badges": True, "subscriber": True, "msgid": True, - "user": conf['IRC_USERNAME'], - "length": conf['IRC_TTS_LEN'], + "user": CONF['IRC_USERNAME'], + "length": CONF['IRC_TTS_LEN'], "queuetime": datetime.datetime.now(), "timestamp": str(time.time_ns()) } msg_queue[raw_msg['timestamp']] = [raw_msg['user'], raw_msg['msg']] - logging.info('The result is: %s', conf['MESSAGE']['VOTERESULT'] +" "+ str(count[0])) + logging.info('The result is: %s', CONF['MESSAGE']['VOTERESULT'] +" "+ str(count[0])) logging.debug('Votemsg: %s', msg) for key, value in count: IRC.sendmsg( self, - conf['IRC_CHANNEL'], "*", - str(key)+" ("+str(value)+ " "+ conf['MESSAGE']['VOTES'] + ")" + CONF['IRC_CHANNEL'], "*", + str(key)+" ("+str(value)+ " "+ CONF['MESSAGE']['VOTES'] + ")" ) self.quickvote_status = False @@ -865,11 +865,11 @@ class IRC: self.votemsg = msg.split('!quickvote', 1)[1].strip() if self.votemsg: IRC.sendmsg(self, - conf['IRC_CHANNEL'], "@chat", - conf['MESSAGE']['VOTESTART'] + " (" + str(self.votemsg) + ")" + CONF['IRC_CHANNEL'], "@chat", + CONF['MESSAGE']['VOTESTART'] + " (" + str(self.votemsg) + ")" ) else: - IRC.sendmsg(self, conf['IRC_CHANNEL'], "@chat", conf['MESSAGE']['VOTESTART']) + IRC.sendmsg(self, CONF['IRC_CHANNEL'], "@chat", CONF['MESSAGE']['VOTESTART']) return @@ -906,8 +906,8 @@ class IRC: "badges": True, "subscriber": True, "msgid": True, - "user": conf['IRC_USERNAME'], - "length": conf['IRC_TTS_LEN'], + "user": CONF['IRC_USERNAME'], + "length": CONF['IRC_TTS_LEN'], "queuetime": datetime.datetime.now(), "timestamp": str(time.time_ns()) } @@ -1021,7 +1021,7 @@ class HTTPserv(BaseHTTPRequestHandler): self.send_header('Content-type', 'text/json') self.end_headers() - usermap = conf['USERMAP'] + usermap = CONF['USERMAP'] sorted_tts = {k: msg_queue[k] for k in sorted(msg_queue, reverse=True)} logging.debug(usermap) @@ -1029,10 +1029,10 @@ class HTTPserv(BaseHTTPRequestHandler): if key not in tts_done: if msg_queue[key][0].lower() in usermap: logging.debug('Using usermap for user: %s (%s)', msg_queue[key][0], usermap[msg_queue[key][0].lower()]) - tts = {str(key): str(usermap[msg_queue[key][0].lower()]) + " " + str(conf['MESSAGE']['SAYS']) + ":" + str(msg_queue[key][1])} + tts = {str(key): str(usermap[msg_queue[key][0].lower()]) + " " + str(CONF['MESSAGE']['SAYS']) + ":" + str(msg_queue[key][1])} else: logging.debug('No usermap entry found for user: %s', msg_queue[key][0]) - tts = {str(key): str(msg_queue[key][0]) + " " + str(conf['MESSAGE']['SAYS']) + ":" + str(msg_queue[key][1])} + tts = {str(key): str(msg_queue[key][0]) + " " + str(CONF['MESSAGE']['SAYS']) + ":" + str(msg_queue[key][1])} tts_json = json.dumps(tts) self.wfile.write(bytes(str(tts_json)+"\n", "utf-8")) @@ -1057,11 +1057,11 @@ class HTTPserv(BaseHTTPRequestHandler): data['client_id'] = "ebo548vs6tq54c9zlrgin2yfzzlrrs" data['response_type'] = "token" data['scope'] = "chat:edit chat:read" - if conf['HTTP_PORT'] == 80: + if CONF['HTTP_PORT'] == 80: data['redirect_uri'] = "http://localhost/token" - elif conf['HTTP_PORT'] == 8080: + elif CONF['HTTP_PORT'] == 8080: data['redirect_uri'] = "http://localhost:8080/token" - elif conf['HTTP_PORT'] == 3000: + elif CONF['HTTP_PORT'] == 3000: data['redirect_uri'] = "http://localhost:3000/token" else: self.send_response(500) @@ -1115,87 +1115,87 @@ def load_config(): for section in cfg: try: logging.debug('Fetching config: %s', section) - 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_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.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', True) - conf['WIKI_LANG'] = cfg.get('bot', {}).get('language', 'en') + 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', True) + CONF['WIKI_LANG'] = cfg.get('bot', {}).get('language', 'en') - 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['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.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'] = {} + 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.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', "Votes") + 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', "Votes") - conf['MESSAGE']['PICKSTART'] = cfg.get('messages', {}).get('pickstart', "Pick started. Send #pickme to participate.") - conf['MESSAGE']['PICKRESULT'] = cfg.get('messages', {}).get('pickresult', "Pick ended. The results are:") - conf['MESSAGE']['PICKNONE'] = cfg.get('messages', {}).get('picknone', "Pick ended. Nobody was picked.") + CONF['MESSAGE']['PICKSTART'] = cfg.get('messages', {}).get('pickstart', "Pick started. Send #pickme to participate.") + CONF['MESSAGE']['PICKRESULT'] = cfg.get('messages', {}).get('pickresult', "Pick ended. The results are:") + CONF['MESSAGE']['PICKNONE'] = cfg.get('messages', {}).get('picknone', "Pick ended. Nobody was picked.") - conf['MESSAGE']['QUOTE_NOT_FOUND'] = cfg.get('messages', {}).get('quotenotfound', "Sorry, no quote found.") - conf['MESSAGE']['QUOTE_ADDED_PREFIX'] = cfg.get('messages', {}).get('quoteaddedprefix', "Quote:") - conf['MESSAGE']['QUOTE_ADDED_SUFFIX'] = cfg.get('messages', {}).get('quoteaddedsuffix', "added.") + CONF['MESSAGE']['QUOTE_NOT_FOUND'] = cfg.get('messages', {}).get('quotenotfound', "Sorry, no quote found.") + CONF['MESSAGE']['QUOTE_ADDED_PREFIX'] = cfg.get('messages', {}).get('quoteaddedprefix', "Quote:") + CONF['MESSAGE']['QUOTE_ADDED_SUFFIX'] = cfg.get('messages', {}).get('quoteaddedsuffix', "added.") - conf['MESSAGE']['WIKI_TOO_MANY'] = cfg.get('messages', {}).get('wiki_too_many', "Sorry, there are too many possible results. Try a more narrow search.") - conf['MESSAGE']['WIKI_NO_RESULT'] = cfg.get('messages', {}).get('wiki_no_result', "Sorry, there was an error fetching the wikipedia answer.") + CONF['MESSAGE']['WIKI_TOO_MANY'] = cfg.get('messages', {}).get('wiki_too_many', "Sorry, there are too many possible results. Try a more narrow search.") + CONF['MESSAGE']['WIKI_NO_RESULT'] = cfg.get('messages', {}).get('wiki_no_result', "Sorry, there was an error fetching the wikipedia answer.") - conf['FEATURE'] = {} - conf['FEATURE']['WIKI'] = cfg.get('features', {}).get('wiki', True) - conf['FEATURE']['PICK'] = cfg.get('features', {}).get('pick', True) - conf['FEATURE']['VOTE'] = cfg.get('features', {}).get('vote', True) - conf['FEATURE']['QUOTE'] = cfg.get('features', {}).get('quote', True) - conf['FEATURE']['RANDOM'] = cfg.get('features', {}).get('random', True) - conf['FEATURE']['VERSION'] = cfg.get('features', {}).get('version', True) - conf['FEATURE']['PING'] = cfg.get('features', {}).get('ping', True) + CONF['FEATURE'] = {} + CONF['FEATURE']['WIKI'] = cfg.get('features', {}).get('wiki', True) + CONF['FEATURE']['PICK'] = cfg.get('features', {}).get('pick', True) + CONF['FEATURE']['VOTE'] = cfg.get('features', {}).get('vote', True) + CONF['FEATURE']['QUOTE'] = cfg.get('features', {}).get('quote', True) + CONF['FEATURE']['RANDOM'] = cfg.get('features', {}).get('random', True) + CONF['FEATURE']['VERSION'] = cfg.get('features', {}).get('version', True) + CONF['FEATURE']['PING'] = cfg.get('features', {}).get('ping', True) - conf['USERMAP'] = cfg.get('usermapping', []) + CONF['USERMAP'] = cfg.get('usermapping', []) if 'whitelist' in cfg: - conf['WHITELIST'] = True - conf['WHITELIST_USER'] = cfg['whitelist'] + CONF['WHITELIST'] = True + CONF['WHITELIST_USER'] = cfg['whitelist'] else: - conf['WHITELIST'] = False + CONF['WHITELIST'] = False except KeyError: logging.exception('Your config file is invalid, please check and try again.') sys.exit(254) - if conf['WHITELIST']: + if CONF['WHITELIST']: logging.info('Whitelist mode enabled') - logging.debug('Whitelist: %s', conf['WHITELIST_USER']) + logging.debug('Whitelist: %s', CONF['WHITELIST_USER']) - if not conf['IRC_CHANNEL']: + if not CONF['IRC_CHANNEL']: raise ValueError('Please add your twitch channel to config.yml.') - if not conf['IRC_USERNAME']: + if not CONF['IRC_USERNAME']: raise ValueError('Please add the bots username to config.yml.') - if not conf['IRC_OAUTH_TOKEN']: - conf['IRC_OAUTH_TOKEN'] = "Invalid" - return conf - if not conf['IRC_OAUTH_TOKEN'].startswith('oauth:'): + if not CONF['IRC_OAUTH_TOKEN']: + CONF['IRC_OAUTH_TOKEN'] = "Invalid" + return CONF + 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 def send_tts_queue(): """ Send messages to TTS """ @@ -1204,7 +1204,7 @@ def send_tts_queue(): logging.debug('Raw msg: %s', msg_queue_raw) now = datetime.datetime.now() - if now - raw_msg['queuetime'] > datetime.timedelta(seconds=conf['IRC_CLEARMSG_TIMEOUT']): + if now - raw_msg['queuetime'] > datetime.timedelta(seconds=CONF['IRC_CLEARMSG_TIMEOUT']): logging.debug('clearmsg_timeout reached') if raw_msg['timestamp'] not in msg_queue: logging.info('Sending TTS message') @@ -1215,12 +1215,12 @@ def send_tts_queue(): def get_url(path=False): """ Generate a valid URL from config values """ - if conf['HTTP_BIND'] == "0.0.0.0": + if CONF['HTTP_BIND'] == "0.0.0.0": url = "localhost" else: - url = conf['HTTP_BIND'] + url = CONF['HTTP_BIND'] - url = "http://"+str(url)+":"+str(conf['HTTP_PORT'])+"/" + url = "http://"+str(url)+":"+str(CONF['HTTP_PORT'])+"/" if path: url = url+str(path) @@ -1229,42 +1229,42 @@ def get_url(path=False): def check_oauth_token(): """ Check for valid authentication via Twitch API """ - global conf # pylint: disable=global-statement,invalid-name + global CONF # pylint: disable=global-statement logging.debug('Checking OAuth Token') try: url = 'https://id.twitch.tv/oauth2/validate' - oauth = "OAuth "+str(conf['IRC_OAUTH_TOKEN'].replace('oauth:','')) + oauth = "OAuth "+str(CONF['IRC_OAUTH_TOKEN'].replace('oauth:','')) request = urllib.request.Request(url) request.add_header('Authorization', oauth) urllib.request.urlopen(request) except HTTPError: logging.fatal('Twitch rejected your OAuth Token. Please check and generate a new one.') - logging.info('Please open http://%s:%s/token to generate your OAuth-Token.', conf['HTTP_BIND'], conf['HTTP_PORT']) + logging.info('Please open http://%s:%s/token to generate your OAuth-Token.', CONF['HTTP_BIND'], CONF['HTTP_PORT']) url = get_url("token") webbrowser.open_new_tab(url) logging.info('Please complete the OAuth process and add the token into your "config.yml" within the next 5 minutes.') time.sleep(300) - conf = load_config() + CONF = load_config() check_oauth_token() logging.info('OAuth Token is valid') - return conf + return CONF def main(): """Main loop""" - global conf # pylint: disable=global-statement,invalid-name - conf = load_config() + global CONF # pylint: disable=global-statement + CONF = load_config() lastreload = datetime.datetime.now() - logging.getLogger().setLevel(conf['LOG_LEVEL']) - if conf['LOG_LEVEL'] == 'DEBUG': + logging.getLogger().setLevel(CONF['LOG_LEVEL']) + if CONF['LOG_LEVEL'] == 'DEBUG': sys.tracebacklimit = 5 logging.info("Starting Webserver") - httpd = ThreadingSimpleServer((conf['HTTP_BIND'], conf['HTTP_PORT']), HTTPserv) + httpd = ThreadingSimpleServer((CONF['HTTP_BIND'], CONF['HTTP_PORT']), HTTPserv) http_thread = Thread(target=http_serve_forever, daemon=True, args=(httpd, )) http_thread.start() @@ -1273,8 +1273,8 @@ def main(): logging.info("Starting IRC bot") irc = IRC() - irc.connect(conf['IRC_SERVER'], 6667, conf['IRC_CHANNEL'], conf['IRC_USERNAME'], conf['IRC_OAUTH_TOKEN']) - irc.sendmsg(conf['IRC_CHANNEL'], 'MrDestructoid', conf['MESSAGE']['READY']) + irc.connect(CONF['IRC_SERVER'], 6667, CONF['IRC_CHANNEL'], CONF['IRC_USERNAME'], CONF['IRC_OAUTH_TOKEN']) + irc.sendmsg(CONF['IRC_CHANNEL'], 'MrDestructoid', CONF['MESSAGE']['READY']) logging.info('Connected and joined') url = get_url() @@ -1282,7 +1282,7 @@ def main(): webbrowser.open_new_tab(url) while True: - if conf['LOG_LEVEL'] == "DEBUG": + if CONF['LOG_LEVEL'] == "DEBUG": time.sleep(1) try: @@ -1290,12 +1290,12 @@ def main(): confreload = datetime.datetime.now() if confreload - lastreload > datetime.timedelta(seconds=60): - conf = load_config() + CONF = load_config() lastreload = datetime.datetime.now() if irc.quickvote_status and irc.votemsg: logging.info('Quickvote is active') - irc.sendmsg(conf['IRC_CHANNEL'], "@chat", conf['MESSAGE']['VOTESTART'] + " (" + str(irc.votemsg) + ")") + irc.sendmsg(CONF['IRC_CHANNEL'], "@chat", CONF['MESSAGE']['VOTESTART'] + " (" + str(irc.votemsg) + ")") if not irc.tts_status: continue @@ -1312,8 +1312,8 @@ if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(module)s %(threadName)s %(levelname)s: %(message)s') sys.tracebacklimit = 3 - VERSION = "1.7.0" - conf = {} + VERSION = "1.7.1" + CONF = {} tts_done = [] msg_queue_raw = [] msg_queue = {}