From 2cdc920d21b7fba00207031b52703ee76aaa6f28 Mon Sep 17 00:00:00 2001 From: Matt Norton Date: Sun, 18 May 2025 14:08:31 +0100 Subject: [PATCH 1/2] Add command to display Discord server invite link --- .env.example | 4 ++++ cogs/__init__.py | 3 +++ cogs/link.py | 30 ++++++++++++++++++++++++++++++ config.py | 16 ++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 cogs/link.py diff --git a/.env.example b/.env.example index c1149c346..513acfcdd 100644 --- a/.env.example +++ b/.env.example @@ -26,6 +26,10 @@ GROUP_SHORT_NAME=[Replace with the short colloquial name of your community group # Must be a valid URL PURCHASE_MEMBERSHIP_URL=[Replace with your group\'s purchase=membership URL] +# The invite link URL to allow users to join your community group's Discord server +# Must be a valid URL +DISCORD_INVITE_URL=[Replace with your group\'s Discord server invite link] + # The minimum level that logs must meet in order to be logged to the console output stream # One of: DEBUG, INFO, WARNING, ERROR, CRITICAL diff --git a/cogs/__init__.py b/cogs/__init__.py index 068641872..5fd8f04f7 100644 --- a/cogs/__init__.py +++ b/cogs/__init__.py @@ -29,6 +29,7 @@ InductSlashCommandCog, ) from .kill import KillCommandCog +from .link import LinkCommandCog from .make_applicant import MakeApplicantContextCommandsCog, MakeApplicantSlashCommandCog from .make_member import MakeMemberCommandCog, MemberCountCommandCog from .ping import PingCommandCog @@ -64,6 +65,7 @@ "InductSendMessageCog", "InductSlashCommandCog", "KillCommandCog", + "LinkCommandCog", "MakeApplicantContextCommandsCog", "MakeApplicantSlashCommandCog", "MakeMemberCommandCog", @@ -103,6 +105,7 @@ def setup(bot: "TeXBot") -> None: InductSendMessageCog, InductSlashCommandCog, KillCommandCog, + LinkCommandCog, MakeApplicantContextCommandsCog, MakeApplicantSlashCommandCog, MakeMemberCommandCog, diff --git a/cogs/link.py b/cogs/link.py new file mode 100644 index 000000000..403d74936 --- /dev/null +++ b/cogs/link.py @@ -0,0 +1,30 @@ +"""Contains cog classes for any invite link display interactions.""" + +from typing import TYPE_CHECKING + +import discord + +from config import settings +from utils import TeXBotBaseCog + +if TYPE_CHECKING: + from collections.abc import Sequence + + from utils import TeXBotApplicationContext + +__all__: "Sequence[str]" = ("LinkCommandCog",) + + +class LinkCommandCog(TeXBotBaseCog): + """Cog class that defines the "/link" command and its call-back method.""" + + @discord.slash_command(description="Display the invite link to this server.") # type: ignore[no-untyped-call, misc] + async def link(self, ctx: "TeXBotApplicationContext") -> None: # type: ignore[misc] + """Definition & callback response of the "link" command.""" + await ctx.respond( + ( + f"Invite your friends to the {self.bot.group_short_name} Discord server: " + f"{settings['DISCORD_INVITE_URL']}" + ), + ephemeral=False, + ) diff --git a/config.py b/config.py index a401be2e6..985063cff 100644 --- a/config.py +++ b/config.py @@ -291,6 +291,21 @@ def _setup_membership_perks_url(cls) -> None: cls._settings["MEMBERSHIP_PERKS_URL"] = raw_membership_perks_url + @classmethod + def _setup_discord_invite_url(cls) -> None: + raw_discord_invite_url: str | None = os.getenv("DISCORD_INVITE_URL") + + DISCORD_INVITE_URL_IS_VALID: Final[bool] = bool( + not raw_discord_invite_url or validators.url(raw_discord_invite_url), + ) + if not DISCORD_INVITE_URL_IS_VALID: + INVALID_DISCORD_INVITE_URL_MESSAGE: Final[str] = ( + "DISCORD_INVITE_URL must be a valid URL." + ) + raise ImproperlyConfiguredError(INVALID_DISCORD_INVITE_URL_MESSAGE) + + cls._settings["DISCORD_INVITE_URL"] = raw_discord_invite_url + @classmethod def _setup_ping_command_easter_egg_probability(cls) -> None: INVALID_PING_COMMAND_EASTER_EGG_PROBABILITY_MESSAGE: Final[str] = ( @@ -738,6 +753,7 @@ def _setup_env_variables(cls) -> None: cls._setup_members_list_auth_session_cookie() cls._setup_membership_perks_url() cls._setup_purchase_membership_url() + cls._setup_discord_invite_url() cls._setup_send_introduction_reminders() cls._setup_send_introduction_reminders_delay() cls._setup_send_introduction_reminders_interval() From 9c3658a264abf9b461e4d75de442ad7e61293e99 Mon Sep 17 00:00:00 2001 From: Matt Norton Date: Sun, 18 May 2025 19:35:19 +0100 Subject: [PATCH 2/2] Rename command --- cogs/__init__.py | 6 +++--- cogs/{link.py => invite_link.py} | 14 ++++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) rename cogs/{link.py => invite_link.py} (51%) diff --git a/cogs/__init__.py b/cogs/__init__.py index 5fd8f04f7..e97071fa9 100644 --- a/cogs/__init__.py +++ b/cogs/__init__.py @@ -28,8 +28,8 @@ InductSendMessageCog, InductSlashCommandCog, ) +from .invite_link import InviteLinkCommandCog from .kill import KillCommandCog -from .link import LinkCommandCog from .make_applicant import MakeApplicantContextCommandsCog, MakeApplicantSlashCommandCog from .make_member import MakeMemberCommandCog, MemberCountCommandCog from .ping import PingCommandCog @@ -64,8 +64,8 @@ "InductContextCommandsCog", "InductSendMessageCog", "InductSlashCommandCog", + "InviteLinkCommandCog", "KillCommandCog", - "LinkCommandCog", "MakeApplicantContextCommandsCog", "MakeApplicantSlashCommandCog", "MakeMemberCommandCog", @@ -105,7 +105,7 @@ def setup(bot: "TeXBot") -> None: InductSendMessageCog, InductSlashCommandCog, KillCommandCog, - LinkCommandCog, + InviteLinkCommandCog, MakeApplicantContextCommandsCog, MakeApplicantSlashCommandCog, MakeMemberCommandCog, diff --git a/cogs/link.py b/cogs/invite_link.py similarity index 51% rename from cogs/link.py rename to cogs/invite_link.py index 403d74936..d36f27ccc 100644 --- a/cogs/link.py +++ b/cogs/invite_link.py @@ -12,15 +12,17 @@ from utils import TeXBotApplicationContext -__all__: "Sequence[str]" = ("LinkCommandCog",) +__all__: "Sequence[str]" = ("InviteLinkCommandCog",) -class LinkCommandCog(TeXBotBaseCog): - """Cog class that defines the "/link" command and its call-back method.""" +class InviteLinkCommandCog(TeXBotBaseCog): + """Cog class that defines the "/invite-link" command and its call-back method.""" - @discord.slash_command(description="Display the invite link to this server.") # type: ignore[no-untyped-call, misc] - async def link(self, ctx: "TeXBotApplicationContext") -> None: # type: ignore[misc] - """Definition & callback response of the "link" command.""" + @discord.slash_command( # type: ignore[no-untyped-call, misc] + name="invite-link", description="Display the invite link to this server." + ) + async def invite_link(self, ctx: "TeXBotApplicationContext") -> None: # type: ignore[misc] + """Definition & callback response of the "invite-link" command.""" await ctx.respond( ( f"Invite your friends to the {self.bot.group_short_name} Discord server: "