Skip to content
This repository has been archived by the owner on Jan 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #8 from Garlic-Team/dev
Browse files Browse the repository at this point in the history
V1.1.0
  • Loading branch information
S222em authored Dec 1, 2021
2 parents a05345f + 73fb0b1 commit 3bdda25
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 77 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
dist/
dist/
package-lock.json
.idea/
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ src/
.eslintrc.json
.eslintignore

# NPM
package-lock.json

# git
.gitignore
.github
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export * from './structures/Component';
export * from './structures/CustomId';

// Constants
export { ComponentType } from './util/Constants';
export { ComponentType, ComponentOptions } from './util/Constants';
10 changes: 6 additions & 4 deletions src/managers/GComponentHandler.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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();

Expand All @@ -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);
}
});
}
}
16 changes: 11 additions & 5 deletions src/managers/GComponentsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Component>;

constructor(client: Client, components: Collection<string, Component>, dir: string) {
Expand All @@ -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;
Expand All @@ -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}`);
Expand Down
100 changes: 47 additions & 53 deletions src/structures/Color.ts
Original file line number Diff line number Diff line change
@@ -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)}`;
}
}
13 changes: 6 additions & 7 deletions src/structures/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
/* 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;

export class Component {
readonly client: Client;
readonly name: Name | Array<Name>;
readonly type: ComponentType;
readonly run: (interaction: MessageComponentInteraction, args: Array<string>) => unknown;

readonly userRequiredPermissions?: Array<PermissionResolvable>;
private path: string;

constructor(client: Client, options: ComponentOptions) {
this.client = client;
Expand All @@ -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<string>) {
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`);
}
}
6 changes: 5 additions & 1 deletion src/structures/CustomId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ export class CustomId {
public name: string;
public ids: Array<string>;

constructor(options: { name: string, ids: Array<string>}) {
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<string>) {
for (const id in ids) {
this.addId(id);
Expand Down
5 changes: 3 additions & 2 deletions src/util/Constants.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -13,7 +13,8 @@ export const Events = {
export interface ComponentOptions {
name: string | RegExp;
type: 'BUTTON' | 'SELECT_MENU';
userRequiredPermissions?: PermissionResolvable | Array<PermissionResolvable> ;
userRequiredPermissions?: PermissionResolvable | Array<PermissionResolvable>;
run: (interaction: MessageComponentInteraction, args: Array<string>) => unknown;
}

export interface GComponentsOptions {
Expand Down

0 comments on commit 3bdda25

Please sign in to comment.