async def deserialize(bot, data): # Get the server server = None for elem in bot.get_servers(): if elem.id == data["server_id"]: server = elem break if not server: print("Could not find server id %s" % data["server_id"]) return None # Get the roles first_role = dutils.get_role_by_id(server, data["first_role_id"]) last_role = dutils.get_role_by_id(server, data["last_role_id"]) if not first_role: print( "Could not find frole id %s/%s" % data["server_id"], data["first_role_id"], ) return None if not last_role: print( "Could not find lrole id %s/%s" % data["server_id"], data["last_role_id"], ) return None # Get the channel chan = dutils.get_channel_by_id(server, data["channel_id"]) # Create the selector selector = RoleSelectorInterval( server, chan, first_role.name, last_role.name, data["title"], data["max_selectable"], ) # Set selector page selector.shown_page = data["shown_page"] # Rebuild message cache msg_id = data["msg_id"] # Get the saved message and set it msg = await chan.async_get_message(msg_id) selector.msg = msg # Add message to backend cache bot.backend.add_msg_to_cache(msg) # Remove reacts from other people await selector.reset_reacts(bot) return selector
def assign_old(bot): server = None for srv in bot.backend.get_servers(): if srv.id == RODDIT_ID: server = srv break import datetime now = datetime.datetime.now() two_weeks = now - datetime.timedelta(days=14) joins_2weeks = get_joins_between(server, two_weeks, now) joins_2weeks_ids = [usr.id for usr in joins_2weeks] joinrole = dutils.get_role_by_id(server, "688168479018451081") for user in server.get_users(): has_role = False for role in user.roles: if role.id == joinrole.id: has_role = True break if has_role: if user.id not in joins_2weeks_ids: print("remove " + str(user.id)) user.remove_role(joinrole)
def add_selector_roles(server, storage, text, bot, str_to_id): """<selector> <roles> - adds the specified roles to the selector""" text = text.split() if len(text) < 2: return "Format: " + add_selector_roles.__doc__ selector = text[0] print(text) if "selectors" not in storage or storage["selectors"] == {}: return "No selectors available" if selector not in storage["selectors"]: return "Invalid selector" for i in text[1:]: role = dutils.get_role_by_id(server, str_to_id(i)) if role == None: continue storage["selectors"][selector]["roles"].append(role.id) storage["selectors"][selector]["roles"] = list( set(storage["selectors"][selector]["roles"])) storage.sync() return "Done"
def remove_selector_roles(server, storage, text, bot, str_to_id): """<selector> <roles> - removes the specified roles from the selector """ text = text.split() if len(text) < 2: return "Format: " + remove_selector_roles.__doc__ selector = text[0] print(text) if "selectors" not in storage or storage["selectors"] == {}: return "No selectors available" if selector not in storage["selectors"]: return "Invalid selector" for i in text[1:]: role = dutils.get_role_by_id(server, str_to_id(i)) if role == None: continue try: storage["selectors"][selector]["roles"].remove(role.id) except: pass storage["selectors"][selector]["roles"] = list( set(storage["selectors"][selector]["roles"]) ) storage.sync() reload_file(bot) return "Done"
def check_exp_time(rstorage, command_name, role, server): if command_name not in rstorage: return tnow = datetime.datetime.now().timestamp() to_del = [] for elem in rstorage[command_name]: if elem['expire'] < tnow: to_del.append(elem) for elem in to_del: role = dutils.get_role_by_name(server, role) member = dutils.get_user_by_id(server, elem["user"]) new_roles = [] for role_id in elem['crt_roles']: role = dutils.get_role_by_id(server, role_id) if role: new_roles.append(role) if member: member.replace_roles(new_roles) rstorage[command_name].remove(elem) rstorage.sync()
def check_expired_roles(server, storage): tnow = datetime.datetime.now().timestamp() # Go through each command for cmd_name, cmd_list in storage["temp_roles"].items(): # Go through each element in the command for cmd_element in cmd_list: to_del = [] # If timeout has expired if cmd_element["expire"] < tnow: # Make a list of elements to remove to_del.append(cmd_element) # For each element replace the roles for elem in to_del: member = dutils.get_user_by_id(server, elem["user_id"]) new_roles = [] for role_id in elem['crt_roles']: role = dutils.get_role_by_id(server, role_id) if role: new_roles.append(role) if member: member.replace_roles(new_roles) storage["temp_roles"][cmd_name].remove(elem) storage.sync()
def give_toggled_role(text, server, command_name, storage, event): try: text = " ".join(text.split()).split() if len(text) < 1: return 'Needs a user (e.g. .{CMD} @cnc - to toggle @cnc the {CMD} role OR .{CMD} @cnc bad boy - to toggle @cnc the {CMD} role and save the reason "bad boy"'.format( CMD=command_name) user = dutils.get_user_by_id(server, dutils.str_to_id(text[0])) if not user: return "No such user" reason = "Not given" if len(text) >= 2: reason = " ".join(text[1:]) # Get the role main_role = dutils.get_role_by_id( server, storage["cmds"][command_name]["role_id"]) if main_role is None: return "Could not find given role" # Check if user is already in toggled role present = False for role in user.roles: if role == main_role: present = True break if not present: user.add_role(main_role) storage.sync() user.send_pm( "You have been given the `%s` role.\nReason: %s\nAuthor: %s" % ( storage["cmds"][command_name]["role_name"], reason, event.author.name, )) return "Given role" else: user.remove_role(main_role) user.send_pm( "You have been removed the `%s` role.\nReason: %s\nAuthor: %s" % ( storage["cmds"][command_name]["role_name"], reason, event.author.name, )) return "Removed the role" return "Nothing happened" except Exception as e: return "Couldn't give role: %s" % repr(e)
def create_temp_role_cmd(text, hook, str_to_id, server, bot, storage): """ <command name, role> - create a command that assigns a temporary role by specifying `command_name role` """ text = text.split() # Check if length is correct if len(text) < 2: return create_temp_role_cmd.__doc__ cmd = text[0] # Check minumum length if len(cmd) < 5: return "Command length needs to be at least 5." # Check that command exists if hook.root.get_command(cmd) != None: return "Command `%s` already exists. Try using another name." % cmd # Get the given role role = dutils.get_role_by_id(server, str_to_id(text[1])) if not role: role = dutils.get_role_by_name(server, text[1]) if not role: return "No such role " + text[1] if "cmds" not in storage: storage["cmds"] = {} # Create new object new_cmd = {} new_cmd["cmd_type"] = "temporary" new_cmd["name"] = cmd new_cmd["role_id"] = role.id new_cmd["role_name"] = role.name storage["cmds"][cmd] = new_cmd storage.sync() register_cmd(new_cmd, server) return "Done"
def give_temp_role(text, server, command_name, storage, event): # Remove extra whitespace and split text = " ".join(text.split()).split() if len(text) < 2: return "Needs at least user and time (e.g. .{CMD} @plp, 5m - to give @plp {CMD} for 5 minutes OR .{CMD} @plp 5m bad user - to give @plp {CMD} for 5m and save the reason \"bad user\")".format(CMD=command_name) + \ "The abbrebiations are: s - seconds, m - minutes, h - hours, d - days." # Get user user = dutils.get_user_by_id(server, dutils.str_to_id(text[0])) if not user: return "No such user" # Get reason reason = "Not given" if len(text) >= 3: reason = " ".join(text[2:]) # Get timeout timeout_sec = time_utils.timeout_to_sec(text[1]) # If timeout is 0, double check the input if timeout_sec == 0 and text[1] != "0s": return "There may have been a problem parsing `%s`. Please check it and run the command again." % text[ 1] # When the timeout will expire texp = datetime.datetime.now().timestamp() + timeout_sec # Get the role role = dutils.get_role_by_id(server, storage["cmds"][command_name]["role_id"]) if role is None: return "Could not find given role" if "temp_roles" not in storage: storage["temp_roles"] = {} if command_name not in storage["temp_roles"]: storage["temp_roles"][command_name] = [] # Check if user is already in temp role extra = False for entry in storage["temp_roles"][command_name]: if entry["user_id"] == user.id: extra = True break crt_roles = [] # Get current roles for urole in user.roles: # If the role has already been given, assume that it will be timed from now if urole.id == role.id: continue crt_roles.append(urole.id) # Check if it's extra time if not extra: # Create a new user entry reason_entry = create_user_reason( storage, user, event.author, reason, "https://discordapp.com/channels/%s/%s/%s" % (server.id, event.channel.id, event.msg.id), texp, command_name) # Create command entry new_entry = {} new_entry["user_id"] = str(user.id) new_entry["user_name"] = user.name new_entry["expire"] = texp new_entry["crt_roles"] = crt_roles new_entry["reason_id"] = reason_entry["Case ID"] storage["temp_roles"][command_name].append(new_entry) # Replace user roles user.replace_roles([role]) storage.sync() user.send_pm( "You have been given the `%s` role. It will last for %s.\nReason: %s\nAuthor: %s" % (storage["cmds"][command_name]["role_name"], text[1], reason, event.author.name)) return reason_entry else: adjust_user_reason( storage, event.author, user.id, command_name, texp, "https://discordapp.com/channels/%s/%s/%s" % (server.id, event.channel.id, event.msg.id)) return "Adjusted time for user to %d" % timeout_sec return "Nothing happened"