Skip to content

Commit

Permalink
[config] /config silent, surpress wrong name and errors
Browse files Browse the repository at this point in the history
* adding the silent config logic to countryballs/components

* making a new sql migration file for silent config

* adding silent to admin/resources

* adding silent to core/models

* adding the silent logic to config/components

* adding the command /config silent

* removing unnecessary comma

* removing unnecessary whitespaces

* removing /config silent, adding silent as an arg in /config channel

* removing the unnecessary silent activation embed

* Update command description

---------

Co-authored-by: flaree <[email protected]>
  • Loading branch information
imtherealF1 and flaree authored Sep 6, 2024
1 parent 44573fc commit 9e8f318
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ballsdex/core/admin/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class GuildConfigResource(Model):
placeholder="Filter by ID",
),
]
fields = ["guild_id", "spawn_channel", "enabled"]
fields = ["guild_id", "spawn_channel", "enabled", "silent"]


@app.register
Expand Down
4 changes: 4 additions & 0 deletions ballsdex/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class GuildConfig(models.Model):
enabled = fields.BooleanField(
description="Whether the bot will spawn countryballs in this guild", default=True
)
silent = fields.BooleanField(
description="Whether the responses of guesses get sent as ephemeral or not",
default=False,
)


class Regime(models.Model):
Expand Down
5 changes: 4 additions & 1 deletion ballsdex/packages/config/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async def channel(
self,
interaction: discord.Interaction,
channel: Optional[discord.TextChannel] = None,
silent: bool = False,
):
"""
Set or change the channel where countryballs will spawn.
Expand All @@ -54,6 +55,8 @@ async def channel(
----------
channel: discord.TextChannel
The channel you want to set, current one if not specified.
silent: bool
Whether to config a server to suppress wrong name and error messages.
"""
user = cast(discord.Member, interaction.user)

Expand All @@ -66,7 +69,7 @@ async def channel(
)
return

view = AcceptTOSView(interaction, channel, user)
view = AcceptTOSView(interaction, channel, user, silent=silent)
message = await channel.send(embed=activation_embed, view=view)
view.message = message

Expand Down
3 changes: 3 additions & 0 deletions ballsdex/packages/config/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ def __init__(
interaction: discord.Interaction,
channel: discord.TextChannel,
new_player: discord.Member,
silent: bool = False,
):
super().__init__()
self.original_interaction = interaction
self.channel = channel
self.new_player = new_player
self.silent = silent
self.message: Optional[discord.Message] = None

self.add_item(
Expand Down Expand Up @@ -55,6 +57,7 @@ async def interaction_check(self, interaction: discord.Interaction) -> bool:
async def accept_button(self, interaction: discord.Interaction, button: discord.ui.Button):
config, created = await GuildConfig.get_or_create(guild_id=interaction.guild_id)
config.spawn_channel = self.channel.id # type: ignore
config.silent = self.silent
await config.save()
interaction.client.dispatch(
"ballsdex_settings_change", interaction.guild, channel=self.channel
Expand Down
21 changes: 15 additions & 6 deletions ballsdex/packages/countryballs/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from prometheus_client import Counter
from tortoise.timezone import now as datetime_now

from ballsdex.core.models import BallInstance, Player, specials
from ballsdex.core.models import BallInstance, GuildConfig, Player, specials
from ballsdex.settings import settings

if TYPE_CHECKING:
Expand All @@ -37,27 +37,34 @@ def __init__(self, ball: "CountryBall", button: CatchButton):
self.button = button

async def on_error(self, interaction: discord.Interaction, error: Exception, /) -> None:
config = await GuildConfig.get(guild_id=interaction.guild_id)
log.exception("An error occured in countryball catching prompt", exc_info=error)
if interaction.response.is_done():
await interaction.followup.send(
f"An error occured with this {settings.collectible_name}."
f"An error occured with this {settings.collectible_name}.",
ephemeral=config.silent,
)
else:
await interaction.response.send_message(
f"An error occured with this {settings.collectible_name}."
f"An error occured with this {settings.collectible_name}.",
ephemeral=config.silent,
)

async def on_submit(self, interaction: discord.Interaction["BallsDexBot"]):
# TODO: use lock
config = await GuildConfig.get(guild_id=interaction.guild_id)

if self.ball.catched:
await interaction.response.send_message(
f"{interaction.user.mention} I was caught already!"
f"{interaction.user.mention} I was caught already!",
ephemeral=config.silent,
)
return

if self.ball.model.catch_names:
possible_names = (self.ball.name.lower(), *self.ball.model.catch_names.split(";"))
else:
possible_names = (self.ball.name.lower(),)

if self.name.value.lower().strip() in possible_names:
self.ball.catched = True
await interaction.response.defer(thinking=True)
Expand All @@ -83,7 +90,9 @@ async def on_submit(self, interaction: discord.Interaction["BallsDexBot"]):
self.button.disabled = True
await interaction.followup.edit_message(self.ball.message.id, view=self.button.view)
else:
await interaction.response.send_message(f"{interaction.user.mention} Wrong name!")
await interaction.response.send_message(
f"{interaction.user.mention} Wrong name!", ephemeral=config.silent
)

async def catch_ball(
self, bot: "BallsDexBot", user: discord.Member
Expand Down
4 changes: 4 additions & 0 deletions migrations/models/32_20240830012954_update.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- upgrade --
ALTER TABLE "guildconfig" ADD "silent" BOOL NOT NULL DEFAULT False;
-- downgrade --
ALTER TABLE "guildconfig" DROP COLUMN "silent";

0 comments on commit 9e8f318

Please sign in to comment.