async def role_list(self, ctx: SlashContext): """Shows list of all roles available to you. Roles are grouped by role group""" await ctx.defer() caller_has_server_perms = await has_server_perms_from_ctx(ctx) embed = utils.bot_embed(self.bot) embed.title = "Available roles:" db_groups = await RoleGroup.all() for group in db_groups: if await self.get_group_state(group) != RoleGroupStates.normal: continue query = Role.filter(group=group, archived=False) if not caller_has_server_perms: query = query.filter(assignable=True) db_roles = await query.values_list("name", flat=True) if not db_roles: continue role_mentions = [self.get_role_repr(ctx, role) for role in db_roles] embed.add_field(name=group.name, value="\n".join(role_mentions), inline=True) if not embed.fields: embed.description = "Woe is me, there are no roles!" embed.set_footer(text="Use `/role assign <role>` to get those roles!", icon_url=self.bot.user.avatar_url) await ctx.send(embed=embed, allowed_mentions=discord.AllowedMentions.none())
def make_embed(self): embed = utils.bot_embed(self.cog.bot) embed.title = self.game_title winner_index = None if self.state is GameStates.has_winner: winner_index, win_text = self.get_winner() if winner_index is not None: embed.add_field( name=f"🎉 {self.players[winner_index].member.name} won! 🎉", value=win_text.format( self.players[winner_index].member.display_name, self.players[self.get_next_player( winner_index)].member.display_name), inline=False) else: embed.add_field(name="Draw!", value=win_text, inline=False) embed.set_footer( text=f"Use `/play {self.command_name}` for another game!", icon_url=self.cog.bot.user.avatar_url) elif self.state is GameStates.game_timeout: embed.set_footer(text="Game ended at", icon_url=self.cog.bot.user.avatar_url) embed.timestamp = datetime.utcnow() else: embed.description = "Join the game! Press button to play!" embed.set_footer(text="Game ends at", icon_url=self.cog.bot.user.avatar_url) embed.timestamp = self.started_at + timedelta( seconds=self.cog.global_timeout) for i, player in enumerate(self.players): if player: text = f"{player.member.mention}\n{player.member}" text += self.additional_player_text(i) else: text = "Nobody wanted to play :(" if self.state is GameStates.game_timeout \ else "Free spot!\nMake a move to join the game!" title = self.get_player_title(i) if winner_index == i: title += "- winner!" embed.insert_field_at(i, name=title, value=text) return embed
async def emote_archive(self, ctx: SlashContext): """Sends zip archive containing all emote images""" await ctx.defer() with io.BytesIO() as zip_binary: with zipfile.ZipFile(zip_binary, mode="w", compression=zipfile.ZIP_DEFLATED) as zf: for path in self.emotes.values(): zf.write(path, os.path.basename(path)) zip_binary.seek(0) embed = utils.bot_embed(self.bot) embed.title = f"Compressed emotes archive ({len(self.emotes)} total)" embed.description = f"Here, I prepared a compressed archive of all the emotes in my storage!" await ctx.send(embed=embed, file=discord.File(zip_binary, filename="emotes.zip"))
def make_embed(self): embed = utils.bot_embed(self.cog.bot) embed.title = self.game_title if self.state is GameStates.has_winner: winner_state, win_text = self.get_winner() if winner_state: name = f"🎉 {self.player.member.name} won! 🎉" else: name = f"{self.player.member.name} lost :(" embed.add_field(name=name, value=win_text.format( self.player.member.display_name), inline=False) embed.set_footer( text=f"Use `/play {self.command_name}` for another game!", icon_url=self.cog.bot.user.avatar_url) elif self.state is GameStates.game_timeout: embed.set_footer(text="Game ended at", icon_url=self.cog.bot.user.avatar_url) embed.timestamp = datetime.utcnow() else: embed.description = "Press button to play!" embed.set_footer(text="Game ends at", icon_url=self.cog.bot.user.avatar_url) embed.timestamp = self.started_at + timedelta( seconds=self.cog.global_timeout) if self.player: text = f"{self.player.member.mention} | {self.player.member}" else: text = "Nobody wanted to play :(" if self.state is GameStates.game_timeout \ else "Free spot!\nMake a move to join the game!" title = "Player" if self.state is GameStates.has_winner and winner_state: title += "- winner!" embed.add_field(name=title, value=text) return embed
async def emote_list(self, ctx): """Shows list of available emotes.""" if not self.has_thumbnail: await ctx.send( "There is no available emotes. " "`Add them with /emote add name:<name> attachment_link:<link>`" ) return await ctx.defer() embed = utils.bot_embed(self.bot) embed.title = f"Available emotes ({len(self.emotes)} total)" embed.description = "Click on the image to enlarge" embed.set_footer( text="Use `/emote pick <emote>` to send those emotes!", icon_url=self.bot.user.avatar_url) embed.set_image(url="attachment://emotes.png") await ctx.send(embed=embed, file=discord.File(self.emotes_thumbnail, filename="emotes.png"))
async def make_bot_status_embed(self) -> discord.Embed: now = datetime.utcnow() started_at = self.bot.get_cog("Greetings").get_start_time() last_active = self.bot.get_cog("Greetings").get_last_activity_time() delta = relativedelta.relativedelta(now, started_at) embed = utils.bot_embed(self.bot) embed.title = "Bot check results" no = "Not available" git_hash = (await utils.run(f"git describe --always"))[0] or no commits_behind = (await utils.run( f"git fetch; " f"git rev-list HEAD...origin/master --count"))[0] commits_behind = commits_behind.strip() commits_behind = int( commits_behind) or "Up to date" if commits_behind else no embed.add_field(name="Version", value=utils.format_lines({ "Version number": self.bot.version, "Commit Hash": git_hash.strip(), "Commits Behind": commits_behind, })) embed.add_field( name="Statistics", value=utils.format_lines({ "Servers": len(self.bot.guilds), "Users": len(self.bot.users), "Admins": len(await self.bot.get_cog("Permissions").get_permissions_list()) })) embed.add_field(name="Running", value=utils.format_lines({ "Since": f"{started_at.strftime(utils.time_format)} (GMT)", "For": display_delta(delta), "Last activity check": f"{last_active.strftime(utils.time_format)} (GMT)", }), inline=False) extensions = {} total_loaded = 0 for key in self.bot.initial_extensions: loaded = key in self.bot.extensions extensions[ f"{'+' if loaded else '-'} {key}"] = "Online" if loaded else "Offline" total_loaded += loaded embed.add_field( name=f"Loaded extensions " f"({total_loaded:02d}/{len(self.bot.initial_extensions):02d} online)", value=utils.format_lines(extensions, lang="diff", delimiter=" :")) process = psutil.Process(os.getpid()) with process.oneshot(): memory = process.memory_info().rss memory_p = process.memory_percent() cpu_p = process.cpu_percent() disk_info = psutil.disk_usage(os.getcwd()) storage_size, _ = await utils.run(f"du -s {os.getcwd()}") if storage_size: storage_size = int(storage_size.split("\t")[0].strip()) storage_size = utils.format_size(storage_size * 1024) else: storage_size = no embed.add_field(name="Resource consumption", value=utils.format_lines({ "CPU": f"{cpu_p:.1%}", "RAM": f"{utils.format_size(memory)} ({memory_p:.1f}%)", "Disk": f"{utils.format_size(disk_info.used)} " f"({disk_info.percent:.1f}%)", "Storage": storage_size, "Latency": f"{math.ceil(self.bot.latency * 100)} ms" })) embed.set_footer(text="Yours truly!", icon_url=self.bot.user.avatar_url) return embed
def main_menu_embed(self): embed = utils.bot_embed(self.cog.bot) embed.title = "Uno!"