async def update(self): with self.update_lock: print("Updating roles") guilds = self.bot.guilds guild_count = 0 member_count = 0 mapping_count = 0 for guild in guilds: guild_count += 1 await guild.chunk() role_mappings = list(data.get_role_mappings(guild.id)) channel_mappings = list(data.get_channel_mappings(guild.id)) mapping_count += len(role_mappings) + len(channel_mappings) for member in guild.members: member_count += 1 rally_id = data.get_rally_id(member.id) if rally_id: balances = rally_api.get_balances(rally_id) for role_mapping in role_mappings: print(role_mapping) await grant_deny_role_to_member( role_mapping, member, balances) for channel_mapping in channel_mappings: await grant_deny_channel_to_member( channel_mapping, member, balances) print("Done! Checked " + str(guild_count) + " guilds. " + str(mapping_count) + " mappings. " + str(member_count) + " members.")
async def balance(self, ctx): rally_id = data.get_rally_id(ctx.message.author.id) balances = rally_api.get_balances(rally_id) balance_str = "" for balance in balances: balance_str += f"{balance['coinKind']}: {round(float(balance['coinBalance']), 2)} (Est. USD$ {round(float(balance['estimatedInUsd']), 2)})\n" await pretty_print( ctx, balance_str, title=f"{ctx.message.author.name}'s Balance", color=WARNING_COLOR, )
async def set_rally_id(self, ctx): member = ctx.author with self.update_lock: for guild in self.bot.guilds: await guild.chunk() if not member in guild.members: continue role_mappings = list(data.get_role_mappings(guild.id)) channel_mappings = list(data.get_channel_mappings(guild.id)) rally_id = data.get_rally_id(member.id) if rally_id: balances = rally_api.get_balances(rally_id) for role_mapping in role_mappings: try: await grant_deny_role_to_member( role_mapping, member, balances) except discord.HTTPException: raise errors.RequestError( "network error, try again later") except: # Forbidden, NotFound or Invalid Argument exceptions only called when code # or bot is wrongly synced / setup raise errors.FatalError( "bot is setup wrong, call admin") for channel_mapping in channel_mappings: try: await grant_deny_channel_to_member( channel_mapping, member, balances) except discord.HTTPException: raise errors.RequestError( "network error, try again later") except: # Forbidden, NotFound or Invalid Argument exceptions only called when code # or bot is wrongly synced / setup raise errors.FatalError( "bot is setup wrong, call admin") await pretty_print( ctx, "Command completed successfully!", title="Success", color=SUCCESS_COLOR, )
async def one_time_role_mapping(self, ctx, coin_name, coin_amount: int, role: discord.Role): for member in ctx.guild.members: rally_id = data.get_rally_id(member.id) if rally_id: balances = rally_api.get_balances(rally_id) await update_cog.grant_deny_role_to_member( { data.GUILD_ID_KEY: ctx.guild.id, data.COIN_KIND_KEY: coin_name, data.REQUIRED_BALANCE_KEY: coin_amount, data.ROLE_NAME_KEY: role.name, }, member, balances, ) await update_cog.force_update(self.bot, ctx)
async def one_time_role_mapping(self, ctx, coin_name, coin_amount: int, role_name): if not await validation.is_valid_role(ctx, role_name): return if ctx.guild is None: ctx.send("Please send this command in a server.") return for member in ctx.guild.members: rally_id = data.get_rally_id(member.id) if rally_id is not None: balances = rally_api.get_balances(rally_id) await update_cog.grant_deny_role_to_member( { data.GUILD_ID_KEY: ctx.guild.id, data.COIN_KIND_KEY: coin_name, data.REQUIRED_BALANCE_KEY: coin_amount, data.ROLE_NAME_KEY: role_name, }, member, balances, ) update = self.bot.get_cog("UpdateTask") await update.force_update(ctx) await ctx.send("Done")