From b73d11b2aee32e8fd64316b9ac270b83229c66b9 Mon Sep 17 00:00:00 2001 From: Nathaniel Landau Date: Tue, 26 Sep 2023 16:39:20 -0400 Subject: [PATCH] feat(misc): add `/server_info` command (#62) --- src/valentina/cogs/misc.py | 85 ++++++++++++++++++++++++++++- src/valentina/models/probability.py | 18 ++++-- src/valentina/models/statistics.py | 7 ++- tests/test_utils.py | 2 +- 4 files changed, 102 insertions(+), 10 deletions(-) diff --git a/src/valentina/cogs/misc.py b/src/valentina/cogs/misc.py index 728a2b8b..be01b6c3 100644 --- a/src/valentina/cogs/misc.py +++ b/src/valentina/cogs/misc.py @@ -2,7 +2,9 @@ """Miscellaneous commands.""" import random +import arrow import discord +import inflect import semver from discord.commands import Option from discord.ext import commands @@ -11,9 +13,12 @@ from valentina.models import Probability, Statistics from valentina.models.bot import Valentina from valentina.models.db_tables import Character, Macro +from valentina.utils import errors from valentina.utils.changelog_parser import ChangelogParser from valentina.utils.options import select_changelog_version_1, select_changelog_version_2 +p = inflect.engine() + class Misc(commands.Cog): """Miscellaneous commands.""" @@ -21,6 +26,81 @@ class Misc(commands.Cog): def __init__(self, bot: Valentina) -> None: self.bot: Valentina = bot + @commands.slash_command(name="server_info", description="View information about the server") + async def server_info( + self, + ctx: discord.ApplicationContext, + hidden: Option( + bool, + description="Make the probability only visible to you (default False)", + default=False, + ), + ) -> None: + """View information about the server.""" + # Compute data + created_on = arrow.get(ctx.guild.created_at) + player_characters = self.bot.char_svc.fetch_all_player_characters(ctx) + storyteller_characters = self.bot.char_svc.fetch_all_storyteller_characters(ctx) + num_characters = len(player_characters) + len(storyteller_characters) + campaigns = self.bot.campaign_svc.fetch_all(ctx) + num_campaigns = len(campaigns) + try: + active_campaign = self.bot.campaign_svc.fetch_active(ctx) + except errors.NoActiveCampaignError: + active_campaign = None + roll_stats = Statistics(ctx) + + # Build the Embed + embed = discord.Embed( + description=f"## {ctx.guild.name} Information", color=EmbedColor.INFO.value + ) + embed.add_field( + name="", + value=f"""\ +```scala +Created: {created_on.humanize()} ({created_on.format('YYYY-MM-DD')}) +Owner : {ctx.guild.owner.display_name} +Members: {ctx.guild.member_count} +Roles : {', '.join([f'@{x.name}' if not x.name.startswith('@') else x.name for x in ctx.guild.roles if not x.is_bot_managed() and not x.is_integration() and not x.is_default()][::-1])} +``` +""", + inline=False, + ) + + embed.add_field( + name="Campaigns", + value=f"""\ +```scala +Total Campaigns: {num_campaigns} +Active Campaign: {active_campaign.name if active_campaign else 'None'} +``` +""", + inline=True, + ) + + embed.add_field( + name="Characters", + value=f"""\ +```scala +Total Characters : {num_characters} +Player Characters : {len(player_characters)} +Storyteller Characters: {len(storyteller_characters)} +``` +""", + inline=True, + ) + + embed.add_field( + name="Roll Statistics", + value=roll_stats.get_text(with_title=False), + inline=False, + ) + embed.set_footer( + text=f"Requested by {ctx.author}", + icon_url=ctx.author.display_avatar.url, + ) + await ctx.respond(embed=embed, ephemeral=hidden) + @commands.slash_command(name="probability", description="Calculate the probability of a roll") async def probability( self, @@ -121,7 +201,10 @@ async def user_info( color=EmbedColor.INFO.value, ) embed.set_thumbnail(url=target.display_avatar.url) - embed.set_footer(text=f"Requested by {ctx.author.display_name}") + embed.set_footer( + text=f"Requested by {ctx.author}", + icon_url=ctx.author.display_avatar.url, + ) embed.timestamp = discord.utils.utcnow() await ctx.respond(embed=embed, ephemeral=hidden) diff --git a/src/valentina/models/probability.py b/src/valentina/models/probability.py index 7104c9ba..4b3b960c 100644 --- a/src/valentina/models/probability.py +++ b/src/valentina/models/probability.py @@ -143,11 +143,14 @@ def _get_description(self) -> str: Botch (1): {self.probabilities['botch_dice']:.2f}% ``` -> DEFINITIONS: -> - _Critical Success_: More successes than dice rolled -> - _Success_: At least one success after all dice are tallied -> - _Failure_: Zero successes after all dice are tallied -> - _Botch_: Negative successes after all dice are tallied +> - Probabilities based on {self.trials:,} trials +> - Definitions +> - _Critical Success_: More successes than dice rolled +> - _Success_: At least one success after all dice are tallied +> - _Failure_: Zero successes after all dice are tallied +> - _Botch_: Negative successes after all dice are tallied + + """ async def get_embed(self) -> discord.Embed: @@ -161,6 +164,9 @@ async def get_embed(self) -> discord.Embed: color=EmbedColor.INFO.value, ) embed.description = self._get_description() - embed.set_footer(text=f"Probabilities based on {self.trials:,} trials") + embed.set_footer( + text=f"Requested by {self.ctx.author}", + icon_url=self.ctx.author.display_avatar.url, + ) return embed diff --git a/src/valentina/models/statistics.py b/src/valentina/models/statistics.py index 1edf5a02..25b09a60 100644 --- a/src/valentina/models/statistics.py +++ b/src/valentina/models/statistics.py @@ -117,7 +117,7 @@ def get_text(self, with_title: bool = True) -> str: return msg msg += f"""\ -```json +```python Total Rolls: {'.':.<{25 - 12}} {self.total_rolls} Critical Success Rolls: {'.':.<{25 -23}} {self.criticals:<3} ({self.criticals / self.total_rolls * 100:.2f}%) Successful Rolls: {'.':.<{25 - 17}} {self.successes:<3} ({self.successes / self.total_rolls * 100:.2f}%) @@ -148,5 +148,8 @@ async def get_embed(self) -> discord.Embed: timestamp=discord.utils.utcnow(), ) embed.set_thumbnail(url=self.thumbnail) - + embed.set_footer( + text=f"Requested by {self.ctx.author}", + icon_url=self.ctx.author.display_avatar.url, + ) return embed diff --git a/tests/test_utils.py b/tests/test_utils.py index b322f4ec..d5313844 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -48,6 +48,6 @@ async def test_get_embed(self, mock_ctx): assert isinstance(embed, discord.Embed) result = embed.to_dict() - assert result["footer"]["text"] == IsStr(regex=r"Probabilities based on [0-9,]+ trials") + assert result["footer"]["text"] == IsStr(regex=r"Requested by .+") assert result["description"] == IsStr() assert isinstance(result["fields"], list)