aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Johannes Christ <[email protected]>2019-01-03 20:42:39 +0100
committerGravatar Johannes Christ <[email protected]>2019-01-03 20:42:39 +0100
commitc16a97d51f4dd47f66e45b308022972419fff891 (patch)
tree7baa3636e9e993c89d0d744d7de77a98ec7d341e
parentAdd test case for deduplication of `in_guild` field update. (diff)
Move diff logging to cog, add manual sync commands.
-rw-r--r--bot/cogs/sync/cog.py41
-rw-r--r--bot/cogs/sync/syncers.py24
2 files changed, 53 insertions, 12 deletions
diff --git a/bot/cogs/sync/cog.py b/bot/cogs/sync/cog.py
index 959e26eeb..70acfaab9 100644
--- a/bot/cogs/sync/cog.py
+++ b/bot/cogs/sync/cog.py
@@ -2,6 +2,7 @@ import logging
from typing import Callable, Iterable
from discord import Guild
+from discord.ext import commands
from discord.ext.commands import Bot
from . import syncers
@@ -32,5 +33,41 @@ class Sync:
for syncer in self.ON_READY_SYNCERS:
syncer_name = syncer.__name__[5:] # drop off `sync_`
log.info("Starting `%s` syncer.", syncer_name)
- await syncer(self.bot, guild)
- log.info("`%s` syncer finished.", syncer_name)
+ total_created, total_updated = await syncer(self.bot, guild)
+ log.info(
+ "`%s` syncer finished, created `%d`, updated `%d`.",
+ syncer_name, total_created, total_updated
+ )
+
+ @commands.group(name='sync')
+ @commands.has_permissions(administrator=True)
+ async def sync_group(self, ctx):
+ """Run synchronizations between the bot and site manually."""
+
+ @sync_group.command(name='roles')
+ @commands.has_permissions(administrator=True)
+ async def sync_roles_command(self, ctx):
+ """Manually synchronize the guild's roles with the roles on the site."""
+
+ initial_response = await ctx.send("📊 Synchronizing roles.")
+ total_created, total_updated = await syncers.sync_roles(self.bot, ctx.guild)
+ await initial_response.edit(
+ content=(
+ f"👌 Role synchronization complete, created **{total_created}** "
+ f"and updated **{total_created}** roles."
+ )
+ )
+
+ @sync_group.command(name='users')
+ @commands.has_permissions(administrator=True)
+ async def sync_users_command(self, ctx):
+ """Manually synchronize the guild's users with the users on the site."""
+
+ initial_response = await ctx.send("📊 Synchronizing users.")
+ total_created, total_updated = await syncers.sync_users(self.bot, ctx.guild)
+ await initial_response.edit(
+ content=(
+ f"👌 User synchronization complete, created **{total_created}** "
+ f"and updated **{total_created}** users."
+ )
+ )
diff --git a/bot/cogs/sync/syncers.py b/bot/cogs/sync/syncers.py
index 66e041f49..3037d2e31 100644
--- a/bot/cogs/sync/syncers.py
+++ b/bot/cogs/sync/syncers.py
@@ -1,13 +1,9 @@
-import itertools
-import logging
from collections import namedtuple
from typing import Dict, Set, Tuple
from discord import Guild
from discord.ext.commands import Bot
-log = logging.getLogger(__name__)
-
# These objects are declared as namedtuples because tuples are hashable,
# something that we make use of when diffing site roles against guild roles.
Role = namedtuple('Role', ('id', 'name', 'colour', 'permissions'))
@@ -59,6 +55,11 @@ async def sync_roles(bot: Bot, guild: Guild):
guild (discord.Guild):
The guild instance from the bot's cache
to synchronize roles with.
+
+ Returns:
+ Tuple[int, int]:
+ A tuple with two integers representing how many roles were created
+ (element `0`) and how many roles were updated (element `1`).
"""
roles = await bot.api_client.get('bot/roles')
@@ -77,7 +78,6 @@ async def sync_roles(bot: Bot, guild: Guild):
roles_to_create, roles_to_update = get_roles_for_sync(guild_roles, api_roles)
for role in roles_to_create:
- log.info(f"Creating role `{role.name}` on the site.")
await bot.api_client.post(
'bot/roles',
json={
@@ -89,7 +89,6 @@ async def sync_roles(bot: Bot, guild: Guild):
)
for role in roles_to_update:
- log.info(f"Updating role `{role.name}` on the site.")
await bot.api_client.put(
'bot/roles/' + str(role.id),
json={
@@ -100,6 +99,8 @@ async def sync_roles(bot: Bot, guild: Guild):
}
)
+ return (len(roles_to_create), len(roles_to_update))
+
def get_users_for_sync(
guild_users: Dict[int, User], api_users: Dict[int, User]
@@ -167,6 +168,11 @@ async def sync_users(bot: Bot, guild: Guild):
guild (discord.Guild):
The guild instance from the bot's cache
to synchronize roles with.
+
+ Returns:
+ Tuple[int, int]:
+ A tuple with two integers representing how many users were created
+ (element `0`) and how many users were updated (element `1`).
"""
current_users = await bot.api_client.get('bot/users')
@@ -192,7 +198,6 @@ async def sync_users(bot: Bot, guild: Guild):
users_to_create, users_to_update = get_users_for_sync(guild_users, api_users)
- log.info("Creating a total of `%d` users on the site.", len(users_to_create))
for user in users_to_create:
await bot.api_client.post(
'bot/users',
@@ -205,9 +210,7 @@ async def sync_users(bot: Bot, guild: Guild):
'roles': list(user.roles)
}
)
- log.info("User creation complete.")
- log.info("Updating a total of `%d` users on the site.", len(users_to_update))
for user in users_to_update:
await bot.api_client.put(
'bot/users/' + str(user.id),
@@ -220,4 +223,5 @@ async def sync_users(bot: Bot, guild: Guild):
'roles': list(user.roles)
}
)
- log.info("User update complete.")
+
+ return (len(users_to_create), len(users_to_update))