export type TargetRole = { guild_id: string; role_id: string }; type SerializedRule = { group_id: string; target_role: TargetRole }; /** * A set of rules for associating CRI groups with Discord roles. */ export class RuleSet { private rules; public constructor() { this.rules = new Map(); } /** * Reads a RuleSet from a JSON serialized file. */ public static async from_file(path: string) { const result = new RuleSet(); const file_content = await Deno.readTextFile(path); const parsed = JSON.parse(file_content) as SerializedRule[]; for (const { group_id, target_role } of parsed) result.append_rule(group_id, target_role); return result; } /** * Gets which Discord roles must be assigned to a user which is part of a given CRI group. */ public roles_for_group(group_id: string) { return this.rules.get(group_id) ?? []; } /** * Number of managed Discord roles. */ public size() { let result = 0; for (const roles of this.rules.values()) result += roles.length; return result; } private append_rule(group_id: string, target_role: TargetRole) { let roles = this.rules.get(group_id); if (roles === undefined) { roles = []; this.rules.set(group_id, roles); } roles.push(target_role); } }