async def list(msg: discord.Message, p, lid=None): chn: discord.TextChannel = msg.channel if lid: try: l: Lobby = Lobby.get_or_none(int(lid)) if not l: await Module.error( msg, f"Lobby not found, check your command or try use `{p} list`" ) return users = LobbyList.select().where( LobbyList.lobby == l.id).execute() users_list = list(users) users_desc = "\n".join( map( lambda x: f"- {x.user}, joined {x.joined:%Y-%m-%d %H:%M}", users_list)) await msg.channel.send(embed=discord.Embed( title=f"Details of {l.subject}", color=Module.COLOR_INFO, description=f"**Author**: {l.author}\n" f"**Created**: {l.created:%Y-%m-%d %H:%M}\n" f"**Slots**: `{len(users_list)}` / `{l.slots if l.slots > 0 else 'Unlimited'}`\n" f"\n\n" f"Users:\n{users_desc}")) pass except ValueError: await Module.error( msg, f"Please type the command correctly.\nExample: `{p} list 13`" ) return lobbies = list( Lobby.select().where(Lobby.server == chn.guild.id).execute()) if lobbies: content = "" for lobby in lobbies: content += f"**`{lobby.id}`**: {lobby.subject}\n" pass await msg.channel.send(embed=discord.Embed( title="List of lobbies", color=discord.Colour.from_rgb(150, 200, 150), description=content)) pass else: await msg.channel.send(embed=discord.Embed( title="List of lobbies", color=discord.Colour.from_rgb(50, 50, 200), description="There is currently no lobby on this server.")) pass
async def join(msg: discord.Message, lobby_id, p): l: Lobby = Lobby.get_or_none(Lobby.id == int(lobby_id)) if l: p = LobbyList.get_or_none( LobbyList.user == msg.author.display_name) if p: await msg.channel.send(embed=discord.Embed( title="Info", color=Module.COLOR_INFO, description="You are already joined in lobby.")) return else: LobbyList.create(lobby=l.id, user=msg.author.display_name, user_mention=msg.author.mention) await msg.channel.send( embed=discord.Embed(title="Info", color=Module.COLOR_INFO, description="Welcome to lobby!")) return pass else: await Module.error( msg, f"Lobby not found, check your command or try use `{p} list`.") pass
async def leave(msg: discord.Message, id_lobby, p): l: Lobby = Lobby.get_or_none(Lobby.id == int(id_lobby)) if l: ll: LobbyList = LobbyList.get_or_none( (LobbyList.lobby == id_lobby) & (LobbyList.user_mention == msg.author.mention)) if not ll: await Module.error( msg, f"You are not joined to the selected lobby.") return if l.author_mention == msg.author.mention: await Module.error( msg, "You are the founder of the lobby, you can't leave it. " "If you want to leave the lobby, you can remove it." "Use:" f"`{p} remove {id_lobby}`") pass else: ll.delete_instance() await Module.error(msg, "You left the lobby.") pass pass else: await Module.error( msg, f"Lobby not found, check your command or try use `{p} list`.") pass pass
async def join(self, ctx: Context, lobby: str): """Připojíte se k vybranému lobby. $join NAME $join NašeSuperLobby""" l = Lobby.get_or_none((Lobby.name == lobby) & (Lobby.server == ctx.guild.id)) if not l: await self.generic_error( ctx, "Lobby nebylo nalezeno, zkontrolujte příkaz a jeho argumenty.") return p = LobbyList.get_or_none( (LobbyList.user_mention == ctx.author.mention) & (LobbyList.lobby == l.id)) if p: await ctx.send(embed=discord.Embed( title="Info", color=discord.Colour.from_rgb(100, 200, 100), description="Již jste v tomto lobby připojeni.")) return LobbyList.create(lobby=l.id, user=ctx.author.display_name, user_mention=ctx.author.mention) await ctx.send(embed=discord.Embed(title="Info", color=discord.Colour.green(), description=f"Vítejte v {l.name}!")) pass
async def leave(self, ctx: Context, lobby: str): """Příkaz pro opuštění lobby. $leave NAME $leave NašeSuperLobby""" l: Lobby = Lobby.get_or_none((Lobby.name == lobby) & (Lobby.server == ctx.guild.id)) if not l: await self.generic_error(ctx, "Lobby nebylo nalezeno.") return if l.author_mention == ctx.author.mention: await self.generic_error( ctx, f"Jelikož jste lobby založili, nemůžete ho opustit. " f"Použijte příkaz pro smazání.\n`$delete \"{l.name}\"`") return ll: LobbyList = LobbyList.get_or_none((LobbyList.lobby == l.id) & ( LobbyList.user_mention == ctx.author.mention)) if ll: ll.delete_instance() await ctx.send( embed=discord.Embed(title="Info", colour=discord.Colour.green(), description="Opustili jste lobby.")) return else: await self.generic_error( ctx, "Daného lobby jste nebyli členy, zkontrolujte příkaz.") return pass
async def lobby(self, ctx: Context, lobby: str): """Zobrazí detail vybraného lobby. $lobby NAME $lobby NašeSuperLobby""" l: Lobby = Lobby.get_or_none((Lobby.name == lobby) & (Lobby.server == ctx.guild.id)) if not l: await self.generic_error(ctx, "Lobby nebylo nalezeno.") return users = list( LobbyList.select().where(LobbyList.lobby == l.id).execute()) users_desc = "\n".join( map(lambda x: f"- {x.user}, připojil se {x.joined:%Y-%m-%d %H:%M}", users)) await ctx.send(embed=discord.Embed( title=l.name, color=discord.Colour.green(), description=f"**Popis**: {l.subject}\n\n" f"**Vlasntík**: {l.author}\n" f"**Založeno**: {l.created:%Y-%m-%d %H:%M}\n\n\n" f"**Členové** ({len(users)}): \n{users_desc}")) return
async def ownership(self, ctx: Context, lobby: str, user: discord.User): """Předá vlastnictví zadanému uživateli. $ownership NAME @USER $ownership NašeSuperLobby @Franta""" l: Lobby = Lobby.get_or_none((Lobby.name == lobby) & (Lobby.server == ctx.guild.id)) if not l: await self.generic_error(ctx, "Lobby nebylo nalezeno.") return if l.author_mention != ctx.author.mention and ctx.author.id != self.owner: await self.generic_error(ctx, "Lobby může předat pouze vlastník.") return l.author = user.display_name l.author_mention = user.mention l.save() await ctx.send( embed=discord.Embed(title="Info", color=discord.Colour.green(), description="Vlastnictví lobby předáno.")) pass
async def create(self, ctx: Context, name: str, subject: str): """Vytváří nové lobby. $create NAME SUBJECT $create NašeSuperLobby \"Naše skupina se věnuje hraní XYZ\"""" name = name.replace(" ", "_") try: l: Lobby = Lobby.create(name=name, subject=subject, author=ctx.author.display_name, author_mention=ctx.author.mention, server=ctx.guild.id) except peewee.IntegrityError as e: if e.args[ 0] == "UNIQUE constraint failed: lobby.name, lobby.server": await self.generic_error( ctx, "Vybraný název již existuje, zvolte prosím jiný.") return raise LobbyList.create(lobby=l.id, user=ctx.author.display_name, user_mention=ctx.author.mention) await ctx.send(embed=discord.Embed( title="Info", color=discord.Colour.green(), description=f"Lobby {l.name} bylo úspěšně založeno. GL&HF!")) pass
async def delete(msg: discord.Message, id_lobby, p): l: Lobby = Lobby.get_or_none(Lobby.id == int(id_lobby)) if l: if l.author_mention == msg.author.mention: l.delete_instance(recursive=True) await Module.error(msg, "You removed the lobby.") pass else: await Module.error(msg, "You are not the founder of the lobby. ") pass pass else: await Module.error( msg, f"Lobby not found, check your command or try use `{p} list`.") pass pass
async def create(msg: discord.Message, name, slots=0): if slots == 1: slots = 2 pass l: Lobby = Lobby.create(subject=name, author=msg.author.display_name, author_mention=msg.author.mention, slots=slots, server=msg.guild.id) LobbyList.create(lobby=l.id, user=msg.author.display_name, user_mention=msg.author.mention) await msg.channel.send( f"Hey {msg.author.mention}, your lobby *{name}* has been created with ID `{l.id}`." ) pass
async def lst(self, ctx: Context): """Vypíše seznam lobby. $list""" lobbies = Lobby.select().where(Lobby.server == ctx.guild.id).execute() buffer = "" for lobby in lobbies: buffer += f"**`{lobby.name}`**: {lobby.subject}\n" pass await ctx.send( embed=discord.Embed(title="List of lobbies", color=discord.Colour.from_rgb(50, 50, 200), description=buffer if len(buffer) else "Aktuálně nejsou žádná lobby vytvořená.")) pass
async def mention(self, ctx: Context, lobby: str, msg: str = None): """Označí a svolá všechny, kteří jsou členy vybraného lobby. $mention NAME $mention NAME MESSAGE $mention NašeSuperLobby $mention NašeSuperLobby \"Dnes hra od 20:00.\"""" l: Lobby = Lobby.get_or_none((Lobby.name == lobby) & (Lobby.server == ctx.guild.id)) if not l: await self.generic_error(ctx, "Lobby nebylo nalezeno.") return content_msg = "Hey, wake up! \n" if not msg else msg + "\n" mentions = [] for row in LobbyList.select().where(LobbyList.lobby == l.id).execute(): mentions.append(row.user_mention) pass content = content_msg + " • ".join(mentions) if len(content) > 2000: content = content_msg for item in mentions: if content == "": content = item continue if len(content + " • " + item) < 2000: content += " • " + item pass else: await ctx.send(content) content = "" pass pass pass else: await ctx.send(content) pass pass
async def update(self, ctx: Context, lobby: str, key: str, value: str): """Provede úpravu existujícího lobby. Umožňuje změnit jméno (name) či její předmět (subject). $update NAME KEY VALUE $update NašeSuperLobby name JeštěSuprovějšíLobby $update NašeSuperLobby subject \"Nový popis našeho suprového lobby.\"""" l: Lobby = Lobby.get_or_none((Lobby.name == lobby) & (Lobby.server == ctx.guild.id)) if not l: await self.generic_error(ctx, "Lobby nebylo nalezeno.") return if l.author_mention != ctx.author.mention and ctx.author.id != self.owner: await self.generic_error(ctx, "Lobby může upravit pouze vlastník.") return if key.lower() == "name": l.name = value try: l.save() except peewee.IntegrityError as e: if e.args[ 0] == "UNIQUE constraint failed: lobby.name, lobby.server": await self.generic_error( ctx, "Vybraný název již existuje, zvolte prosím jiný.") return raise pass elif key.lower() == "subject": l.subject = value l.save() pass pass
async def delete(self, ctx: Context, lobby: str): """Maže vybrané lobby. $delete NAME $delete NašeSuperLobby""" l: Lobby = Lobby.get_or_none((Lobby.name == lobby) & (Lobby.server == ctx.guild.id)) if not l: await self.generic_error(ctx, "Lobby nebylo nalezeno.") return if l.author_mention != ctx.author.mention and ctx.author.id != self.owner: await self.generic_error(ctx, "Lobby může smazat pouze vlastník.") return l.delete_instance(recursive=True) await ctx.send( embed=discord.Embed(title="Info", color=discord.Colour.green(), description="Lobby bylo úspěšně smazáno.")) pass
async def mention(msg: discord.Message, id_lobby, p): l: Lobby = Lobby.get_or_none(Lobby.id == int(id_lobby)) if not l: await Module.error( msg, f"Lobby not found, check your command or try use `{p} list`.") return # if l.author_mention != msg.author.mention: # await Module.error(msg, f"You are not a founder of this lobby.") # return content_msg = "Hey, wake up! \n" mentions = [] for row in LobbyList.select().where(LobbyList.lobby == int(id_lobby)): mentions.append(row.user_mention) pass content = content_msg + " • ".join(mentions) if len(content) > 2000: content = content_msg for item in mentions: if content == "": content = item continue if len(content + " • " + item) < 2000: content += " • " + item else: await msg.channel.send(content) content = "" pass pass else: await msg.channel.send(content) pass pass