def delete_reaction(reaction_id): """ Delete a reaction from the database :param reaction_id: The id of the reaction """ session = Session() session.query(Reaction).filter_by(reaction_id=reaction_id).delete() session.commit() session.close()
def remove_server(server_id): """ Deletes a server from the database :param server_id: Server id """ session = Session() session.query(Server).filter_by(server_id=server_id).delete() session.commit() session.close()
def change_status(activity): """ Changes the playing status in the database :param activity: """ session = Session() session.query(Misc).first().status_playing = activity session.commit() session.close()
def set_image_chance(server_id, new_chance): """ Set the image chance percentage of a server :param server_id: Server id :param new_chance: What percentage to set it to """ session = Session() server = get_server(server_id) server.image_chance = new_chance session.commit() session.close()
def increment_reaction_counter(discord_id, inc_score): """ Increment amount of reactions triggered by an user :param discord_id: Discord id :param inc_score: Amount to increment by """ session = Session() user = get_user(discord_id) user.reaction_count += inc_score session.commit() session.close()
def add_server(server_id, server_name): """ Adds a server to the database :param server_id: Server id :param server_name: The server name """ if not server_in_db(server_id): session = Session() new_server = Server(server_id=server_id, server_name=server_name) session.add(new_server) session.commit() session.close()
def update_answer(reaction_id, server_id, answer): """ Update the keyword of a reaction :param reaction_id: Reaction id :param server_id: Server id :param answer: Answer to update """ session = Session() reaction = session.query(Reaction).filter_by( reaction_id=reaction_id, from_server=server_id).first() reaction.answer = answer session.commit() session.close()
def update_keyword(reaction_id, server_id, keyword): """ Update the keyword of a reaction :param reaction_id: Reaction id :param server_id: Server id :param keyword: Keyword to update """ session = Session() reaction = session.query(Reaction).filter_by( reaction_id=reaction_id, from_server=server_id).first() reaction.keyword = keyword session.commit() session.close()
def create_user(discord_id, discord_name, server_id, reaction_count=0): """ Create an user and post it to the database :param discord_id: Discord id :param discord_name: Discord name (not nickname) :param server_id: From which server the user comes :param reaction_count: Amount of reactions that user triggered """ session = Session() user = User(uid=discord_id, username=discord_name, from_server=server_id, reaction_count=reaction_count) session.add(user) session.commit() session.close() logger.info( f"Posted user to DB | {discord_id}: {discord_name} [{server_id}]")
def add_reaction(answer, keyword, react_type, server_id): """ Add a reaction to the database :param answer: What to send back as response :param keyword: On what keyword to match :param react_type: Whether the reaction to be sent is a gif or a normal message :param server_id: From which server the reaction belongs to """ try: session = Session() reaction = Reaction(answer=answer, keyword=keyword, react_type=react_type, from_server=server_id) session.add(reaction) session.commit() session.close() except: logger.error('Error when commiting reaction to database', exc_info=True)