def toggle_module(self, input, command): match = re.search(r'^([^ ]+)$', input.strip()) if not match: self.print(f'Usage: {command.usage}') return module_name = match.group(1) con, cur = get_db() sql = "SELECT * FROM modules WHERE module_name = ?" cur.execute(sql, (module_name, )) res = cur.fetchone() if not res: self.print(f'Module not found: {module_name}') con.commit() con.close() return enabled = not res['enabled'] sql = "UPDATE modules SET enabled = ? WHERE id = ?" cur.execute(sql, (enabled, res['id'])) enabled_str = 'enabled' if enabled else 'disabled' self.print(f'Module {module_name} has been {enabled_str}') self.buffer_print( 'STATUS', 'Modules changed. Restart VoltronBot for changes to take effect.') con.commit() con.close()
def counter(self, event, *args): if len(args) != 1: return None counter_name = args[0] con, cur = get_db() sql = "SELECT id, value FROM counters WHERE counter_name = ?" cur.execute(sql, (counter_name, )) res = cur.fetchone() count = 1 if not res: sql = "INSERT INTO counters (counter_name, value) VALUES (?, 1)" cur.execute(sql, (counter_name, )) count = 1 else: count = res['value'] + 1 sql = "UPDATE counters SET value = ? WHERE id = ?" cur.execute(sql, (count, res['id'])) con.commit() con.close() return str(count)
def update_modules(self): con, cur = get_db() mod_dir = get_module_directory() core_mods = next(os.walk('./Modules'))[1] user_mods = next(os.walk(mod_dir))[1] core_mods += user_mods module_names = [] for mod in core_mods: if mod[0] == '_': continue if not mod in user_mods: mod_import = import_module('Modules.{}'.format(mod)).VoltronModule else: mod_import = import_module('{}.{}'.format(config.USER_MODULES_DIRECTORY, mod)).VoltronModule mod_name = mod_import.module_name if mod_name in self._core_mod_names: self.buffer_queue.put(('ERR', f'Invalid Module Name: {mod_name}')) continue module_names.append(mod_name) sql = 'SELECT * FROM modules WHERE module_name = ?' cur.execute(sql, (mod_name, )) res = cur.fetchone() if not res: sql = 'INSERT INTO modules (module_name, enabled) VALUES (?, ?)' cur.execute(sql, (mod_name, 0)) continue elif not res['enabled']: continue mod_instance = mod_import(self, self.voltron) self.modules.append(mod_instance) #module_names = [] #for mod in self.modules: # module_names.append(mod.module_name) sql = "DELETE FROM modules WHERE module_name NOT IN ({})".format(','.join('?' * len(module_names))) cur.execute(sql, module_names) res = cur.fetchall() con.commit() con.close()
def list_modules(self, input, command): con, cur = get_db() sql = "SELECT * FROM modules" cur.execute(sql) res = cur.fetchall() self.print('') self.print('Available Modules:') for r in res: mod_str = ' ' + r['module_name'] if not r['enabled']: mod_str += ' (DISABLED)' self.print(mod_str) self.print('') con.commit() con.close()
def save_oauth(oauth_token, refresh_token, expires_in, is_broadcaster): """ Fetch all of the user information from the Twitch API and save it to the database Args: oauth_token (string): OAuth token received from Twitch refresh_token (string): Refresh token received from Twitch expires_in (int): Number of seconds until oauth_token expires is_broadcaster (bool): True if this is the broadcaster account """ ## PRODUCTION ## ######## ## SET FERNET_KEY HERE FOR PRODUCTION ######## fernet_key = '' if hasattr(config, 'FERNET_KEY'): fernet_key = config.FERNET_KEY ## Calcuate the exact expire time based off how many seconds the token ## is valid. Fetch all of the user information from the Twitch API ## and save it to our database expire_time = datetime.now() + timedelta(seconds=expires_in) cipher = Fernet(fernet_key) oauth_token = cipher.encrypt(oauth_token.encode()) refresh_token = cipher.encrypt(refresh_token.encode()) token = OauthTokens(oauth_token, refresh_token, expire_time) api = TwitchAPIHelper(token) user_info = api.get_this_user() data = user_info.get('data', []) if not data: debug('No user data found') return this_user = data[0] con, cur = get_db() ## If this is the broadcaster we need to check if a broadcaster already ## exists in the database if is_broadcaster: sql = "SELECT id, user_name FROM oauth WHERE is_broadcaster = 1" cur.execute(sql) res = cur.fetchone() if res: debug("Broadcaster Already Exists!") ## Broadcaster already exists and we should do somthing about it sql = "UPDATE oauth SET is_broadcaster = 0 WHERE is_broadcaster = 1" #sql = "DELETE FROM oauth WHERE is_broadcaster = 1" cur.execute(sql) con.commit() ## SQLite doesn't support booleans so convert to an integer is_broadcaster_int = int(is_broadcaster == True) ## If an account already exists for this twitch user, update the ## existing account existing = get_user_by_twitch_id(this_user['id']) new_user = None sql = 'SELECT id FROM oauth WHERE is_default = 1' cur.execute(sql) default = cur.fetchone() is_default = 0 if default else 1 if existing: sql = "UPDATE oauth SET \ user_name = ?, \ login_time = datetime('now'), \ display_name = ?, \ oauth_token = ?, \ refresh_token = ?, \ token_expire_time = ?, \ is_broadcaster = ?, \ is_default = ? \ WHERE id = ? \ " cur.execute(sql, (this_user['login'], this_user['display_name'], oauth_token, refresh_token, expire_time, is_broadcaster_int, is_default, existing.id)) con.commit() new_user = existing new_user.refresh() else: sql = "INSERT INTO oauth \ (user_name, login_time, display_name, twitch_user_id, oauth_token, refresh_token, token_expire_time, is_broadcaster, is_default) \ VALUES (?, datetime('now'), ?, ?, ?, ?, ?, ?, ?)" cur.execute(sql, (this_user['login'], this_user['display_name'], this_user['id'], oauth_token, refresh_token, expire_time, is_broadcaster_int, is_default)) con.commit() new_user = User(cur.lastrowid) con.close() return new_user