Skip to content

Commit

Permalink
Add exception handling to role reminder message (#226)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Norton <matt@carrotmanmatt.com>
  • Loading branch information
MattyTheHacker and CarrotManMatt committed May 26, 2024
1 parent 241f78f commit 33811b0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 46 deletions.
68 changes: 44 additions & 24 deletions cogs/induct.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@


import contextlib
import logging
import random
import re
from logging import Logger
from typing import Literal

import discord
Expand All @@ -36,6 +38,7 @@
)
from utils.error_capture_decorators import capture_guild_does_not_exist_error

logger: Logger = logging.getLogger("TeX-Bot")

class InductSendMessageCog(TeXBotBaseCog):
"""Cog class that defines the "/induct" command and its call-back method."""
Expand Down Expand Up @@ -96,28 +99,34 @@ async def on_member_update(self, before: discord.Member, after: discord.Member)
if await self.bot.member_role in after.roles:
user_type = "member"

await after.send(
f"**Congrats on joining the {self.bot.group_short_name} Discord server "
f"as a {user_type}!** "
"You now have access to communicate in all the public channels.\n\n"
"Some things to do to get started:\n"
f"1. Check out our rules in {rules_channel_mention}\n"
f"2. Head to {roles_channel_mention} and click on the icons to get "
"optional roles like pronouns and year groups\n"
"3. Change your nickname to whatever you wish others to refer to you as "
"(You can do this by right-clicking your name in the members-list "
"to the right & selecting \"Edit Server Profile\").",
)
if user_type != "member":
try:
await after.send(
f"You can also get yourself an annual membership "
f"to {self.bot.group_full_name} for only £5! "
f"""Just head to {settings["PURCHASE_MEMBERSHIP_URL"]}. """
"You'll get awesome perks like a free T-shirt:shirt:, "
"access to member only events:calendar_spiral: "
f"& a cool green name on the {self.bot.group_short_name} Discord server"
":green_square:! "
f"Checkout all the perks at {settings["MEMBERSHIP_PERKS_URL"]}",
f"**Congrats on joining the {self.bot.group_short_name} Discord server "
f"as a {user_type}!** "
"You now have access to communicate in all the public channels.\n\n"
"Some things to do to get started:\n"
f"1. Check out our rules in {rules_channel_mention}\n"
f"2. Head to {roles_channel_mention} and click on the icons to get "
"optional roles like pronouns and year groups\n"
"3. Change your nickname to whatever you wish others to refer to you as "
"(You can do this by right-clicking your name in the members-list "
"to the right & selecting \"Edit Server Profile\").",
)
if user_type != "member":
await after.send(
f"You can also get yourself an annual membership "
f"to {self.bot.group_full_name} for only £5! "
f"""Just head to {settings["PURCHASE_MEMBERSHIP_URL"]}. """
"You'll get awesome perks like a free T-shirt:shirt:, "
"access to member only events:calendar_spiral: "
f"& a cool green name on the {self.bot.group_short_name} Discord server"
":green_square:! "
f"Checkout all the perks at {settings["MEMBERSHIP_PERKS_URL"]}",
)
except discord.Forbidden:
logger.info(
"Failed to open DM channel to user %s so no welcome message was sent.",
after,
)


Expand Down Expand Up @@ -248,9 +257,20 @@ async def _perform_induction(self, ctx: TeXBotApplicationContext, induction_memb
recent_message: discord.Message
for recent_message in await intro_channel.history(limit=30).flatten():
if recent_message.author.id == induction_member.id:
if tex_emoji:
await recent_message.add_reaction(tex_emoji)
await recent_message.add_reaction("👋")
forbidden_error: discord.Forbidden
try:
if tex_emoji:
await recent_message.add_reaction(tex_emoji)
await recent_message.add_reaction("👋")
except discord.Forbidden as forbidden_error:
if "90001" not in str(forbidden_error):
raise forbidden_error from forbidden_error

logger.info(
"Failed to add reactions because the user, %s, "
"has blocked the bot.",
recent_message.author,
)
break

await initial_response.edit(content=":white_check_mark: User inducted successfully.")
Expand Down
22 changes: 14 additions & 8 deletions cogs/send_get_roles_reminders.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,20 @@ async def send_get_roles_reminders(self) -> None:
)
continue

await member.send(
"Hey! It seems like you have been given the `@Guest` role "
f"on the {self.bot.group_short_name} Discord server "
" but have not yet nabbed yourself any opt-in roles.\n"
f"You can head to {roles_channel_mention} "
"and click on the icons to get optional roles like pronouns "
"and year group identifiers.",
)
try:
await member.send(
"Hey! It seems like you have been given the `@Guest` role "
f"on the {self.bot.group_short_name} Discord server "
" but have not yet nabbed yourself any opt-in roles.\n"
f"You can head to {roles_channel_mention} "
"and click on the icons to get optional roles like pronouns "
"and year group identifiers.",
)
except discord.Forbidden:
logger.info(
"Failed to open DM channel to user, %s, so no role reminder was sent.",
member,
)

await SentGetRolesReminderMember.objects.acreate(discord_id=member.id)

Expand Down
35 changes: 21 additions & 14 deletions cogs/send_introduction_reminders.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,27 @@ async def send_introduction_reminders(self) -> None:
)
continue

await member.send(
content=(
"Hey! It seems like you joined "
f"the {self.bot.group_short_name} Discord server "
"but have not yet introduced yourself.\n"
"You will only get access to the rest of the server after sending "
"an introduction message."
),
view=(
self.OptOutIntroductionRemindersView(self.bot)
if settings["SEND_INTRODUCTION_REMINDERS"] == "interval"
else None # type: ignore[arg-type]
),
)
try:
await member.send(
content=(
"Hey! It seems like you joined "
f"the {self.bot.group_short_name} Discord server "
"but have not yet introduced yourself.\n"
"You will only get access to the rest of the server after sending "
"an introduction message."
),
view=(
self.OptOutIntroductionRemindersView(self.bot)
if settings["SEND_INTRODUCTION_REMINDERS"] == "interval"
else None # type: ignore[arg-type]
),
)
except discord.Forbidden:
logger.info(
"Failed to open DM channel with user, %s, "
"so no induction reminder was sent.",
member,
)

await SentOneOffIntroductionReminderMember.objects.acreate(
discord_id=member.id,
Expand Down

0 comments on commit 33811b0

Please sign in to comment.