def retrieve_top_tokens(arg_guild): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild.id).first() header = ":moneybag: tokens" top_list = [] # Fetch top tokens temp_list = sorted(guild.users, key=lambda x: x.tokens, reverse=True) for index, user in enumerate(temp_list): guild_user = arg_guild.get_member(int(user.user_id)) # If user currently no longer in guild, skip said person. if guild_user is None: continue # Create dict object to store user data in. dbuser = services.AttrDict() dbuser.update({"user": str(guild_user.mention), "value": user.tokens}) top_list.append(dbuser) # maximum of 10 entries tyvm if len(top_list) is 10: break session.commit() session.close() return header, top_list
def get_guild_prefix(arg_guild): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild.id).first() prefix = guild.prefix session.commit() session.close() return prefix
def retrieve_top_activity(arg_guild): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild.id).first() header = ":speaking_head: activity" top_list = [] i = 0 # Fetch top messages_sent temp_list = sorted(guild.users, key=lambda x: x.activity_points, reverse=True) for index, user in enumerate(temp_list): guild_user = arg_guild.get_member(int(user.user_id)) # If user currently no longer in guild, skip said person. if guild_user is None: continue # Create dict object to store user data in. dbuser = services.AttrDict() dbuser.update({ "user": str(guild_user.display_name), "value": user.activity_points }) top_list.append(dbuser) # maximum of 10 entries tyvm if len(top_list) is 10: break session.commit() session.close() return header, top_list
def update_user_tokens(arg_guild, arg_user, arg_increment): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild.id).first() user = next((x for x in guild.users if int(x.user_id) == int(arg_user.id)), None) user.tokens += arg_increment session.commit() session.close()
def update_user_activity(arg_guild, arg_user, arg_increment): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild.id).first() user = next((x for x in guild.users if int(x.user_id) == int(arg_user.id)), None) user.activity_points += arg_increment user.last_message = datetime.now() session.commit() session.close()
def add_user(arg_guild, arg_user): # Prep session session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild.id).first() new_user = User(arg_user.name, arg_user.id) guild.users.append(new_user) session.add(new_user) session.commit() session.close()
def check_reaction(arg_emoji_id, arg_guild_id): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild_id).first() # Return if the reaction matches true or false if guild.karma_emoji == arg_emoji_id: return True else: return False
def add_channel(arg_guild_id, arg_channel): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild_id).first() # Check if channel already exists for guild if sum(str(x.channel_id) == str(arg_channel.id) for x in guild.channels) is 0: # Create new channel object for db new_channel = Channel(arg_channel.id) guild.channels.append(new_channel) session.add(new_channel) session.commit() session.close()
def add_role(arg_guild_id, arg_role, arg_message_req, arg_point_req, arg_karma_req, arg_token_req): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild_id).first() # Check if role already exists for guild if sum(str(x.role_id) == str(arg_role.id) for x in guild.roles) is 0: # Create new role object for db new_role = Role(arg_role.id, arg_message_req, arg_point_req, arg_karma_req, arg_token_req) guild.roles.append(new_role) session.add(new_role) session.commit() session.close()
def remove_channel(arg_guild_id, arg_channel): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild_id).first() # Fetch channel through guild channel_to_remove = next( (x for x in guild.channels if int(x.channel_id) == int(arg_channel.id)), None) if channel_to_remove is not None: guild.channels.remove(channel_to_remove) session.delete(channel_to_remove) session.commit() session.close()
def remove_role(arg_guild_id, arg_role): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild_id).first() # Fetch role through guild role_to_remove = next( (x for x in guild.roles if int(x.role_id) == int(arg_role.id)), None) if role_to_remove is not None: guild.roles.remove(role_to_remove) session.delete(role_to_remove) else: session.close() return False session.commit() session.close() return True
def guild_add_users(arg_guild): # Prep session session = Session() users = arg_guild.members guild = session.query(Guild).filter(Guild.guild_id == arg_guild.id).first() # Create new users and add them to guild for user in users: if not user.bot: # Check if user already exists for guild if sum(str(x.user_id) == str(user.id) for x in guild.users) is 0: new_user = User(user.name, user.id) guild.users.append(new_user) session.add(new_user) session.commit() session.close()
def check_user_last_message(arg_user, arg_guild_id): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild_id).first() user = next((x for x in guild.users if int(x.user_id) == int(arg_user.id)), None) if user.last_message is None: user.last_message = datetime.now() if services.difference_in_minutes(user.last_message, datetime.now()) > 1: user.last_message = datetime.now() result = True else: result = False session.commit() session.close() return result
def retrieve_guild(arg_guild): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild.id).first() dbguild = services.AttrDict() dbguild.update({ "name": guild.name, "id": guild.guild_id, "join_date": guild.join_date, "prefix": guild.prefix, "attached": guild.attached, "karma_emoji": guild.karma_emoji, "log_channel_id": guild.log_channel_id }) session.commit() session.close() return dbguild
def guild_add(arg_guild): session = Session() guilds = session.query(Guild).all() # If new guild if sum(str(x.guild_id) == str(arg_guild.id) for x in guilds) is 0: new_guild = Guild(arg_guild.name, arg_guild.id) session.add(new_guild) # If guild still attached (Matched against bot id) elif sum(int(x.id) == cfg.bot_id for x in arg_guild.members) is 1: guild = session.query(Guild).filter( Guild.guild_id == arg_guild.id).first() guild.attached = True # If guild left while bot offline else: guild = session.query(Guild).filter( Guild.guild_id == arg_guild.id).first() guild.attached = False session.commit() session.close()
def get_user(arg_user): session = Session() guild = session.query(Guild).filter( Guild.guild_id == arg_user.guild.id).first() user = next((x for x in guild.users if int(x.user_id) == int(arg_user.id)), None) dbuser = services.AttrDict() dbuser.update({ "name": user.name, "user_id": user.user_id, "messages_sent": user.messages_sent, "activity_points": user.activity_points, "tokens": user.tokens, "karma": user.karma, "last_message": user.last_message }) session.commit() session.close() return dbuser
def set_karma_event(arg_channel, arg_user_giving, arg_user_receiving, arg_guild_id): session = Session() # Retrieve required data guild = session.query(Guild).filter(Guild.guild_id == arg_guild_id).first() giving_user = next( (x for x in guild.users if int(x.user_id) == int(arg_user_giving.id)), None) receiving_user = next( (x for x in guild.users if int(x.user_id) == int(arg_user_receiving.id)), None) # Check if pre-existing matching karma event event_check = session.query(KarmaEvents).filter( (KarmaEvents.user_giving_id == giving_user.id) & (KarmaEvents.user_receiving_id == receiving_user.id)).order_by( desc(KarmaEvents.datetime)).first() if event_check: # The event already exists, check datetime if event_check.datetime != datetime.today().date(): session.add( KarmaEvents(giving_user.id, receiving_user.id, arg_channel.id)) check = True else: check = False else: # The event doesn't exist, make a new one! session.add( KarmaEvents( giving_user.id, receiving_user.id, arg_channel.id, )) check = True session.commit() session.close() return check
def set_guild_prefix(arg_guild, arg_prefix): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild.id).first() guild.prefix = arg_prefix session.commit() session.close()
def retrieve_channels(arg_guild_id): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild_id).first() channels = guild.channels.copy() session.close() return channels
def guild_remove(arg_guild): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild.id).first() guild.attached = False session.commit() session.close()
def set_guild_karma_emoji(arg_guild, arg_emoji_id): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild.id).first() guild.karma_emoji = arg_emoji_id session.commit() session.close()
def set_guild_log_channel(arg_guild, arg_channel_id): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild.id).first() guild.log_channel_id = arg_channel_id session.commit() session.close()
def retrieve_roles(arg_guild_id): session = Session() guild = session.query(Guild).filter(Guild.guild_id == arg_guild_id).first() roles = guild.roles.copy() session.close() return roles