Mod cog: Option to show an extra field with custom content on the ban embed (#6593)

Co-authored-by: Michael Oliveira <34169552+Flame442@users.noreply.github.com>
This commit is contained in:
Myra 2025-08-10 23:54:51 +02:00 committed by GitHub
parent 2dbbb51208
commit 3fd23d4163
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 206 additions and 11 deletions

View File

@ -253,7 +253,23 @@ modset dm
.. code-block:: none .. code-block:: none
[p]modset dm [enabled] [p]modset dm
**Description**
Settings for messaging the user when being kicked or banned.
.. _mod-command-modset-dm-sendmessage:
"""""""""""""""""""""
modset dm sendmessage
"""""""""""""""""""""
**Syntax**
.. code-block:: none
[p]modset dm sendmessage [enabled]
**Description** **Description**
@ -266,6 +282,72 @@ and reason as to why they were kicked/banned.
* ``[enabled]``: Whether a message should be sent to a user when they are kicked/banned. |bool-input| * ``[enabled]``: Whether a message should be sent to a user when they are kicked/banned. |bool-input|
.. _mod-command-modset-banshowextrafield:
"""""""""""""""""""""""""""
modset dm banshowextrafield
"""""""""""""""""""""""""""
**Syntax**
.. code-block:: none
[p]modset dm banshowextrafield [enabled]
**Description**
Toggle whether to show an extra customizable field when banning.
This can be used to add additional information for the banned user, such as a ban appeal link.
**Arguments**
* ``[enabled]``: If an extra customizable embed field should appear when banning. |bool-input|
.. _mod-command-modset-banextrafieldtitle:
""""""""""""""""""""""""""""
modset dm banextrafieldtitle
""""""""""""""""""""""""""""
**Syntax**
.. code-block:: none
[p]modset dm banextrafieldtitle [title]
**Description**
Set the title for the optional extra embed on ban.
Cannot be over 252 characters long.
**Arguments**
* ``[title]``: The title of the embed field. Can by any string of text under 252 charcters long.
.. _mod-command-modset-banextrafieldcontents:
"""""""""""""""""""""""""""""""
modset dm banextrafieldcontents
"""""""""""""""""""""""""""""""
**Syntax**
.. code-block:: none
[p]modset dm banextrafieldcontents [contents]
**Description**
Set the contents for the optional extra embed on ban
Cannot be over 1024 characters long.
**Arguments**
* ``[contents]``: The contents of the embed field. Can by any string of text under 1024 charcters long.
.. _mod-command-modset-requirereason: .. _mod-command-modset-requirereason:
"""""""""""""""""""" """"""""""""""""""""

View File

@ -143,6 +143,8 @@ class KickBanMixin(MixinMeta):
toggle = await self.config.guild(guild).dm_on_kickban() toggle = await self.config.guild(guild).dm_on_kickban()
if toggle: if toggle:
extra_embed = await self.config.guild(guild).ban_show_extra()
with contextlib.suppress(discord.HTTPException): with contextlib.suppress(discord.HTTPException):
em = discord.Embed( em = discord.Embed(
title=bold(_("You have been banned from {guild}.").format(guild=guild)), title=bold(_("You have been banned from {guild}.").format(guild=guild)),
@ -153,6 +155,17 @@ class KickBanMixin(MixinMeta):
value=reason if reason is not None else _("No reason was given."), value=reason if reason is not None else _("No reason was given."),
inline=False, inline=False,
) )
if extra_embed:
extra_embed_title = await self.config.guild(guild).ban_extra_embed_title()
extra_embed_contents = await self.config.guild(
guild
).ban_extra_embed_contents()
em.add_field(
name=bold(extra_embed_title, escape_formatting=False),
value=extra_embed_contents,
inline=False,
)
await user.send(embed=em) await user.send(embed=em)
ban_type = "ban" ban_type = "ban"
@ -658,16 +671,38 @@ class KickBanMixin(MixinMeta):
with contextlib.suppress(discord.HTTPException): with contextlib.suppress(discord.HTTPException):
# We don't want blocked DMs preventing us from banning # We don't want blocked DMs preventing us from banning
msg = _("You have been temporarily banned from {server_name} until {date}.").format(
server_name=guild.name, date=discord.utils.format_dt(unban_time) extra_embed = await self.config.guild(guild).ban_show_extra()
em = discord.Embed(
title=bold(
_("You have been temporarily banned from {guild} until {date}.").format(
guild=guild, date=discord.utils.format_dt(unban_time)
)
),
color=await self.bot.get_embed_color(member),
)
em.add_field(
name=_("**Reason**"),
value=reason if reason is not None else _("No reason was given."),
inline=False,
) )
if guild_data["dm_on_kickban"] and reason:
msg += _("\n\n**Reason:** {reason}").format(reason=reason)
if invite: if invite:
msg += _("\n\nHere is an invite for when your ban expires: {invite_link}").format( em.add_field(
invite_link=invite name=bold(_("Here is an invite for when your ban expires")),
value=invite,
inline=False,
) )
await member.send(msg) if extra_embed:
extra_embed_title = await self.config.guild(guild).ban_extra_embed_title()
extra_embed_contents = await self.config.guild(guild).ban_extra_embed_contents()
em.add_field(
name=bold(extra_embed_title, escape_formatting=False),
value=extra_embed_contents,
inline=False,
)
await member.send(embed=em)
audit_reason = get_audit_reason(author, reason, shorten=True) audit_reason = get_audit_reason(author, reason, shorten=True)

View File

@ -61,6 +61,9 @@ class Mod(
"default_days": 0, "default_days": 0,
"default_tempban_duration": 60 * 60 * 24, "default_tempban_duration": 60 * 60 * 24,
"track_nicknames": True, "track_nicknames": True,
"ban_show_extra": False,
"ban_extra_embed_title": "Message from staff",
"ban_extra_embed_contents": "Please set me",
} }
default_channel_settings = {"ignored": False} default_channel_settings = {"ignored": False}

