diff options
| -rw-r--r-- | bot/cogs/sync/cog.py | 14 | ||||
| -rw-r--r-- | tests/bot/cogs/sync/test_cog.py | 17 | 
2 files changed, 18 insertions, 13 deletions
diff --git a/bot/cogs/sync/cog.py b/bot/cogs/sync/cog.py index 97ea31ba5..578cccfc9 100644 --- a/bot/cogs/sync/cog.py +++ b/bot/cogs/sync/cog.py @@ -34,14 +34,15 @@ class Sync(Cog):          for syncer in (self.role_syncer, self.user_syncer):              await syncer.sync(guild) -    async def patch_user(self, user_id: int, updated_information: Dict[str, Any]) -> None: +    async def patch_user(self, user_id: int, json: Dict[str, Any], ignore_404: bool = False) -> None:          """Send a PATCH request to partially update a user in the database."""          try: -            await self.bot.api_client.patch(f"bot/users/{user_id}", json=updated_information) +            await self.bot.api_client.patch(f"bot/users/{user_id}", json=json)          except ResponseCodeError as e:              if e.response.status != 404:                  raise -            log.warning("Unable to update user, got 404. Assuming race condition from join event.") +            if not ignore_404: +                log.warning("Unable to update user, got 404. Assuming race condition from join event.")      @Cog.listener()      async def on_guild_role_create(self, role: Role) -> None: @@ -137,7 +138,7 @@ class Sync(Cog):          if member.guild != constants.Guild.id:              return -        await self.patch_user(member.id, updated_information={"in_guild": False}) +        await self.patch_user(member.id, json={"in_guild": False})      @Cog.listener()      async def on_member_update(self, before: Member, after: Member) -> None: @@ -147,7 +148,7 @@ class Sync(Cog):          if before.roles != after.roles:              updated_information = {"roles": sorted(role.id for role in after.roles)} -            await self.patch_user(after.id, updated_information=updated_information) +            await self.patch_user(after.id, json=updated_information)      @Cog.listener()      async def on_user_update(self, before: User, after: User) -> None: @@ -158,7 +159,8 @@ class Sync(Cog):                  "name": after.name,                  "discriminator": int(after.discriminator),              } -            await self.patch_user(after.id, updated_information=updated_information) +            # A 404 likely means the user is in another guild. +            await self.patch_user(after.id, json=updated_information, ignore_404=True)      @commands.group(name='sync')      @commands.has_permissions(administrator=True) diff --git a/tests/bot/cogs/sync/test_cog.py b/tests/bot/cogs/sync/test_cog.py index d7d60e961..e5be14391 100644 --- a/tests/bot/cogs/sync/test_cog.py +++ b/tests/bot/cogs/sync/test_cog.py @@ -226,7 +226,7 @@ class SyncCogListenerTests(SyncCogTestCase):          self.cog.patch_user.assert_called_once_with(              member.id, -            updated_information={"in_guild": False} +            json={"in_guild": False}          )      async def test_sync_cog_on_member_remove_ignores_guilds(self): @@ -247,7 +247,7 @@ class SyncCogListenerTests(SyncCogTestCase):          await self.cog.on_member_update(before_member, after_member)          data = {"roles": sorted(role.id for role in after_member.roles)} -        self.cog.patch_user.assert_called_once_with(after_member.id, updated_information=data) +        self.cog.patch_user.assert_called_once_with(after_member.id, json=data)      async def test_sync_cog_on_member_update_other(self):          """Members should not be patched if other attributes have changed.""" @@ -308,12 +308,15 @@ class SyncCogListenerTests(SyncCogTestCase):                      # Don't care if *all* keys are present; only the changed one is required                      call_args = self.cog.patch_user.call_args -                    self.assertEqual(call_args[0][0], after_user.id) -                    self.assertIn("updated_information", call_args[1]) +                    self.assertEqual(call_args.args[0], after_user.id) +                    self.assertIn("json", call_args.kwargs) -                    updated_information = call_args[1]["updated_information"] -                    self.assertIn(api_field, updated_information) -                    self.assertEqual(updated_information[api_field], api_value) +                    self.assertIn("ignore_404", call_args.kwargs) +                    self.assertTrue(call_args.kwargs["ignore_404"]) + +                    json = call_args.kwargs["json"] +                    self.assertIn(api_field, json) +                    self.assertEqual(json[api_field], api_value)                  else:                      self.cog.patch_user.assert_not_called()  |