add discossiate command & fixes

This commit is contained in:
Matthieu Jolimaitre 2024-08-13 01:48:49 +02:00
parent d6b3b6830a
commit 80fd2ac842
3 changed files with 77 additions and 16 deletions

View file

@ -13,6 +13,7 @@ import {
SlashCommandRoleOption,
SlashCommandStringOption,
SlashCommandSubcommandBuilder,
SlashCommandUserOption,
} from "npm:discord.js@14.14.1";
import { RuleSet } from "./rules.ts";
import { channel, log_from, SimpleResult, split_promise, try_ } from "./utils.ts";
@ -35,9 +36,7 @@ export class EpitlsBot {
this.bot.on("interactionCreate", (interaction) => this.handle_interaction(interaction));
this.rest = new REST().setToken(bot_token);
this.rules = rules;
this.assoc_channel = channel<
{ cri_login: string; discord_user_id: string; callback: (success: SimpleResult) => void }
>();
this.assoc_channel = channel<Association | Dissociation>();
}
/**
@ -116,7 +115,7 @@ export class EpitlsBot {
private async register_commands() {
const assoc_cmd = new SlashCommandBuilder()
.setName("associate")
.setDescription("Associates a cri account to your discord account.")
.setDescription("Associes un utilisateur cri avec votre compte discord.")
.addStringOption(
new SlashCommandStringOption()
.setName("cri_email")
@ -124,10 +123,21 @@ export class EpitlsBot {
.setRequired(true),
).toJSON();
const ADMIN_PERMISSIONS = 0x0000000000000008n;
const dissoc_cmd = new SlashCommandBuilder()
.setName("dissociate")
.setDescription("Dissocies un utilisateur cri de votre compte discord.")
.setDefaultMemberPermissions(ADMIN_PERMISSIONS)
.addUserOption(
new SlashCommandUserOption()
.setName("user")
.setRequired(true),
).toJSON();
const rule_cmd = new SlashCommandBuilder()
.setName("rule")
.setDescription("Gestion des règles d'associations groupes cri / rôles.")
.setDefaultMemberPermissions(0x0000000000000008n)
.setDefaultMemberPermissions(ADMIN_PERMISSIONS)
.addSubcommand(
new SlashCommandSubcommandBuilder()
.setName("list")
@ -172,7 +182,7 @@ export class EpitlsBot {
for (const guild_id of this.bot.guilds.cache.keys()) {
await this.rest.put(
Routes.applicationGuildCommands(this.bot.application!.id, guild_id),
{ body: [assoc_cmd, rule_cmd] },
{ body: [assoc_cmd, dissoc_cmd, rule_cmd] },
);
}
}
@ -180,6 +190,7 @@ export class EpitlsBot {
private async handle_interaction(interaction: Interaction<CacheType>) {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === "associate") await this.handle_command_associate(interaction);
if (interaction.commandName === "dissociate") await this.handle_command_dissociate(interaction);
if (interaction.commandName === "rule") {
const subcommand = interaction.options.getSubcommand();
if (subcommand === "list") await this.handle_command_rule_list(interaction);
@ -198,7 +209,7 @@ export class EpitlsBot {
const discord_user_id = interaction.user.id;
const { promise, resolver } = split_promise<SimpleResult>();
this.assoc_channel.send({ cri_login, discord_user_id, callback: resolver });
this.assoc_channel.send({ kind: "associate", cri_login, discord_user_id, callback: resolver });
await interaction.reply(message_command_response_assoc_pending(email));
log(`Started verification for discord id '${discord_user_id}' with cri login '${cri_login}'.`);
const result = await promise;
@ -206,6 +217,16 @@ export class EpitlsBot {
else interaction.editReply(message_command_response_assoc_error(result));
}
private async handle_command_dissociate(interaction: ChatInputCommandInteraction<CacheType>) {
const user = interaction.options.getUser("user");
if (user === null) throw new Error("Unreachable.");
const discord_user_id = user.id;
const { promise, resolver } = split_promise<SimpleResult>();
this.assoc_channel.send({ kind: "dissociate", discord_user_id, callback: resolver });
await promise;
await interaction.reply(message_command_response_dissoc_success());
}
private async handle_command_rule_list(interaction: ChatInputCommandInteraction<CacheType>) {
const guild_id = interaction.guildId;
if (guild_id === null) {
@ -261,6 +282,19 @@ export class EpitlsBot {
}
}
export type Association = {
kind: "associate";
cri_login: string;
discord_user_id: string;
callback: (success: SimpleResult) => void;
};
export type Dissociation = {
kind: "dissociate";
discord_user_id: string;
callback: (success: SimpleResult) => void;
};
function message_command_response_assoc_pending(email: string) {
const embed = new EmbedBuilder()
.setTitle("`🟡`| Association")
@ -328,6 +362,13 @@ function to_comparable(input: string) {
.replace(/\p{Diacritic}/gu, ""); // remotes accents
}
function message_command_response_dissoc_success() {
const embed = new EmbedBuilder()
.setTitle("`✅`| Dissociation")
.setDescription("Votre compte Discord a bien été dissocié de votre compte CRI.");
return { embeds: [embed] };
}
function display_rule(rule: GuildRule) {
if (rule.include_historical) return `- \`${rule.group_id}\` [H] → \`@${rule.role_name}\``;
else return `- \`${rule.group_id}\`\`@${rule.role_name}\``;