View File

@ -48,6 +48,9 @@ class ModSettings(MixinMeta):
dm_on_kickban = data["dm_on_kickban"] dm_on_kickban = data["dm_on_kickban"]
default_days = data["default_days"] default_days = data["default_days"]
default_tempban_duration = data["default_tempban_duration"] default_tempban_duration = data["default_tempban_duration"]
ban_show_extra = data["ban_show_extra"]
ban_extra_embed_title = data["ban_extra_embed_title"]
ban_extra_embed_contents = data["ban_extra_embed_contents"]
if not track_all_names and track_nicknames: if not track_all_names and track_nicknames:
yes_or_no = _("Overridden by another setting") yes_or_no = _("Overridden by another setting")
else: else:
@ -98,9 +101,18 @@ class ModSettings(MixinMeta):
) )
else: else:
msg += _("Default message history delete on ban: Don't delete any\n") msg += _("Default message history delete on ban: Don't delete any\n")
msg += _("Default tempban duration: {duration}").format( msg += _("Default tempban duration: {duration}\n").format(
duration=humanize_timedelta(seconds=default_tempban_duration) duration=humanize_timedelta(seconds=default_tempban_duration)
) )
msg += _("Show optional information field in embed: {yes_or_no}\n").format(
yes_or_no=_("Yes") if ban_show_extra else _("No")
)
msg += _("Title of the optional extra field: {ban_embed_title}\n").format(
ban_embed_title=ban_extra_embed_title if ban_extra_embed_title else _("None")
)
msg += _("Contents of the optional extra field: {ban_embed_contents}").format(
ban_embed_contents=ban_extra_embed_contents if ban_extra_embed_contents else _("None")
)
await ctx.send(box(msg)) await ctx.send(box(msg))
@modset.command() @modset.command()
@ -347,9 +359,15 @@ class ModSettings(MixinMeta):
) )
) )
@modset.command() @modset.group()
@commands.guild_only() @commands.guild_only()
async def dm(self, ctx: commands.Context, enabled: bool = None): async def dm(self, ctx: commands.Context):
"""
Settings for messaging the user when being kicked or banned.
"""
@dm.command(name="sendmessage")
async def dm_sendmessage(self, ctx: commands.Context, enabled: bool = None):
"""Toggle whether a message should be sent to a user when they are kicked/banned. """Toggle whether a message should be sent to a user when they are kicked/banned.
If this option is enabled, the bot will attempt to DM the user with the guild name If this option is enabled, the bot will attempt to DM the user with the guild name
@ -370,6 +388,63 @@ class ModSettings(MixinMeta):
_("Bot will no longer attempt to send a DM to user before kick and ban.") _("Bot will no longer attempt to send a DM to user before kick and ban.")
) )
@dm.command(name="banshowextrafield")
async def dm_banshowextrafield(self, ctx: commands.Context, enabled: bool = None):
"""
Toggle whether to show an extra customizable field when banning.
This can be used to add additional information for the banned user, such as a ban appeal link.
"""
guild = ctx.guild
if enabled is None:
setting = await self.config.guild(guild).ban_show_extra()
await ctx.send(
_("The extra embed field is currently set to: {setting}").format(setting=setting)
)
return
await self.config.guild(guild).ban_show_extra.set(enabled)
if enabled:
await ctx.send(
_(
"An extra field will be shown when banning. Configure it with `{prefix}modset dm banextrafieldtitle` and `{prefix}modset dm banextrafieldcontents`"
).format(prefix=ctx.prefix)
)
else:
await ctx.send(_("An extra field will be no longer be shown when banning."))
@dm.command(name="banextrafieldtitle")
async def dm_banextrafieldtitle(self, ctx: commands.Context, *, title: str) -> None:
"""
Set the title for the optional extra embed on ban.
Cannot be over 252 characters long.
"""
guild = ctx.guild
# Bolding the text is 4 characters (**bolded**)
# All the bold function used in the embeds does is add those star characters and some other convenience stuffs.
# Such as escaping formatting.
if len(title) > 252:
await ctx.send(_("Embed title cannot be over 252 characters long."))
else:
await self.config.guild(guild).ban_extra_embed_title.set(title)
await ctx.send(_("Embed Title has been set to `{title}`").format(title=title))
@dm.command(name="banextrafieldcontents")
async def dm_banextrafieldcontents(self, ctx: commands.Context, *, contents: str) -> None:
"""
Set the contents for the optional extra embed on ban
Cannot be over 1024 characters long.
"""
guild = ctx.guild
if len(contents) > 1024:
await ctx.send(_("Embed contents cannot be over 1024 characters long."))
else:
await self.config.guild(guild).ban_extra_embed_contents.set(contents)
await ctx.send(
_("Embed Contents has been set to `{contents}`").format(contents=contents)
)
@modset.command() @modset.command()
@commands.guild_only() @commands.guild_only()
async def requirereason(self, ctx: commands.Context, enabled: bool = None): async def requirereason(self, ctx: commands.Context, enabled: bool = None):