Repository: python-discord/bot

Issue:
Use on_member_update for verification DMs
We want to DM users some information once they pass the native Discord verification gate. To do this we need to watch for the `pending` attribute of the user change from `True` to `False`.

As of now we have implemented this by keeping track of a list of users who need to receive a welcome DM in a set called `pending_members` in the `Verification` cog but we should switch this to use `on_member_update(before, after)` as soon as discord.py makes this available.

Once a user passes the native gate we want to send them the `ALTERNATE_VERIFIED_MESSAGE` defined in this cog.

As of now Discord.py do not have an upstream issue for this, but we should keep track of it to watch for when one is opened.


Discussion:

Resolving patch:
diff --git a/bot/exts/moderation/verification.py b/bot/exts/moderation/verification.py
--- a/bot/exts/moderation/verification.py
+++ b/bot/exts/moderation/verification.py
@@ -174,9 +174,6 @@ class Verification(Cog):
     # ]
     task_cache = RedisCache()
 
-    # Create a cache for storing recipients of the alternate welcome DM.
-    member_gating_cache = RedisCache()
-
     def __init__(self, bot: Bot) -> None:
         """Start internal tasks."""
         self.bot = bot
@@ -570,9 +567,9 @@ async def on_member_join(self, member: discord.Member) -> None:
         # If the user has the pending flag set, they will be using the alternate
         # gate and will not need a welcome DM with verification instructions.
         # We will send them an alternate DM once they verify with the welcome
-        # video.
+        # video when they pass the gate.
         if raw_member.get("pending"):
-            return self.pending_members.add(member.id)
+            return
 
         log.trace(f"Sending on join message to new member: {member.id}")
         try:
@@ -580,34 +577,18 @@ async def on_member_join(self, member: discord.Member) -> None:
         except discord.HTTPException:
             log.exception("DM dispatch failed on unexpected error code")
 
-    @Cog.listener()
-    async def on_socket_response(self, msg: dict) -> None:
-        """Check if the users pending status has changed and send them them a welcome message."""
-        if msg.get("t") == "GUILD_MEMBER_UPDATE":
-            user_id = int(msg["d"]["user"]["id"])
-
-            if msg["d"].get("pending") is False:
-                if user_id in self.pending_members:
-                    self.pending_members.remove(user_id)
-                    if member := self.bot.get_guild(constants.Guild.id).get_member(user_id):
-                        await safe_dm(member.send(ALTERNATE_VERIFIED_MESSAGE))
-
     @Cog.listener()
     async def on_member_update(self, before: discord.Member, after: discord.Member) -> None:
         """Check if we need to send a verification DM to a gated user."""
-        before_roles = [role.id for role in before.roles]
-        after_roles = [role.id for role in after.roles]
-
-        if constants.Roles.verified not in before_roles and constants.Roles.verified in after_roles:
-            if await self.member_gating_cache.pop(after.id):
-                try:
-                    # If the member has not received a DM from our !accept command
-                    # and has gone through the alternate gating system we should send
-                    # our alternate welcome DM which includes info such as our welcome
-                    # video.
-                    await safe_dm(after.send(ALTERNATE_VERIFIED_MESSAGE))
-                except discord.HTTPException:
-                    log.exception("DM dispatch failed on unexpected error code")
+        if before.pending is True and after.pending is False:
+            try:
+                # If the member has not received a DM from our !accept command
+                # and has gone through the alternate gating system we should send
+                # our alternate welcome DM which includes info such as our welcome
+                # video.
+                await safe_dm(after.send(ALTERNATE_VERIFIED_MESSAGE))
+            except discord.HTTPException:
+                log.exception("DM dispatch failed on unexpected error code")
 
     @Cog.listener()
     async def on_message(self, message: discord.Message) -> None:


Test patch: