diff --git a/src/bot.ts b/src/bot.ts index fd47139..993b2c1 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -121,21 +121,25 @@ export class EpitlsBot { function message_command_response_sending_email(email: string) { const embed = new EmbedBuilder() - .setTitle("`🟡`| Verification") - .setDescription(`Sending a verification link to your email \`${email}\`.`); + .setTitle("`🟡`| Vérification") + .setDescription(` +Un lien de vérification a été envoyé par e-mail à \`${email}\`. +Le lien expirera dans 5 minutes. +**Il pourraît être arrivé dans \`Courrier indésirable\`.** +`.trim()); return { embeds: [embed] }; } function message_command_response_error(msg: string) { const embed = new EmbedBuilder() - .setTitle("`❌`| Verification") - .setDescription(`An error occured\n\`${msg}\``); + .setTitle("`❌`| Vérification") + .setDescription(`Échec de la vérification :\n\`${msg}\``); return { embeds: [embed] }; } function message_command_response_success() { const embed = new EmbedBuilder() - .setTitle("`✅`| Verification") - .setDescription("Your email was associated with this account."); + .setTitle("`✅`| Vérification") + .setDescription("Votre e-mail CRI a bien été associé à ce compte Discord."); return { embeds: [embed] }; } diff --git a/src/cri.ts b/src/cri.ts index a980044..5c0d8c9 100644 --- a/src/cri.ts +++ b/src/cri.ts @@ -1,9 +1,14 @@ import { z } from "https://deno.land/x/zod@v3.22.4/mod.ts"; +import { log_from } from "./utils.ts"; +const log = (...args: unknown[]) => log_from(import.meta.url, ...args); /** * Wraps the CRI API. */ export class CriApi { + /** + * note : a CRI token is actually + */ private token; public constructor(token: string) { @@ -14,13 +19,8 @@ export class CriApi { * Fetches an array of the groups an user has been part of. */ public async groups_of(login: string) { - const response = await fetch(`https://cri.epita.fr/api/v2/users/${login}/`, { - headers: { - accept: "application/json", - authorization: "Basic " + this.token, - }, - }); - const body = await response.json(); + log(`Fetching groups of '${login}'.`); + const response = await fetch_json_auth(`https://cri.epita.fr/api/v2/users/${login}/`, this.token); const group = z.object({ slug: z.string() }); const parser = z.object({ primary_group: group, @@ -30,7 +30,7 @@ export class CriApi { })), current_groups: z.array(group), }); - const parsed = parser.parse(body); + const parsed = parser.parse(response); const result = new Set(); result.add(parsed.primary_group.slug); for (const group of parsed.current_groups) result.add(group.slug); @@ -42,12 +42,43 @@ export class CriApi { * Tests wether a given login exists within the CRI registry. */ public async user_exists(login: string) { + log(`Testing existence of user '${login}'.`); const response = await fetch(`https://cri.epita.fr/api/v2/users/${login}/`, { headers: { accept: "application/json", authorization: "Basic " + this.token, }, }); - return response.status === 200; + const found = response.status === 200; + return found; + } + + public async get_all_groups() { + const result = [] as string[]; + const response_parser = z.object({ + next: z.string().or(z.null()), + results: z.array(z.object({ + slug: z.string(), + })), + }); + let next_url = `https://cri.epita.fr/api/v2/groups/`; + while (true) { + const response = await fetch_json_auth(next_url, this.token); + const parsed = response_parser.parse(response); + result.push(...parsed.results.map((g) => g.slug)); + if (parsed.next === null) break; + next_url = parsed.next; + } + return result; } } + +async function fetch_json_auth(url: string, token: string) { + const response = await fetch(url, { + headers: { + accept: "application/json", + authorization: "Basic " + token, + }, + }); + return await response.json() as unknown; +}