def update_role_list(self): # Check if we need to get the roles if tutils.tnow( ) - self.last_role_update > ChanSelector.UPDATE_INTERVAL: roles = [] # Get all channels for cat in get_bot_categs(self.storage, self.server): for chan in self.server.get_chans_in_cat(cat["id"]): # Build a line crt_name = f"**{chan.name}**" if chan.topic: crt_name += " " + chan.topic # Clip line length if len(crt_name) > ChanSelector.TOTAL_LEN: crt_name = crt_name[:ChanSelector.TOTAL_LEN] + "..." roles.append(crt_name) role_dict = OrderedDict() for role in sorted(roles): role_dict[role] = self.do_stuff # Mark last role update time self.last_role_update = tutils.tnow() # Set the items self.set_items(role_dict)
def update_role_list(self): """ Update roles in case a role name has changed """ # Check if we need to get the roles if tutils.tnow() - self.last_role_update > RoleSelector.ROLE_UPDATE_INTERVAL: # Get the roles roles = dutils.get_roles_from_ids(self.roles, self.server) self.name_to_role = {} # Map names to roles for quick lookup role_list = list() for role in roles: self.name_to_role[role] = roles[role] role_list.append(role) role_list = sorted(list(role_list), key=str.casefold) role_dict = OrderedDict() # Role list to pass to the selector for item in role_list: role_dict[item] = self.do_stuff # Mark last role update time self.last_role_update = tutils.tnow() # Set the items self.set_items(role_dict)
def do_auto_order(server, chanlist): lower_limit = int(time_utils.tnow()) - time_utils.SEC_IN_MIN * 60 server_chans = server.get_channels() to_order = [] msgs_to_chan = {} # Get channels to sort for chan_id in chanlist: for srvchan in server_chans: if srvchan.id == chan_id: to_order.append(srvchan) # Get number of messages for each channel for channel in to_order: cnt = get_msg_cnt_for_channel_after(channel.id, lower_limit) if cnt not in msgs_to_chan: msgs_to_chan[cnt] = [] # Store it in a map in case there are multiple channels with the same number # of messages msgs_to_chan[cnt].append(channel) # Create an array containing the old positions old_positions = sorted([i.position for i in to_order]) # Each most active channel will be positioned to the topmost position for nb_messages in reversed(sorted(msgs_to_chan.keys())): crt_elem = msgs_to_chan[nb_messages] for chan in crt_elem: # Only change the order if it's different if chan.position != old_positions[0]: print(chan.name + " -> " + str(old_positions[0])) chan.move_channel(old_positions[0]) del old_positions[0]
def remind(event, text, storage): """<period message> - ask the bot to remind you about something in given period (e.g. '.remind 1h bleh bleh' sends you 'bleh bleh' in one hour""" # Get period and message data = text.split(" ", maxsplit=1) if len(data) != 2: return "Must specify period and message" # Extract all the data remind_seconds = time_utils.timeout_to_sec(data[0]) message = data[1] if "remind" not in storage: storage["remind"] = [] # Create new entry elem = {} elem["author"] = event.author.id elem["deadline"] = time_utils.tnow() + remind_seconds elem["message"] = message # Append it to the reminder list storage["remind"].append(elem) # Save it storage.sync() return "Okay!"
def update_role_list(self): # Check if we need to get the roles if tutils.tnow( ) - self.last_role_update > RoleSelector.ROLE_UPDATE_INTERVAL: # Get the roles roles = dutils.get_roles_between(self.first_role, self.last_role, self.server) self.name_to_role = {} # Map names to roles for quick lookup role_list = OrderedDict() # Role list to pass to the selector for role in roles: self.name_to_role[role.name] = role role_list[role.name] = self.do_stuff # Mark last role update time self.last_role_update = tutils.tnow() # Set the items self.set_items(role_list)
def firewall_check(bot, storage_getter): for server in bot.backend.get_servers(): storage = storage_getter(server.id) if "fw" not in storage: continue if (storage["fw"]["status"] == "up" and storage["fw"]["end_time"] and time_utils.tnow() > storage["fw"]["end_time"]): storage["fw"][ "status"] = "auto stopped on %s GMT" % time_utils.time_to_date( storage["fw"]["end_time"]) storage.sync()
async def is_spam(self, event): # Check role assign spam now = tutils.tnow() if (event.author.id in last_user_assign and now - last_user_assign[event.author.id] < MIN_SEC): last_user_assign[event.author.id] = now event.author.send_pm( "You're assigning roles too quickly. You need to wait %d seconds between assignments" % MIN_SEC) return True last_user_assign[event.author.id] = now return False
async def timeout(text, server, storage, event, send_embed): """ Timeout an user: timeout @plp 1m - timeouts plp for one minute timeout @plp - displays timeout for plp If the user is timeouted, the timeout can be modified by issuing the timeout command again. """ text = text.split(" ") if len(text) == 0: return "Please specify a parameter:\n" + timeout.__doc__ user = dutils.get_user_by_id(server, dutils.str_to_id(text[0])) if not user: return "Could not find user" crt_timeout = user.timeout # User info needed if len(text) == 1: if crt_timeout == None: return "User does not have a timeout set." else: return f"Timeout will expire in: {time_utils.sec_to_human(crt_timeout.timestamp() - time_utils.tnow())}" # Set a timeout elif len(text) >= 2: tosec = time_utils.timeout_to_sec(text[1]) texp = time_utils.tnow() + tosec reason = "Not given" if len(text) >= 3: reason = " ".join(text[2:]) await user.set_timeout(time_utils.time_to_date(texp)) if crt_timeout != None: return f"Adjusted timeout to {time_utils.sec_to_human(tosec)}" else: # Create reason reason = add_reason(storage, event, user, reason, server, texp, "timeout") # Log the action log_action( storage, reason, send_embed, "User given timeout", ) return f"Set timeout to {time_utils.sec_to_human(tosec)}"
def update_sub(reddit, sub, ustorage): print(f"updating {sub}") if sub not in dont_cache: links = get_links_from_sub(reddit, sub, ["day", "week", "month", "year"]) if sub not in ustorage["data"]: ustorage["data"][sub] = { "links": links, "updated_on": tutils.tnow(), } else: ustorage["data"][sub]["links"].extend(links) ustorage["data"][sub]["updated_on"] = tutils.tnow() # filter out duplicates ustorage["data"][sub]["links"] = list( set(ustorage["data"][sub]["links"])) ustorage.sync() return ustorage["data"][sub]["links"] else: return get_links_from_sub(reddit, sub, ["month"])
def fw_up(text): # Extract the duration if len(text) > 0: storage["fw"]["end_time"] = time_utils.tnow( ) + time_utils.timeout_to_sec(text[0]) else: storage["fw"]["end_time"] = None storage["fw"]["status"] = "up" storage.sync() if storage["fw"]["end_time"]: reply("Firewall enabled. It will stop in %s" % time_utils.sec_to_human(time_utils.timeout_to_sec(text[0]))) else: reply("Firewall enabled. It will run indefinitely.")
def remind_check_server(server, storage, send_pm): if "remind" not in storage: return # Parse list for elem in storage["remind"]: # Check if expired if elem["deadline"] < time_utils.tnow(): # Remove it from list storage["remind"].remove(elem) storage.sync() # Get target user target_user = dutils.get_user_by_id(server, elem["author"]) if not target_user: print("invalid user") continue send_pm( "You set a reminder with the message:\n%s" % elem["message"], target_user, )
def check_to_delete(): for key in list(to_delete.keys()): if key < time_utils.tnow(): to_delete[key].delete_message() del to_delete[key]
def add_temporary_reply(reply): if reply.timeout != 0: to_delete[time_utils.tnow() + reply.timeout] = reply
def update_porn(): for sub in ustorage["data"].keys(): if tutils.tnow( ) - ustorage["data"][sub]["updated_on"] > UPDATE_TIMEOUT: update_sub(reddit_inst, sub, ustorage)