async def switch_db_toggle(self, ctx): """Toggle member's switch availability status in database.""" author = ctx.author # Get current status db.execute('''SELECT switch FROM mentors WHERE discord_id = %(id)s''', {'id': author.id}) current = db.fetchone()[0] # Toggle if current: new = False status = '- Unavailable' else: new = True status = '+ Available' db.execute( '''UPDATE mentors SET switch = %(new)s WHERE discord_id = %(id)s''', { 'new': new, 'id': author.id }) conn.commit() # Send embed embed = discord.Embed( color=16711690, description=f'**{author.mention} {str(author)}:**\n' f'```diff\n{status}```', timestamp=datetime.utcnow()) embed.set_author(name='Switch availability updated', icon_url=author.avatar_url) embed.set_footer(text=f'ID: {author.id}') await ctx.send(embed=embed)
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()
async def advisor_role_toggle(self, ctx): """Toggle member's roles from mentor to advisor, and update database.""" embed = await helpers.update_roles( ctx.guild.get_member(ctx.author.id), discord.utils.get(ctx.guild.roles, name='Mentor'), # Remove discord.utils.get(ctx.guild.roles, name='Advisor')) # Add # Update database db.execute( '''UPDATE mentors SET status = 'Advisor' WHERE discord_id = %(id)s''', {'id': ctx.author.id}) conn.commit() await ctx.send(embed=embed)
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 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]}')
async def do_not_disturb_toggle(self, ctx): """Toggle member's do not disturb role, and update database.""" mentors_role = discord.utils.get(ctx.guild.roles, name='Mentors') dnd_role = discord.utils.get(ctx.guild.roles, name='DO NOT DISTURB') member = ctx.guild.get_member(ctx.author.id) # Mentors -> DND if mentors_role in ctx.author.roles: dnd_value = True # Change nickname try: if ctx.author.display_name[:6] != '[DND] ': await ctx.author.edit( nick=f'[DND] {ctx.author.display_name}') except discord.errors.Forbidden: pass # Update roles and get embed embed = await helpers.update_roles(member, mentors_role, dnd_role) # DND -> Mentors elif dnd_role in ctx.author.roles: dnd_value = False # Change nickname try: if ctx.author.display_name[6:] == ctx.author.name: await ctx.author.edit(nick=None) elif ctx.author.display_name[:6] == '[DND] ': await ctx.author.edit(nick=ctx.author.display_name[6:]) except discord.errors.Forbidden: pass # Update roles and get embed embed = await helpers.update_roles(member, dnd_role, mentors_role) # Update database db.execute( '''UPDATE mentors SET do_not_disturb = %(value)s WHERE discord_id = %(id)s''', { 'value': dnd_value, 'id': ctx.author.id }) conn.commit() await ctx.send(embed=embed)