Skip to content

Commit a5e7eb3

Browse files
Implement command to display Discord server invite link (#493)
1 parent a5e7b17 commit a5e7eb3

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ GROUP_SHORT_NAME=[Replace with the short colloquial name of your community group
2626
# Must be a valid URL
2727
PURCHASE_MEMBERSHIP_URL=[Replace with your group\'s purchase=membership URL]
2828

29+
# The invite link URL to allow users to join your community group's Discord server
30+
# Must be a valid URL
31+
DISCORD_INVITE_URL=[Replace with your group\'s Discord server invite link]
32+
2933

3034
# The minimum level that logs must meet in order to be logged to the console output stream
3135
# One of: DEBUG, INFO, WARNING, ERROR, CRITICAL

cogs/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
InductSendMessageCog,
2929
InductSlashCommandCog,
3030
)
31+
from .invite_link import InviteLinkCommandCog
3132
from .kill import KillCommandCog
3233
from .make_applicant import MakeApplicantContextCommandsCog, MakeApplicantSlashCommandCog
3334
from .make_member import MakeMemberCommandCog, MemberCountCommandCog
@@ -63,6 +64,7 @@
6364
"InductContextCommandsCog",
6465
"InductSendMessageCog",
6566
"InductSlashCommandCog",
67+
"InviteLinkCommandCog",
6668
"KillCommandCog",
6769
"MakeApplicantContextCommandsCog",
6870
"MakeApplicantSlashCommandCog",
@@ -103,6 +105,7 @@ def setup(bot: "TeXBot") -> None:
103105
InductSendMessageCog,
104106
InductSlashCommandCog,
105107
KillCommandCog,
108+
InviteLinkCommandCog,
106109
MakeApplicantContextCommandsCog,
107110
MakeApplicantSlashCommandCog,
108111
MakeMemberCommandCog,

cogs/invite_link.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Contains cog classes for any invite link display interactions."""
2+
3+
from typing import TYPE_CHECKING
4+
5+
import discord
6+
7+
from config import settings
8+
from utils import TeXBotBaseCog
9+
10+
if TYPE_CHECKING:
11+
from collections.abc import Sequence
12+
13+
from utils import TeXBotApplicationContext
14+
15+
__all__: "Sequence[str]" = ("InviteLinkCommandCog",)
16+
17+
18+
class InviteLinkCommandCog(TeXBotBaseCog):
19+
"""Cog class that defines the "/invite-link" command and its call-back method."""
20+
21+
@discord.slash_command( # type: ignore[no-untyped-call, misc]
22+
name="invite-link", description="Display the invite link to this server."
23+
)
24+
async def invite_link(self, ctx: "TeXBotApplicationContext") -> None: # type: ignore[misc]
25+
"""Definition & callback response of the "invite-link" command."""
26+
await ctx.respond(
27+
(
28+
f"Invite your friends to the {self.bot.group_short_name} Discord server: "
29+
f"{settings['DISCORD_INVITE_URL']}"
30+
),
31+
ephemeral=False,
32+
)

config.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,21 @@ def _setup_membership_perks_url(cls) -> None:
291291

292292
cls._settings["MEMBERSHIP_PERKS_URL"] = raw_membership_perks_url
293293

294+
@classmethod
295+
def _setup_discord_invite_url(cls) -> None:
296+
raw_discord_invite_url: str | None = os.getenv("DISCORD_INVITE_URL")
297+
298+
DISCORD_INVITE_URL_IS_VALID: Final[bool] = bool(
299+
not raw_discord_invite_url or validators.url(raw_discord_invite_url),
300+
)
301+
if not DISCORD_INVITE_URL_IS_VALID:
302+
INVALID_DISCORD_INVITE_URL_MESSAGE: Final[str] = (
303+
"DISCORD_INVITE_URL must be a valid URL."
304+
)
305+
raise ImproperlyConfiguredError(INVALID_DISCORD_INVITE_URL_MESSAGE)
306+
307+
cls._settings["DISCORD_INVITE_URL"] = raw_discord_invite_url
308+
294309
@classmethod
295310
def _setup_ping_command_easter_egg_probability(cls) -> None:
296311
INVALID_PING_COMMAND_EASTER_EGG_PROBABILITY_MESSAGE: Final[str] = (
@@ -738,6 +753,7 @@ def _setup_env_variables(cls) -> None:
738753
cls._setup_members_list_auth_session_cookie()
739754
cls._setup_membership_perks_url()
740755
cls._setup_purchase_membership_url()
756+
cls._setup_discord_invite_url()
741757
cls._setup_send_introduction_reminders()
742758
cls._setup_send_introduction_reminders_delay()
743759
cls._setup_send_introduction_reminders_interval()

0 commit comments

Comments
 (0)