diff options
| author | 2020-09-21 16:56:39 +0800 | |
|---|---|---|
| committer | 2020-09-21 16:56:39 +0800 | |
| commit | 4615571c71b1cc4571dde3fdb08223f8e3355d57 (patch) | |
| tree | 57bab84418e439277308fa19b558bcae8e6e341c | |
| parent | Merge pull request #1103 from python-discord/feat/backend/160/cog-subdirs (diff) | |
Move Channels information to embed field.
| -rw-r--r-- | bot/exts/info/information.py | 28 | ||||
| -rw-r--r-- | tests/bot/exts/info/test_information.py | 20 |
2 files changed, 26 insertions, 22 deletions
diff --git a/bot/exts/info/information.py b/bot/exts/info/information.py index 55ecb2836..b7c96acbd 100644 --- a/bot/exts/info/information.py +++ b/bot/exts/info/information.py @@ -3,7 +3,6 @@ import logging import pprint import textwrap from collections import Counter, defaultdict -from string import Template from typing import Any, Mapping, Optional, Tuple, Union from discord import ChannelType, Colour, CustomActivity, Embed, Guild, Member, Message, Role, Status, utils @@ -143,37 +142,37 @@ class Information(Cog): @command(name="server", aliases=["server_info", "guild", "guild_info"]) async def server_info(self, ctx: Context) -> None: """Returns an embed full of server information.""" + embed = Embed( + colour=Colour.blurple(), + title="Server Information", + ) + created = time_since(ctx.guild.created_at, precision="days") features = ", ".join(ctx.guild.features) region = ctx.guild.region roles = len(ctx.guild.roles) member_count = ctx.guild.member_count - channel_counts = self.get_channel_type_counts(ctx.guild) # How many of each user status? statuses = Counter(member.status for member in ctx.guild.members) - embed = Embed(colour=Colour.blurple()) # How many staff members and staff channels do we have? staff_member_count = len(ctx.guild.get_role(constants.Roles.helpers).members) staff_channel_count = self.get_staff_channel_count(ctx.guild) - # Because channel_counts lacks leading whitespace, it breaks the dedent if it's inserted directly by the - # f-string. While this is correctly formated by Discord, it makes unit testing difficult. To keep the formatting - # without joining a tuple of strings we can use a Template string to insert the already-formatted channel_counts - # after the dedent is made. - embed.description = Template( - textwrap.dedent(f""" - **Server information** + total_channels = len(ctx.guild.channels) + channel_counts = ( + f"{self.get_channel_type_counts(ctx.guild)}\n" + f"Staff channels: {staff_channel_count}" + ) + embed.add_field(name=f"Channels: {total_channels}", value=channel_counts) + + embed.description = textwrap.dedent(f""" Created: {created} Voice region: {region} Features: {features} - **Channel counts** - $channel_counts - Staff channels: {staff_channel_count} - **Member counts** Members: {member_count:,} Staff members: {staff_member_count} @@ -185,7 +184,6 @@ class Information(Cog): {constants.Emojis.status_dnd} {statuses[Status.dnd]:,} {constants.Emojis.status_offline} {statuses[Status.offline]:,} """) - ).substitute({"channel_counts": channel_counts}) embed.set_thumbnail(url=ctx.guild.icon_url) await ctx.send(embed=embed) diff --git a/tests/bot/exts/info/test_information.py b/tests/bot/exts/info/test_information.py index ba8d5d608..7a65d6c2b 100644 --- a/tests/bot/exts/info/test_information.py +++ b/tests/bot/exts/info/test_information.py @@ -139,21 +139,15 @@ class InformationCogTests(unittest.TestCase): _, kwargs = self.ctx.send.call_args embed = kwargs.pop('embed') self.assertEqual(embed.colour, discord.Colour.blurple()) + self.assertEqual(embed.title, "Server Information") self.assertEqual( embed.description, textwrap.dedent( f""" - **Server information** Created: {time_since_patch.return_value} Voice region: {self.ctx.guild.region} Features: {', '.join(self.ctx.guild.features)} - **Channel counts** - Category channels: 1 - Text channels: 1 - Voice channels: 1 - Staff channels: 0 - **Member counts** Members: {self.ctx.guild.member_count:,} Staff members: 0 @@ -167,6 +161,18 @@ class InformationCogTests(unittest.TestCase): """ ) ) + + channel_field = embed.fields[0] + self.assertEqual(channel_field.name, "Channels: 3") + self.assertEqual( + channel_field.value, + textwrap.dedent(""" + Category channels: 1 + Text channels: 1 + Voice channels: 1 + Staff channels: 0 + """).strip(), + ) self.assertEqual(embed.thumbnail.url, 'a-lemon.jpg') |