def register_user(message): reply_message = 'Welcome ' + \ str(message.from_user.first_name) + ' to F1 predictions!' try: cursor.execute("SELECT * FROM users WHERE username=?", (message.from_user.username, )) user = cursor.fetchall() except mariadb.Error as e: logger.error( f'Error SELECTING FROM users WHERE username... MariaDB output: {e}' ) if not user: try: cursor.execute( "INSERT IGNORE INTO users (username,user_id,points) VALUES (?, ?, ?)", (message.from_user.username, message.from_user.id, 0)) conn.commit() except mariadb.Error as e: logger.error(f'Error INSERTING username... MariaDB output: {e}') bot.reply_to(message, reply_message) logger.info('User created in database') return True bot.reply_to(message, "Wait, are you registered? I think so...")
def set_points(message): arguments = message.text.split()[1:] username: str = arguments[0] points: int = arguments[1] print(username + " " + points) cursor.execute("UPDATE users SET points=(?) WHERE username=(?)", (points, username)) conn.commit()
def flakeRead(self, serverID): ids = [] counts = [] cursor.execute('SELECT * FROM Flake' + str(serverID) + ' ORDER BY Count DESC') rows = cursor.fetchall() for row in rows: ids.append(row[0]) counts.append(row[1]) return ids, counts
async def flakeReset(self, ctx): '''Resets the flakeRank (ADMIN).''' await ctx.send( "Are you sure you want to permanently reset the flakeRank? Type 'Y' to confirm." ) if not await self.confirmAction(ctx): await ctx.send('Reset aborted.') return cursor.execute('DROP TABLE IF EXISTS flake' + ctx.message.guild.id) db.commit()
def get_list(message): try: cursor.execute("SELECT * FROM users") users_fetched = cursor.fetchall() except mariadb.Error as e: logger.error(f'Error SELECTING FROM users... MariaDB output: {e}') users = [] for user in users_fetched: users.append(User(user[0], user[1], user[2])) ordered_users = sorted(users, key=lambda x: x.points, reverse=True) table = create_standings_table(ordered_users) bot.send_message(message.chat.id, f'<pre>{table}</pre>', parse_mode=ParseMode.HTML)
def get_predictions(message): current_race = check_race() cursor.execute("SELECT * FROM predictions WHERE race=?", (current_race, )) predictions_fetched = cursor.fetchall() predictions = [] for prediction in predictions_fetched: driver_prediction = [prediction[1], prediction[2], prediction[3]] cursor.execute("SELECT * FROM users WHERE user_id=?", (prediction[0], )) username = cursor.fetchall() predictions.append( Prediction(username[0][0], driver_prediction, prediction[4])) table = create_predictions_table(predictions) bot.send_message(message.chat.id, \ f'<b>{current_race}</b>' + f'<pre>{table}</pre>', parse_mode=ParseMode.HTML) conn.commit()
def set_prediction(message): if (info.are_predictions_closed == True): bot.send_message( message.chat.id, f"Sorry @{message.from_user.username} pit lane is closed. Enjoy the race!" ) return None status = message.text.split()[1:] if any(status.count(element) > 1 for element in status) == True: bot.send_message( message.chat.id, f"Wait @{message.from_user.username}, you have duplicated drivers. Make sure you have use different drivers. Usage: /predict HAM VER BOT" ) return None if (len(status) != 3): bot.send_message( message.chat.id, "[!] Error. Message should be like: /predict HAM VER BOT") return None for driver in status: if (driver not in info.drivers): bot.send_message( message.chat.id, "[!] Error. Message should be like: /predict HAM VER BOT") bot.send_message(message.chat.id, "Drivers: " + '; '.join(info.drivers)) return None race_name = check_race() cursor.execute("INSERT IGNORE INTO predictions (user_id, driver_p1, driver_p2, driver_p3, race) VALUES (?, ?, ?, ?, ?)" \ , (message.from_user.id,status[0],status[1],status[2],race_name)) conn.commit() cursor.execute("UPDATE predictions SET driver_p1=(?), driver_p2=(?), driver_p3=(?) WHERE user_id=(?) AND race=(?)" \ , (status[0],status[1],status[2],message.from_user.id,race_name)) conn.commit() bot.send_message(message.chat.id, \ f'@{message.from_user.username} your prediction was saved: 1st: {status[0]}; 2nd: {status[1]}; 3rd: {status[2]}')
def set_result(message): arguments = message.text.split()[1:] race_choosed = arguments[0] + " " + arguments[1] if (len(arguments) != 5): bot.send_message(message.chat.id, "Error. Usage: /result GP Spain HAM BOT VER") return None if (message.from_user.id == int(os.getenv('ADMIN_ID'))): races = [] # all_races() if race_choosed not in races: bot.send_message(message.chat.id, "Error, race not found") return None podium = [arguments[2], arguments[3], arguments[4]] for driver in podium: if driver not in drivers: bot.send_message(message.chat.id, "Error, driver not found") return None cursor.execute("SELECT * FROM predictions WHERE race=?", (race_choosed, )) predictions_fetched = cursor.fetchall() predictions = [] for prediction in predictions_fetched: driver_prediction = [prediction[1], prediction[2], prediction[3]] cursor.execute("SELECT * FROM users WHERE user_id=?", (prediction[0], )) username = cursor.fetchall() predictions.append( Prediction(username[0][0], driver_prediction, prediction[4])) for prediction in predictions: sum_points = 0 if prediction.driver_prediction[0] == podium[0]: sum_points += 2 if prediction.driver_prediction[1] == podium[1]: sum_points += 2 if prediction.driver_prediction[2] == podium[2]: sum_points += 2 for driver in prediction.driver_prediction: if driver in podium: sum_points += 1 cursor.execute("SELECT * FROM users WHERE username=?", (prediction.username, )) user = cursor.fetchall() updated_user = list(user[0]) updated_user[2] += sum_points points: int = updated_user[2] user_id: int = user[0][1] cursor.execute("UPDATE users SET points=(?) WHERE user_id=(?)", (points, user_id)) conn.commit()
def flakeIncrement(self, ID, serverID): serverID = str(serverID) cursor.execute( 'UPDATE Flake' + serverID + ' SET Count = Count + 1 WHERE ID = ?', (ID, )) cursor.execute( 'INSERT OR IGNORE INTO flake' + serverID + ' (ID, Count) VALUES (?, 1)', (ID, )) cursor.execute('SELECT Count FROM Flake' + serverID + ' WHERE ID = ?', (ID, )) rtn = str(cursor.fetchone()[0]) db.commit() return rtn
def createTable(self, name): cursor.execute( 'CREATE TABLE IF NOT EXISTS {}(ID TEXT PRIMARY KEY, Count INTEGER)' .format(name)) db.commit()