This commit is contained in:
Matthieu Jolimaitre 2024-02-01 22:54:21 +01:00
commit c872cad278
11 changed files with 1052 additions and 0 deletions

29
src/rules.ts Normal file
View file

@ -0,0 +1,29 @@
type SerializedRule = { group_id: string; target_role: TargetRole };
export type TargetRole = { guild_id: string; role_id: string };
export class RuleSet {
rules;
constructor() {
this.rules = new Map<string, TargetRole[]>();
}
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;
}
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);
}
roles_for_group(group_id: string) {
return this.rules.get(group_id) ?? [];
}
}