refactor
This commit is contained in:
parent
fe2e22ba17
commit
e90fa55862
2 changed files with 50 additions and 15 deletions
16
src/bot.ts
16
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] };
|
||||
}
|
||||
|
|
49
src/cri.ts
49
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<string>();
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue