add update_support_server_member_roles loop
@@ -10,9 +10,11 @@ class Database(commands.Cog):
self.db = self.bot.db # the asyncpg pool
self.update_user_health.start()
+ self.update_support_server_member_roles.start()
def cog_unload(self):
self.update_user_health.cancel()
+ self.update_support_server_member_roles.cancel()
async def populate_caches(self):
self.d.ban_cache = await self.fetch_all_botbans()
@@ -25,10 +27,29 @@ class Database(commands.Cog):
async with self.db.acquire() as con:
await con.execute('UPDATE users SET health = health + 1 WHERE health < 20')
- @update_user_health.before_loop
- async def before_update_user_health(self):
+ @tasks.loop(minutes=10)
+ async def update_support_server_member_roles(self):
await self.bot.wait_until_ready()
+ support_guild = self.bot.get_guild(self.d.support_server_id)
+ role_map_values = list(self.d.role_mappings.values())
+
+ for member in support_guild.members:
+ roles = []
+
+ for role in member.roles:
+ if role.id not in role_map_values:
+ roles.append(role)
+
+ pickaxe = await self.fetch_pickaxe(member.id)
+ roles.append(support_guild.get_role(self.d.role_mappings.get(pickaxe)))
+
+ if await self.fetch_item(member.id, 'Bane Of Pillagers Amulet') is not None:
+ roles.append(support_guild.get_role(self.d.role_mappings.get('BOP')))
+
+ if roles != member.roles:
+ await member.edit(roles=roles)
+
async def fetch_all_botbans(self):
botban_records = await self.db.fetch('SELECT uid FROM users WHERE bot_banned = true') # returns [Record<uid=>, Record<uid=>,..]
return [r[0] for r in botban_records]
