add API constraints

This commit is contained in:
Matthieu Jolimaitre 2024-02-02 12:57:42 +01:00
parent 431c184690
commit fe34c8a91c
5 changed files with 48 additions and 34 deletions

View file

@ -13,12 +13,12 @@ import { channel, log_from, SimpleResult, split_promise } from "./utils.ts";
const log = (...args: unknown[]) => log_from(import.meta.url, ...args);
export class EpitlsBot {
bot;
token;
rest;
assoc_channel;
private bot;
private token;
private rest;
private assoc_channel;
constructor(bot_token: string) {
public constructor(bot_token: string) {
this.token = bot_token;
const intents = [GatewayIntentBits.Guilds];
this.bot = new Client({ intents });
@ -29,7 +29,7 @@ export class EpitlsBot {
>();
}
async start() {
public async start() {
const { promise, resolver } = split_promise<void>();
this.bot.on("ready", () => resolver());
this.bot.login(this.token);
@ -37,6 +37,23 @@ export class EpitlsBot {
await this.register_commands();
}
public async assign_role(user_id: string, guild_id: string, role_id: string) {
const guild = await this.bot.guilds.fetch(guild_id);
const member = await guild.members.fetch({ user: user_id });
const role = await guild.roles.fetch(role_id);
if (role === null) return console.error("Role", role_id, "not found in guild", guild_id);
member.roles.add(role_id);
log(`Assigned role '${role.name}' to user '${member.displayName}'.`);
}
public async *receive_associations() {
while (true) yield await this.assoc_channel.receive();
}
public bot_name() {
return this.bot.user?.displayName;
}
private async register_commands() {
const cmd = new SlashCommandBuilder()
.setName("associate")
@ -74,19 +91,6 @@ export class EpitlsBot {
if (result === true) interaction.editReply(message_command_response_success());
else interaction.editReply(message_command_response_error(result));
}
async assign_role(user_id: string, guild_id: string, role_id: string) {
const guild = await this.bot.guilds.fetch(guild_id);
const member = await guild.members.fetch({ user: user_id });
const role = await guild.roles.fetch(role_id);
if (role === null) return console.error("Role", role_id, "not found in guild", guild_id);
member.roles.add(role_id);
log(`Assigned role '${role.name}' to user '${member.displayName}'.`);
}
async *receive_associations() {
while (true) yield await this.assoc_channel.receive();
}
}
function message_command_response_sending_email(email: string) {