diff --git a/.gitignore b/.gitignore index d26c746..aabd37f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules -dist/ \ No newline at end of file +dist/ +package-lock.json +.idea/ diff --git a/.npmignore b/.npmignore index ade6b48..f60671c 100644 --- a/.npmignore +++ b/.npmignore @@ -7,6 +7,9 @@ src/ .eslintrc.json .eslintignore +# NPM +package-lock.json + # git .gitignore .github diff --git a/package-lock.json b/package-lock.json index 8e13046..7b7fcb4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@gcommands/components", - "version": "1.0.2-dev", + "version": "1.0.5-dev", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@gcommands/components", - "version": "1.0.2-dev", + "version": "1.0.5-dev", "license": "ISC", "dependencies": { "discord.js": "^13.2.0" diff --git a/package.json b/package.json index 8dcb208..937a2d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gcommands/components", - "version": "1.0.4", + "version": "1.1.0", "description": "Component handler for discord.js and gcommands", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/index.ts b/src/index.ts index c443115..146f283 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,4 +7,4 @@ export * from './structures/Component'; export * from './structures/CustomId'; // Constants -export { ComponentType } from './util/Constants'; +export { ComponentType, ComponentOptions } from './util/Constants'; diff --git a/src/managers/GComponentHandler.ts b/src/managers/GComponentHandler.ts index a5e0a29..d0b8630 100644 --- a/src/managers/GComponentHandler.ts +++ b/src/managers/GComponentHandler.ts @@ -1,6 +1,6 @@ import { Client, Collection } from 'discord.js'; -import { Events, ComponentType } from '../util/Constants'; +import { ComponentType, Events } from '../util/Constants'; import { Component } from '../structures/Component'; @@ -20,7 +20,7 @@ export class GComponentHandler { if (!interaction || !interaction.isMessageComponent()) return; try { - const regex = new RegExp('[A-Za-z1-9]+', 'g'); + const regex = new RegExp('[A-Za-z0-9]+', 'gd'); const args = interaction.customId.match(regex); const name = args.shift(); @@ -37,7 +37,9 @@ export class GComponentHandler { if (component.userRequiredPermissions[0] && !interaction.memberPermissions.has(component.userRequiredPermissions)) return interaction.deferReply(); await component.run(interaction, args); - } catch (e) { this.client.emit(Events.DEBUG, e); } - }); + } catch (e) { + this.client.emit(Events.DEBUG, e); + } + }); } } diff --git a/src/managers/GComponentsLoader.ts b/src/managers/GComponentsLoader.ts index 964c0f7..adc4fb9 100644 --- a/src/managers/GComponentsLoader.ts +++ b/src/managers/GComponentsLoader.ts @@ -10,8 +10,8 @@ import { Events } from '../util/Constants'; import { isClass } from '../util/util'; export class GComponentsLoader { - private client: Client; - private dir: string; + private readonly client: Client; + private readonly dir: string; private components: Collection; constructor(client: Client, components: Collection, dir: string) { @@ -31,7 +31,9 @@ export class GComponentsLoader { if (fsDirent.isDirectory()) { await this.__loadFiles(path.join(dir, rawFileName)); continue; - } else if (!['.js', '.ts'].includes(fileType)) { continue; } + } else if (!['.js', '.ts'].includes(fileType)) { + continue; + } let file = await import(path.join(dir, rawFileName)); if (file.default) file = file.default; @@ -40,10 +42,14 @@ export class GComponentsLoader { if (isClass(file)) { file = new file(this.client); if (!(file instanceof Component)) throw new GError('[COMPONENT]', `Component ${fileName} doesnt belong in components.`); + } else if (typeof file === 'object') { + try { + file = new Component(this.client, file); + } catch { + throw new GError('[COMPONENT]', `Component ${fileName} doesnt belong in components.`); + } } - file.path = `${dir}/${fileName}${fileType}`; - if (Array.isArray(file.name)) { for (const name of file.name) { if (this.components.has(name)) throw new GError('[COMPONENT]', `Duplicate component found: ${name}`); diff --git a/src/structures/Color.ts b/src/structures/Color.ts index a57c35d..b76f40e 100644 --- a/src/structures/Color.ts +++ b/src/structures/Color.ts @@ -1,60 +1,54 @@ export class Color { public text: string; - constructor(text: string) { - this.text = text; + constructor(text: string) { + this.text = text + .replace(/&0/g, '\x1b[30m') + .replace(/&c/g, '\x1b[31m') + .replace(/&a/g, '\x1b[32m') + .replace(/&e/g, '\x1b[33m') + .replace(/&b/g, '\x1b[34m') + .replace(/&d/g, '\x1b[35m') + .replace(/&3/g, '\x1b[36m') + .replace(/&f/g, '\x1b[37m') + .replace(/&8/g, '\x1b[90m') + .replace(/&h/g, '\x1b[91m') + .replace(/&9/g, '\x1b[92m') + .replace(/&4/g, '\x1b[93m') + .replace(/&2/g, '\x1b[94m') + .replace(/&6/g, '\x1b[95m') + .replace(/&7/g, '\x1b[96m') + .replace(/&i/g, '\x1b[97m') + .replace(/&B0/g, '\x1b[40m') + .replace(/&Bc/g, '\x1b[41m') + .replace(/&Ba/g, '\x1b[42m') + .replace(/&Be/g, '\x1b[43m') + .replace(/&Bb/g, '\x1b[44m') + .replace(/&Bd/g, '\x1b[45m') + .replace(/&B3/g, '\x1b[46m') + .replace(/&Bf/g, '\x1b[47m') + .replace(/&B8/g, '\x1b[100m') + .replace(/&Bh/g, '\x1b[101m') + .replace(/&B9/g, '\x1b[102m') + .replace(/&B4/g, '\x1b[103m') + .replace(/&B2/g, '\x1b[104m') + .replace(/&B6/g, '\x1b[105m') + .replace(/&B7/g, '\x1b[106m') + .replace(/&Bi/g, '\x1b[107m') + .replace(/&r/g, '\x1b[0m') + .replace(/&l/g, '\x1b[1m') + .replace(/&n/g, '\x1b[4m') + .replace(/&k/g, '\x1b[5m') + .replace(/&p/g, '\x1b[7m'); + } - this.text = this.text - // COLORS - .replace(/&0/g, '\x1b[30m') - .replace(/&c/g, '\x1b[31m') - .replace(/&a/g, '\x1b[32m') - .replace(/&e/g, '\x1b[33m') - .replace(/&b/g, '\x1b[34m') - .replace(/&d/g, '\x1b[35m') - .replace(/&3/g, '\x1b[36m') - .replace(/&f/g, '\x1b[37m') - .replace(/&8/g, '\x1b[90m') - .replace(/&h/g, '\x1b[91m') - .replace(/&9/g, '\x1b[92m') - .replace(/&4/g, '\x1b[93m') - .replace(/&2/g, '\x1b[94m') - .replace(/&6/g, '\x1b[95m') - .replace(/&7/g, '\x1b[96m') - .replace(/&i/g, '\x1b[97m') + getText() { + return `${this.text}\x1b[0m`; + } - .replace(/&B0/g, '\x1b[40m') - .replace(/&Bc/g, '\x1b[41m') - .replace(/&Ba/g, '\x1b[42m') - .replace(/&Be/g, '\x1b[43m') - .replace(/&Bb/g, '\x1b[44m') - .replace(/&Bd/g, '\x1b[45m') - .replace(/&B3/g, '\x1b[46m') - .replace(/&Bf/g, '\x1b[47m') - .replace(/&B8/g, '\x1b[100m') - .replace(/&Bh/g, '\x1b[101m') - .replace(/&B9/g, '\x1b[102m') - .replace(/&B4/g, '\x1b[103m') - .replace(/&B2/g, '\x1b[104m') - .replace(/&B6/g, '\x1b[105m') - .replace(/&B7/g, '\x1b[106m') - .replace(/&Bi/g, '\x1b[107m') + getRGB() { + const get = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(this.text); - // OTHER - .replace(/&r/g, '\x1b[0m') - .replace(/&l/g, '\x1b[1m') - .replace(/&n/g, '\x1b[4m') - .replace(/&k/g, '\x1b[5m') - .replace(/&p/g, '\x1b[7m'); - } - - getText() { - return `${this.text}\x1b[0m`; - } - - getRGB() { - const get = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(this.text); - - return `r: ${parseInt(get[1], 16)}, g: ${parseInt(get[2], 16)}, b: ${parseInt(get[3], 16)}`; - } + return `r: ${parseInt(get[1], 16)}, g: ${parseInt(get[2], 16)}, b: ${parseInt(get[3], 16)}`; + } } diff --git a/src/structures/Component.ts b/src/structures/Component.ts index f3342b6..371b363 100644 --- a/src/structures/Component.ts +++ b/src/structures/Component.ts @@ -2,9 +2,9 @@ /* eslint-disable no-unused-vars */ import { Client, MessageComponentInteraction, PermissionResolvable } from 'discord.js'; -import { GError } from '../structures/GError'; +import { GError } from './GError'; -import { ComponentType, ComponentOptions } from '../util/Constants'; +import { ComponentOptions, ComponentType } from '../util/Constants'; type Name = string | RegExp; @@ -12,8 +12,9 @@ export class Component { readonly client: Client; readonly name: Name | Array; readonly type: ComponentType; + readonly run: (interaction: MessageComponentInteraction, args: Array) => unknown; + readonly userRequiredPermissions?: Array; - private path: string; constructor(client: Client, options: ComponentOptions) { this.client = client; @@ -23,14 +24,12 @@ export class Component { this.name = options.name; this.type = ComponentType[options.type]; this.userRequiredPermissions = options.userRequiredPermissions !== undefined ? Array.isArray(options.userRequiredPermissions) ? options.userRequiredPermissions : [options.userRequiredPermissions] : []; - } - - public run(interaction: MessageComponentInteraction, args: Array) { - throw new GError('[COMPONENT]',`Component ${this.name} doesn't provide a run method!`); + if (options.run) this.run = options.run; } private validate(options: ComponentOptions) { if (typeof options.name !== 'string' && !(options.name instanceof RegExp) && !Array.isArray(options.name)) throw new GError(`[COMPONENT]`, `Name must be a string, RegExp or array`); if (!ComponentType[options.type]) throw new GError(`[COMPONENT ${options.name}]`, `Type must be a valid ComponentType`); + if (!options.run && !this.run) throw new GError(`[COMPONENT ${options.name}]`, `The run function must be provided`); } } diff --git a/src/structures/CustomId.ts b/src/structures/CustomId.ts index adc98bf..eb9fb38 100644 --- a/src/structures/CustomId.ts +++ b/src/structures/CustomId.ts @@ -2,21 +2,25 @@ export class CustomId { public name: string; public ids: Array; - constructor(options: { name: string, ids: Array}) { + constructor(options: { name: string, ids: string[] }) { this.name = options.name; this.ids = options.ids !== undefined ? options.ids : []; } + get() { return `${this.name}${this.ids[0] ? `-${this.ids.join('-')}` : ''}`; } + setName(name: string) { this.name = name; return this; } + addId(id: string) { this.ids.push(id); return this; } + addIds(ids: Array) { for (const id in ids) { this.addId(id); diff --git a/src/util/Constants.ts b/src/util/Constants.ts index b23b6d5..8060f42 100644 --- a/src/util/Constants.ts +++ b/src/util/Constants.ts @@ -1,5 +1,5 @@ /* eslint-disable no-unused-vars */ -import { PermissionResolvable } from 'discord.js'; +import { MessageComponentInteraction, PermissionResolvable } from 'discord.js'; export const Events = { DEBUG: 'debug', @@ -13,7 +13,8 @@ export const Events = { export interface ComponentOptions { name: string | RegExp; type: 'BUTTON' | 'SELECT_MENU'; - userRequiredPermissions?: PermissionResolvable | Array ; + userRequiredPermissions?: PermissionResolvable | Array; + run: (interaction: MessageComponentInteraction, args: Array) => unknown; } export interface GComponentsOptions {