def get_matching_reactions(message, server_id, react_type='message'):
    """
    Takes a message, and compares it against the database if it contains a matching keyword.
    All matches are reactions that will be returned.
    :param server_id: From which server the reactions are
    :param react_type: Whether the reaction to be sent is a gif or a normal message
    :param message: A string
    :return: A list of reactions,
    """
    session = Session()
    query = session.query(Reaction.keyword, Reaction.answer)
    query = query.filter_by(react_type=react_type, from_server=server_id)
    reactions = query.all()
    session.close()
    matches = []

    for reaction in reactions:
        keyword = reaction[0]
        answer = reaction[1]
        match = re.search(r"\b{}\b".format(keyword), message,
                          re.IGNORECASE | re.M)
        if match:
            matches.append(answer)
    if matches:
        return matches
示例#2
0
def get_status():
    """
    Gets the playing status from the database
    :return: playing status
    """
    session = Session()
    status = session.query(Misc).first().status_playing
    return status
def get_servers():
    """
    Retrieve all the discord servers in the database
    :return: List of servers
    """
    session = Session()
    servers = session.query(Server).all()
    return servers
示例#4
0
def get_user(discord_id):
    """
    Retrieve an user by id
    :param discord_id: Discord id
    :return: User
    """
    session = Session()
    user = session.query(User).filter_by(uid=discord_id).first()
    return user
示例#5
0
def user_exists(discord_id):
    """
    Checks if a user exists by id
    :param discord_id: Discord id
    :return: Boolean
    """
    session = Session()
    row = session.query(User).filter_by(uid=discord_id).first()
    session.close()
    return True if row else False
def get_server(server_id):
    """
    Retrieve a server from the database
    :param server_id: Server id
    :return: Server object
    """
    session = Session()
    query = session.query(Server)
    query = query.filter_by(server_id=server_id)
    server = query.first()
    return server
def get_reaction(reaction_id, server_id):
    """
    Gets a reaction by its id
    :param reaction_id: Reaction id
    :param server_id: Server id
    :return: Reaction object
    """
    session = Session()
    query = session.query(Reaction)
    query = query.filter_by(reaction_id=reaction_id, from_server=server_id)
    reaction = query.first()
    return reaction
示例#8
0
def get_users_paginated(low_bound, high_bound):
    """
    Get a list of users, between 2 values.
    :param low_bound: On which users to start matching
    :param high_bound: On which users to stop matching
    :return: list of users
    """
    session = Session()
    order = (User.reaction_count.desc(), User.username)
    row_number = func.row_number().over(order_by=order)
    query = session.query(User)
    query = query.add_column(row_number)
    query = query.from_self().filter(row_number.between(low_bound, high_bound))
    users = query.all()
    session.close()
    return users
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()
示例#10
0
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 get_reactions_paginated(low_bound, high_bound, server_id):
    """
    Get a list of reactions, between 2 values.
    :param low_bound: On which reactions to start matching
    :param high_bound: On which reactions to stop matching
    :param server_id: From which server the reaction belongs to
    :return: list of reactions
    """
    session = Session()
    row_number = func.row_number().over(order_by=Reaction.reaction_id)
    query = session.query(Reaction.reaction_id, Reaction.answer,
                          Reaction.keyword, Reaction.from_server)
    query = query.filter_by(from_server=server_id)
    query = query.add_column(row_number)
    query = query.from_self().filter(row_number.between(low_bound, high_bound))
    reactions = query.all()
    session.close()
    return reactions
示例#12
0
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()
示例#13
0
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 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()
示例#16
0
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}]")
示例#17
0
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()
示例#18
0
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()
示例#19
0
def connected_to_db():
    """
    Queries the database to check if the connection is live
    :return: Boolean
    """
    try:
        session = Session()
        session.execute(text('SELECT 1'))
        session.close()
        return True
    except:
        return False
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)