Added the ability to add/remove slash commands

This commit is contained in:
ION606
2023-04-18 16:50:15 -04:00
parent 0c278857ce
commit 04af684e13
7 changed files with 84 additions and 121 deletions
+9 -1
View File
@@ -9,6 +9,8 @@ import { exit } from 'process';
import Guild from '../guilds/Guild.js';
import user from '../messages/User.js';
import { Thread } from '../guilds/ThreadManager.js';
import { InteractionManager } from '../interactions/InteractionManager.js';
import { Interaction } from '../interactions/interaction.js';
@@ -43,6 +45,9 @@ export class Client extends EventEmitter {
/** @type {import('axios').AxiosInstance} */
axiosCustom;
/** @type {InteractionManager} */
commands;
/**
* @param {opts} input
*/
@@ -77,10 +82,13 @@ export class Client extends EventEmitter {
this.emit("messageRecieved", msg);
}
ready(response) {
async ready(response) {
this.user_profile = response.profile;
this.user_settings = response.config;
this.id = response.profile.id;
const commandsRaw = (await this.axiosCustom.get(`applications/${this.id}/commands`)).data;
this.commands = new InteractionManager(commandsRaw, this);
this.emit('ready');
}
@@ -0,0 +1,46 @@
import { DataManager } from "../DataManager.js";
import { Interaction } from "./interaction.js";
import {interactionOptionRaw} from "./interactionOptionRaw.js";
export class InteractionManager extends DataManager {
/** @type {Map<String, interactionOptionRaw>} */
#commands;
/**
* @param {String} name
*/
async delete(name) {
if (!this.#commands.has(name)) return;
const int = this.#commands.get(name);
await this.client.axiosCustom.delete(`/applications/${this.client.id}/commands/${int.id}`);
this.#commands.delete(name);
return int;
}
/**
* @param {{name: string, description: string, options: [interactionOptionRaw], type: number, dm_permission: false}} int
*/
async set(int) {
const response = await this.client.axiosCustom.post(`/applications/${this.client.id}/commands`, int);
this.#commands.set(int.name, response);
return true;
}
/**
* @description returns a map of <name, raw_interaction>
*/
list() {
return new Map(this.#commands);
}
constructor(data, client) {
super(client);
this.#commands = new Map();
for (const commandRaw of data) {
this.#commands.set(commandRaw.name, commandRaw);
}
}
}
@@ -0,0 +1,12 @@
export const interactionOptionRaw = {
type: 0,
name: "",
description: "",
id: "",
required: false,
min_value: 0,
max_value: 0,
max_length: 0,
choices: [],
options: []
};