def convert_subscription_info_to_dict(self, guild: discord.Guild, subscription_info_tuple): """ Helper that converts the result from a database query into a dictionary. :param guild: :param subscription_info_tuple: a tuple containing (channel ID that message is in, subscription message ID, subscribe emoji -- either str or ID, unsubscribe emoji -- also either str or ID, role ID), :return: """ if subscription_info_tuple is None: return None result = dict( zip( [ "channel", "subscription_message_id", "subscribe_emoji", "unsubscribe_emoji", "role", ], subscription_info_tuple ) ) # Convert the subscribe/unsubscribe emoji to the appropriate type if it's a custom emoji. with self.conn: guild_info_cursor = self.conn.execute( """ select subscribe_emoji_type, unsubscribe_emoji_type from role_reaction_subscription where guild_id = ? and subscription_message_id = ?; """, (guild.id, result["subscription_message_id"]) ) subscribe_emoji_type, unsubscribe_emoji_type = guild_info_cursor.fetchone() if subscribe_emoji_type == "custom": result["subscribe_emoji"] = emoji_converter(guild, result["subscribe_emoji"]) if unsubscribe_emoji_type == "custom": result["unsubscribe_emoji"] = emoji_converter(guild, result["unsubscribe_emoji"]) # Other conversions. result["role"] = role_converter(guild, result["role"]) result["channel"] = guild.get_channel(result["channel"]) return result
def get_fyi_info(self, guild: discord.Guild): """ Return this guild's raid FYI configuration. :param guild: :return: """ result = {} with self.conn: guild_info_cursor = self.conn.execute( """ select fyi_emoji, fyi_emoji_type from raid_fyi where guild_id = ?; """, (guild.id, )) fyi_info_tuple = guild_info_cursor.fetchone() if fyi_info_tuple is None: return None fyi_emoji, fyi_emoji_type = fyi_info_tuple result["fyi_emoji"] = fyi_emoji if fyi_emoji_type == "custom": result["fyi_emoji"] = emoji_converter(guild, fyi_emoji) channel_mapping_cursor = self.conn.execute( """ select chat_channel_id, fyi_channel_id from raid_fyi_channel_mapping where guild_id = ?; """, (guild.id, )) channel_mappings = {} for chat_channel_id, fyi_channel_id in channel_mapping_cursor: channel_mappings[guild.get_channel( chat_channel_id)] = guild.get_channel(fyi_channel_id) result["channel_mappings"] = channel_mappings return result
def get_verification_info(self, guild): """ Return the dictionary of information corresponding to the specified guild. :param guild: :return: """ response = self.table.get_item(Key={"guild_id": guild.id}) result = response.get("Item") if result is None: return final_results = {} for field_name, field_type in self.ALL_FIELDS: raw_field = result[field_name] converted_result = raw_field if raw_field is not None: if field_type == "channel": converted_result = guild.get_channel(raw_field) elif field_type == "role": converted_result = role_converter(guild, raw_field) elif field_type == "emoji_or_string": # This is a team emoji; extract the team from the field name. team = field_name.split("_")[0] if result[f"{team}_emoji_type"] == "custom": converted_result = emoji_converter(guild, raw_field) final_results[field_name] = converted_result final_results["standard_roles"] = [ role_converter(guild, role_id) for role_id in result["standard_roles"] ] final_results["mandatory_roles"] = [ role_converter(guild, role_id) for role_id in result["mandatory_roles"] ] return final_results
def get_verification_info(self, guild): """ Return the dictionary of information corresponding to the specified guild. :param guild: :return: """ with self.conn: query = f"""\ select {", ".join([x[0] for x in self.all_fields])} from verification_info where guild_id = ?; """, (guild.id,) verification_info_cursor = self.conn.execute( f"""\ select {", ".join([x[0] for x in self.all_fields])} from verification_info where guild_id = ?; """, (guild.id,) ) verification_info_tuple = verification_info_cursor.fetchone() if verification_info_tuple is None: return None with self.conn: verification_info_cursor = self.conn.execute( f"""\ select instinct_emoji_type, mystic_emoji_type, valor_emoji_type from verification_info where guild_id = ?; """, (guild.id,) ) emoji_tuple = verification_info_cursor.fetchone() team_emoji_types = dict(zip(("instinct", "mystic", "valor"), emoji_tuple)) standard_roles = [] mandatory_roles = [] with self.conn: guild_roles_cursor = self.conn.execute( """ select role_id, mandatory from guild_standard_roles where guild_id = ?; """, (guild.id,) ) for role_id, mandatory in guild_roles_cursor: role = role_converter(guild, role_id) if mandatory: mandatory_roles.append(role) else: standard_roles.append(role) converted_results = [] for i, (field_name, field_type) in enumerate(self.all_fields): converted_result = verification_info_tuple[i] if converted_result is not None: if field_type == "channel": converted_result = guild.get_channel(verification_info_tuple[i]) elif field_type == "role": converted_result = role_converter(guild, verification_info_tuple[i]) elif field_type == "emoji_or_string": # This is a team emoji; extract the team from the field name. team = field_name.split("_")[0] if team_emoji_types[team] == "custom": converted_result = emoji_converter(guild, verification_info_tuple[i]) converted_results.append(converted_result) final_results = dict( zip( [field_name for field_name, _ in self.all_fields], converted_results ) ) final_results["standard_roles"] = standard_roles final_results["mandatory_roles"] = mandatory_roles return final_results
def get_fyi_info(self, guild: discord.Guild): """ Return this guild's raid FYI configuration. :param guild: :return: """ # Get the base guild configuration. response = self.table.get_item(Key={ "guild_id": guild.id, "config_channel_message": "config" }) result = response.get("Item") if result is None: return result["timezone"] = pytz.timezone(result["timezone"]) if result["fyi_emoji_type"] == "custom": result["fyi_emoji"] = emoji_converter(guild, result["fyi_emoji"]) del result["fyi_emoji_type"] if result["enhanced"]: if result["rsvp_emoji_type"] == "custom": result["rsvp_emoji"] = emoji_converter(guild, result["rsvp_emoji"]) del result["rsvp_emoji_type"] if result["remote_emoji_type"] == "custom": result["remote_emoji"] = emoji_converter( guild, result["remote_emoji"]) del result["remote_emoji_type"] if result["cancelled_emoji_type"] == "custom": result["cancelled_emoji"] = emoji_converter( guild, result["cancelled_emoji"]) del result["cancelled_emoji_type"] # Get all channel mappings. response = self.table.query(KeyConditionExpression=( Key("guild_id").eq(guild.id) & Key("config_channel_message").begins_with("chatchannel"))) raw_channel_mappings = response["Items"] # this is a list channel_mappings = {} for chat_channel_config in raw_channel_mappings: chat_channel_id = int( re.match( chat_channel_pattern, chat_channel_config["config_channel_message"]).group(1)) chat_channel = guild.get_channel(chat_channel_id) if chat_channel is None: chat_channel = MissingChannel(chat_channel_id) relay_channel = guild.get_channel( chat_channel_config["relay_channel"]) if relay_channel is None: relay_channel = MissingChannel( chat_channel_config["relay_channel"]) channel_mappings[chat_channel] = { "relay_channel": relay_channel, "timeout_in_hours": chat_channel_config["timeout_in_hours"] } result["channel_mappings"] = channel_mappings # Get all category mappings. response = self.table.query(KeyConditionExpression=( Key("guild_id").eq(guild.id) & Key("config_channel_message").begins_with("category"))) raw_category_mappings = response["Items"] # this is a list category_mappings = {} for category_config in raw_category_mappings: category_id = int( re.match(category_pattern, category_config["config_channel_message"]).group(1)) category = guild.get_channel(category_id) if category is None: category = MissingCategory(category_id) relay_channel = guild.get_channel(category_config["relay_channel"]) if relay_channel is None: relay_channel = MissingChannel( category_config["relay_channel"]) category_mappings[category] = { "relay_channel": relay_channel, "timeout_in_hours": category_config["timeout_in_hours"] } result["category_mappings"] = category_mappings return result
def get_ex_gate_info(self, guild: discord.Guild): """ Return some raw guild information required for the EX gating. Because we don't have a context object, we can't convert channel IDs to channels or role IDs to roles. :param guild: :return: """ with self.conn: guild_info_cursor = self.conn.execute( """ select disclaimer_channel_id, disclaimer_message_id, approve_emoji, ex_role_id, wait_time, approval_message_template from ex_gate where guild_id = ?; """, (guild.id,) ) guild_info_tuple = guild_info_cursor.fetchone() if guild_info_tuple is None: return None result = dict( zip( [ "disclaimer_channel", "disclaimer_message_id", "approve_emoji", "ex_role", "wait_time", "approval_message_template" ], guild_info_tuple ) ) # Convert the approve emoji to the appropriate type if it's a custom emoji. with self.conn: guild_info_cursor = self.conn.execute( """ select approve_emoji_type from ex_gate where guild_id = ?; """, (guild.id,) ) approve_emoji_type = guild_info_cursor.fetchone()[0] if approve_emoji_type == "custom": result["approve_emoji"] = emoji_converter(guild, result["approve_emoji"]) # Other conversions. result["disclaimer_channel"] = guild.get_channel(result["disclaimer_channel"]) result["ex_role"] = role_converter(guild, result["ex_role"]) # Get the strings that are accepted by the guild for granting the EX channels. result["accepted_messages"] = [] with self.conn: guild_info_cursor = self.conn.execute( """ select accepted_message from ex_gate_accepted_message where guild_id = ?; """, (guild.id,) ) result["accepted_messages"] = [accepted_message[0] for accepted_message in guild_info_cursor] return result
def get_no_command_subscription_settings(self, guild: discord.Guild): """ Return a dictionary with all no-command subscription settings for this guild. The dictionary will contain keys: - subscription_channel - instruction_message - wait_time - show_subscriptions_emoji - roles: a dictionary keyed by role IDs, with values being lists of associated channels (or []) :return: """ with self.conn: sub_cursor = self.conn.execute( """ select subscription_channel_id, instruction_message_id, instruction_message_text, wait_time, show_subscriptions_emoji from no_command_subscription where guild_id = ?; """, (guild.id,) ) sub_tuple = sub_cursor.fetchone() if sub_tuple is None: return None result = dict( zip( [ "subscription_channel", "instruction_message_id", "instruction_message_text", "wait_time", "show_subscriptions_emoji" ], sub_tuple ) ) result["subscription_channel"] = guild.get_channel(result["subscription_channel"]) # Convert the show-subscriptions emoji to the appropriate type if it's a custom emoji. with self.conn: guild_info_cursor = self.conn.execute( """ select show_subscriptions_emoji_type from no_command_subscription where guild_id = ?; """, (guild.id,) ) show_subscriptions_emoji_type = guild_info_cursor.fetchone()[0] if show_subscriptions_emoji_type == "custom": result["show_subscriptions_emoji"] = emoji_converter(guild, result["show_subscriptions_emoji"]) # Now retrieve the roles that are registered for no-command subscription. result["roles"] = {} with self.conn: sub_cursor = self.conn.execute( "select role_id from no_command_role where guild_id = ?;", (guild.id,) ) for role_tuple in sub_cursor: role = role_converter(guild, role_tuple[0]) result["roles"][role] = [] sub_cursor = self.conn.execute( "select role_id, channel_id from no_command_role_channel where guild_id = ?;", (guild.id,) ) for role_channel_tuple in sub_cursor: role = role_converter(guild, role_channel_tuple[0]) if role in result["roles"]: channel = guild.get_channel(role_channel_tuple[1]) result["roles"][role].append(channel) return result