diff options
author | 2020-02-23 15:35:59 -0800 | |
---|---|---|
committer | 2020-02-23 15:35:59 -0800 | |
commit | f1c987978d7c66a7886c19a40a00fa9d2b8c7d0c (patch) | |
tree | f46421f042fb98a9a91b15d84639301a80671cb0 | |
parent | Bot: send empty cache warning to a webhook (diff) |
Sync: code style refactoring
* Convert diff namedtuple to dict outside the dict comprehension
* Define long condition as a boolean instead of in the if statement
* Pass role and user dicts to aiohttp normally instead of unpacking
-rw-r--r-- | bot/cogs/sync/cog.py | 6 | ||||
-rw-r--r-- | bot/cogs/sync/syncers.py | 11 |
2 files changed, 10 insertions, 7 deletions
diff --git a/bot/cogs/sync/cog.py b/bot/cogs/sync/cog.py index ee3cccbfa..5708be3f4 100644 --- a/bot/cogs/sync/cog.py +++ b/bot/cogs/sync/cog.py @@ -65,12 +65,14 @@ class Sync(Cog): @Cog.listener() async def on_guild_role_update(self, before: Role, after: Role) -> None: """Syncs role with the database if any of the stored attributes were updated.""" - if ( + was_updated = ( before.name != after.name or before.colour != after.colour or before.permissions != after.permissions or before.position != after.position - ): + ) + + if was_updated: await self.bot.api_client.put( f'bot/roles/{after.id}', json={ diff --git a/bot/cogs/sync/syncers.py b/bot/cogs/sync/syncers.py index 43a8f2b62..6715ad6fb 100644 --- a/bot/cogs/sync/syncers.py +++ b/bot/cogs/sync/syncers.py @@ -192,7 +192,8 @@ class Syncer(abc.ABC): author = ctx.author diff = await self._get_diff(guild) - totals = {k: len(v) for k, v in diff._asdict().items() if v is not None} + diff_dict = diff._asdict() # Ugly method for transforming the NamedTuple into a dict + totals = {k: len(v) for k, v in diff_dict.items() if v is not None} diff_size = sum(totals.values()) confirmed, message = await self._get_confirmation_result(diff_size, author, message) @@ -261,11 +262,11 @@ class RoleSyncer(Syncer): """Synchronise the database with the role cache of `guild`.""" log.trace("Syncing created roles...") for role in diff.created: - await self.bot.api_client.post('bot/roles', json={**role._asdict()}) + await self.bot.api_client.post('bot/roles', json=role._asdict()) log.trace("Syncing updated roles...") for role in diff.updated: - await self.bot.api_client.put(f'bot/roles/{role.id}', json={**role._asdict()}) + await self.bot.api_client.put(f'bot/roles/{role.id}', json=role._asdict()) log.trace("Syncing deleted roles...") for role in diff.deleted: @@ -334,8 +335,8 @@ class UserSyncer(Syncer): """Synchronise the database with the user cache of `guild`.""" log.trace("Syncing created users...") for user in diff.created: - await self.bot.api_client.post('bot/users', json={**user._asdict()}) + await self.bot.api_client.post('bot/users', json=user._asdict()) log.trace("Syncing updated users...") for user in diff.updated: - await self.bot.api_client.put(f'bot/users/{user.id}', json={**user._asdict()}) + await self.bot.api_client.put(f'bot/users/{user.id}', json=user._asdict()) |