async def remove_user(self, user: discord.User, config: Config): """Removes user from club lists.""" def choose_new_pres(pool: list): try: new_pres = random.choice(pool) # Remove it from pool. pool.remove(new_pres) # Set it as new president. self.president = new_pres return True except IndexError: return False if user in self.all_members: self.all_members.remove(user) if user.id == self.president.id: if not choose_new_pres(self.vice_presidents): if not choose_new_pres(self.seniors): if not choose_new_pres(self.members): # Empty club, remove it from database. async with config.clubs() as clubs: where = next(i for i, d in enumerate(clubs) if d.get('id') == self.id) del clubs[where] return True else: if user in self.vice_presidents: self.vice_presidents.remove(user) elif user in self.seniors: self.seniors.remove(user) elif user in self.members: self.members.remove(user) await self.update_club(config)
async def update_club(self, config: Config): """Updates club in the bot database.""" async with config.clubs() as clubs: where = next(i for i, d in enumerate(clubs) if d.get('id') == self.id) clubs[where] = self.to_json() return True
async def create_club(cls, config: Config, ctx: Context): """Interactive club creation process. This function creates the club, adds it to both user and global database and returns the club object. It also adjusts the `club_id_length` if required. All errors must be handled in the caller function. """ async def get_input(timeout=30): pred = await ctx.bot.wait_for( "message", check=MessagePredicate.same_context(ctx), timeout=timeout) if pred.content.strip().lower() == "cancel": raise CancellationError return pred.content.strip() data = {} await ctx.send( ":tada: Let's create your club! First, what name do you want the club to have?" " Note that it cannot be changed later!") data["name"] = await get_input() await ctx.send( "Set the name! Now, what do you want to set as the server description?" ) data["description"] = await get_input(60) await ctx.send( "Set the description! What should be the required trophies?" " Enter a number. (without commas)") data["required_trophies"] = int(await get_input()) await ctx.send("Set required trophies! Select a icon for the club!" " Enter the number corresponding to icon of choice.") data["icon_num"] = int(await get_input(60)) await ctx.send("Set club icon! Now, enter a location for your club!") data["location"] = await get_input() await ctx.send( "Set the location. Lastly, what kind of club do you want to create?" " Enter one of `open`, `closed`, or `invite`.") club_type = await get_input() if club_type.strip().lower() not in ["open", "closed", "invite"]: # We raise `NameError` instead of `ValueError` to keep # it separate from the above `int` conversions. raise NameError else: data["ctype"] = club_type data["president"] = ctx.author await ctx.send(f"All set! Club created! :tada:") default_length = await config.club_id_length() async with config.clubs() as clubs: # First we get all club IDs we've used so far to get an ID for our new club. ids = [c["id"] for c in clubs] data["id"], new_length = cls.get_club_id(ids, default_length) club = cls(data) clubs.append(club.to_json()) await config.user(ctx.author).club.set(club.id) if default_length != new_length: await config.club_id_length.set(new_length) return club