async def grant_deny_channel_to_member(channel_mapping, member, balances): print("Checking channel") rally_id = data.get_rally_id(member.id) if rally_id is None: return matched_channels = [ channel for channel in member.guild.channels if channel.name == channel_mapping[data.CHANNEL_NAME_KEY] ] if len(matched_channels) == 0: return channel_to_assign = matched_channels[0] if channel_to_assign is not None: if ( rally_api.find_balance_of_coin( channel_mapping[data.COIN_KIND_KEY], balances ) >= channel_mapping[data.REQUIRED_BALANCE_KEY] ): perms = channel_to_assign.overwrites_for(member) perms.send_messages = True perms.read_messages = True perms.read_message_history = True await channel_to_assign.set_permissions(member, overwrite=perms) print("Assigned channel to member") else: perms = channel_to_assign.overwrites_for(member) perms.send_messages = False perms.read_messages = False perms.read_message_history = False await channel_to_assign.set_permissions(member, overwrite=perms) print("Removed channel to member") else: print("Channel not found")
async def grant_deny_role_to_member(role_mapping, member, balances): """ Determine if the rally_id and balance for a role is still valid for a particular member Update status in database. Parameters __________ channel_mapping (list) - list of information for the channel mapped to the member member (discord.Member) - The discord member to check balances (list) - The amount allocated to this member per coin """ rally_id = data.get_rally_id(member.id) if rally_id is None or balances is None: return role_to_assign = get(member.guild.roles, name=role_mapping[data.ROLE_NAME_KEY]) if (rally_api.find_balance_of_coin(role_mapping[data.COIN_KIND_KEY], balances) >= role_mapping[data.REQUIRED_BALANCE_KEY]): if role_to_assign is not None: await member.add_roles(role_to_assign) print("Assigned role to member") else: print("Can't find role") print(role_mapping["role"]) else: if role_to_assign in member.roles: await member.remove_roles(role_to_assign) print("Removed role to member")
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 extended_check(ctx): rally_id = data.get_rally_id(ctx.message.author.id) if rally_id is None: raise errors.WalletNotVerified( ctx.message.author.mention + " you haven't verified your wallet yet! Use the `set_rally_id` command" ) return True
async def extended_check(ctx): rally_id = data.get_rally_id(ctx.message.author.id) if rally_id is None: raise errors.WalletNotVerified( ctx.message.author.mention + " hasn’t verified their wallet yet! Type !join" ) return True
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 grant_deny_channel_to_member(channel_mapping, member, balances): """ Determine if the rally_id and balance for a channel is still valid for a particular member Update status in database. Parameters __________ channel_mapping (list) - list of information for the channel mapped to the member member (discord.Member) - The discord member to check balances (list) - The amount of coin allocated to this member per coin """ print("Checking channel") rally_id = data.get_rally_id(member.id) if rally_id is None or balances is None: return matched_channels = [ channel for channel in member.guild.channels if channel.name == channel_mapping[data.CHANNEL_NAME_KEY] ] if len(matched_channels) == 0: return channel_to_assign = matched_channels[0] if channel_to_assign is not None: if (rally_api.find_balance_of_coin(channel_mapping[data.COIN_KIND_KEY], balances) >= channel_mapping[data.REQUIRED_BALANCE_KEY]): perms = channel_to_assign.overwrites_for(member) perms.send_messages = True perms.read_messages = True perms.read_message_history = True await channel_to_assign.set_permissions(member, overwrite=perms) print("Assigned channel to member") else: perms = channel_to_assign.overwrites_for(member) perms.send_messages = False perms.read_messages = False perms.read_message_history = False await channel_to_assign.set_permissions(member, overwrite=perms) print("Removed channel to member") else: print("Channel not found")
async def grant_deny_role_to_member(role_mapping, member, balances): rally_id = data.get_rally_id(member.id) if rally_id is None: return role_to_assign = get(member.guild.roles, name=role_mapping[data.ROLE_NAME_KEY]) print("Checking for coin " + role_mapping[data.COIN_KIND_KEY]) print(rally_api.find_balance_of_coin(role_mapping[data.COIN_KIND_KEY], balances)) if ( rally_api.find_balance_of_coin(role_mapping[data.COIN_KIND_KEY], balances) >= role_mapping[data.REQUIRED_BALANCE_KEY] ): if role_to_assign is not None: await member.add_roles(role_to_assign) print("Assigned role to member") else: print("Can't find role") print(role_mapping["role"]) else: if role_to_assign in member.roles: await member.remove_roles(role_to_assign) print("Removed role to member")
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")