Init.
This commit is contained in:
commit
c872cad278
11 changed files with 1052 additions and 0 deletions
78
src/main.ts
Executable file
78
src/main.ts
Executable file
|
@ -0,0 +1,78 @@
|
|||
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-net --allow-env --unstable-kv
|
||||
|
||||
import { log_from, read_secrets, root_path, SimpleResult, wait } from "./utils.ts";
|
||||
import { State } from "./state.ts";
|
||||
import { RuleSet } from "./rules.ts";
|
||||
import { EpitlsBot } from "./bot.ts";
|
||||
import { CriApi } from "./cri.ts";
|
||||
const log = (...args: unknown[]) => log_from(import.meta.url, ...args);
|
||||
|
||||
async function main() {
|
||||
const secrets = await read_secrets(root_path() + "/secrets.json");
|
||||
const rules = await RuleSet.from_file(root_path() + "/rules.json");
|
||||
log("Loaded rules for", rules.rules.size, "groups.");
|
||||
const state = await State.from_dir(root_path() + "/local");
|
||||
log("Loaded state with", await state.users_count(), "users.");
|
||||
const bot = new EpitlsBot(secrets.discord_bot_token);
|
||||
await bot.start();
|
||||
log(`Started bot '${bot.bot.user?.displayName}' .`);
|
||||
const cri_api = new CriApi(secrets.cri_token);
|
||||
|
||||
const service = new Service(state, bot, cri_api, rules);
|
||||
await service.serve();
|
||||
}
|
||||
|
||||
class Service {
|
||||
state;
|
||||
bot;
|
||||
cri_api;
|
||||
rules;
|
||||
|
||||
constructor(state: State, bot: EpitlsBot, cri_api: CriApi, rules: RuleSet) {
|
||||
this.state = state;
|
||||
this.bot = bot;
|
||||
this.cri_api = cri_api;
|
||||
this.rules = rules;
|
||||
}
|
||||
|
||||
async serve() {
|
||||
await this.update_all_users_roles();
|
||||
|
||||
(async () => {
|
||||
for await (const { discord_user_id, cri_login, callback } of this.bot.receive_associations()) {
|
||||
this.association_procedure(discord_user_id, cri_login).then(callback);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
async update_all_users_roles() {
|
||||
const promises = [] as Promise<void>[];
|
||||
for await (const { cri_login, discord_user_id } of this.state.users()) {
|
||||
promises.push(this.update_user_roles(cri_login, discord_user_id));
|
||||
}
|
||||
await Promise.all(promises);
|
||||
}
|
||||
|
||||
async update_user_roles(cri_login: string, discord_user_id: string) {
|
||||
const groups = await this.cri_api.groups_of(cri_login);
|
||||
const roles = groups.map((group) => this.rules.roles_for_group(group)).flat();
|
||||
for (const { guild_id, role_id } of roles) await this.bot.assign_role(discord_user_id, guild_id, role_id);
|
||||
}
|
||||
|
||||
async association_procedure(discord_user_id: string, cri_login: string): Promise<SimpleResult> {
|
||||
try {
|
||||
await wait(1000);
|
||||
if (!await this.cri_api.user_exists(cri_login)) return "No such login.";
|
||||
this.state.set_user(discord_user_id, cri_login);
|
||||
await this.update_user_roles(cri_login, discord_user_id);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return `${error}`;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// bot.assign_role_to_member("358338548174159873", "871777993922588712", "1202346358867238952");
|
||||
|
||||
main();
|
Loading…
Add table
Add a link
Reference in a new issue