diff --git a/ballsdex/core/admin/resources.py b/ballsdex/core/admin/resources.py index b9e52a2ff..c3d0f8f82 100755 --- a/ballsdex/core/admin/resources.py +++ b/ballsdex/core/admin/resources.py @@ -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 diff --git a/ballsdex/core/models.py b/ballsdex/core/models.py index 97d8e5702..896ea8b6c 100755 --- a/ballsdex/core/models.py +++ b/ballsdex/core/models.py @@ -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): diff --git a/ballsdex/packages/config/cog.py b/ballsdex/packages/config/cog.py index 008472b92..dc209812c 100644 --- a/ballsdex/packages/config/cog.py +++ b/ballsdex/packages/config/cog.py @@ -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. @@ -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) @@ -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 diff --git a/ballsdex/packages/config/components.py b/ballsdex/packages/config/components.py index 8767ffaee..62f843741 100644 --- a/ballsdex/packages/config/components.py +++ b/ballsdex/packages/config/components.py @@ -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( @@ -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 diff --git a/ballsdex/packages/countryballs/components.py b/ballsdex/packages/countryballs/components.py index 2420e8c01..c0fa64ab2 100755 --- a/ballsdex/packages/countryballs/components.py +++ b/ballsdex/packages/countryballs/components.py @@ -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: @@ -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) @@ -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 diff --git a/migrations/models/32_20240830012954_update.sql b/migrations/models/32_20240830012954_update.sql new file mode 100644 index 000000000..b7b91f6dd --- /dev/null +++ b/migrations/models/32_20240830012954_update.sql @@ -0,0 +1,4 @@ +-- upgrade -- +ALTER TABLE "guildconfig" ADD "silent" BOOL NOT NULL DEFAULT False; +-- downgrade -- +ALTER TABLE "guildconfig" DROP COLUMN "silent"; \ No newline at end of file