Skip to content

Commit

Permalink
feat(misc): add /server_info command (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
natelandau committed Sep 26, 2023
1 parent 2584de5 commit b73d11b
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 10 deletions.
85 changes: 84 additions & 1 deletion src/valentina/cogs/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -11,16 +13,94 @@
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."""

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,
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 12 additions & 6 deletions src/valentina/models/probability.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
7 changes: 5 additions & 2 deletions src/valentina/models/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}%)
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit b73d11b

Please sign in to comment.