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

Commit

Permalink
feat(arguements): Better argument options
Browse files Browse the repository at this point in the history
  • Loading branch information
Tricked-dev committed Jun 11, 2021
1 parent db7df02 commit 6ae209a
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 155 deletions.
14 changes: 7 additions & 7 deletions src/struct/NaticoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ export class NaticoClient extends EventEmitter {
if (util) this.util = new ClientUtil(this);
}
/**
*
* @param event Add a event to be emitted
*/
*
* @param event Add a event to be emitted
*/
addEvent(event: string) {
this.events[event] = (...args: any[]) => this.emit(event, ...args);
}
/**
*
* @param token The token used for logging in
* @returns
*/
*
* @param token The token used for logging in
* @returns
*/
login(token: string) {
return startBot({
token,
Expand Down
37 changes: 19 additions & 18 deletions src/struct/commands/ArgumentGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,13 @@ import { NaticoClient } from "../NaticoClient.ts";
import { NaticoCommand } from "./Command.ts";

import { Matches } from "../../util/Interfaces.ts";
import {
DiscordenoMessage,
Lexer,
longShortStrategy,
Parser,
} from "../../../deps.ts";
import { DiscordenoMessage, Lexer, longShortStrategy, Parser } from "../../../deps.ts";
export class ArgumentGenerator {
client: NaticoClient;
constructor(client: NaticoClient) {
this.client = client;
}
async generateArgs(
command: NaticoCommand,
message: DiscordenoMessage,
args?: string,
) {
async generateArgs(command: NaticoCommand, message: DiscordenoMessage, args?: string) {
if (!args) return {};
const lout = new Lexer(args)
.setQuotes([
Expand Down Expand Up @@ -53,18 +44,28 @@ export class ArgumentGenerator {
}
}
if (command?.options) {
let restContent = rest.join(" ");

for (const option of command.options) {
const name = option.name;

//Rest means that everything will be cut off
if (option.match == Matches.rest) {
data[name] = args;
if (option.customType) {
data[option.name] = await option.customType(
message,
rest.join(" "),
);
} else data[option.name] = rest.join(" ");
const info: string | any[] = await option.customType(message, restContent);
if (Array.isArray(info) && info.length == 2) {
restContent = info[1];

data[name] = info[0];
} else {
data[name] = info;
}
} else data[name] = restContent;
} else if (option.match == Matches.content) {
if (option.customType) {
data[option.name] = await option.customType(message, args);
} else data[option.name] = args;
data[name] = await option.customType(message, args);
} else data[name] = args;
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/struct/commands/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class NaticoCommand extends NaticoModule {
ownerOnly?: boolean;
superUserOnly?: boolean;
permissions?: PermissionStrings[];
},
}
) {
super(id);
this.options = options;
Expand All @@ -64,11 +64,7 @@ export class NaticoCommand extends NaticoModule {
this.id = id;

this.aliases = Array.from(
new Set([
...aliases!.map((name: string) => name.toLowerCase()),
id.toLowerCase(),
name!.toLowerCase(),
]),
new Set([...aliases!.map((name: string) => name.toLowerCase()), id.toLowerCase(), name!.toLowerCase()])
);

this.category = category || "general";
Expand Down
134 changes: 49 additions & 85 deletions src/struct/commands/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,24 @@ export class NaticoCommandHandler extends NaticoHandler {
guildonly = false,
handleEdits = false,
}: // handleSlashes = true,
{
directory?: string;
prefix?: prefixFn | string | string[];
IgnoreCD?: string[];
owners?: string[];
/**
* cooldown in millieseconds
*/
cooldown?: number;
rateLimit?: number;
superusers?: string[];
/**
* Commands will only work in guild channels with this on
*/
guildonly?: boolean;
handleEdits?: boolean;
// handleSlashes?: boolean;
},
{
directory?: string;
prefix?: prefixFn | string | string[];
IgnoreCD?: string[];
owners?: string[];
/**
* cooldown in millieseconds
*/
cooldown?: number;
rateLimit?: number;
superusers?: string[];
/**
* Commands will only work in guild channels with this on
*/
guildonly?: boolean;
handleEdits?: boolean;
// handleSlashes?: boolean;
}
) {
super(client, {
directory,
Expand Down Expand Up @@ -92,11 +92,7 @@ export class NaticoCommandHandler extends NaticoHandler {
return this.handleCommand(message as DiscordenoMessage);
});
}
async commandChecks(
command: NaticoCommand,
message: DiscordenoMessage,
args: string | undefined,
) {
async commandChecks(command: NaticoCommand, message: DiscordenoMessage, args: string | undefined) {
if (this.inhibitorHandler) {
if (await this.inhibitorHandler.runChecks(message, command)) return true;
}
Expand Down Expand Up @@ -131,13 +127,7 @@ export class NaticoCommandHandler extends NaticoHandler {
}

if (command.permissions) {
if (
!hasGuildPermissions(
message!.guildId,
message.authorId,
command.permissions,
)
) {
if (!hasGuildPermissions(message!.guildId, message.authorId, command.permissions)) {
this.emit("userPermissions", message, command, args);
return true;
}
Expand All @@ -159,17 +149,13 @@ export class NaticoCommandHandler extends NaticoHandler {
return false;
}
/**
*
* @param command - Command that gets executed
* @param message - Message object to be passed through
* @param args - arguments to be passed though
* @returns - What the ran command returned
*/
public async runCommand(
command: NaticoCommand,
message: DiscordenoMessage,
args?: string,
) {
*
* @param command - Command that gets executed
* @param message - Message object to be passed through
* @param args - arguments to be passed though
* @returns - What the ran command returned
*/
public async runCommand(command: NaticoCommand, message: DiscordenoMessage, args?: string) {
if (await this.commandChecks(command, message, args)) return false;

try {
Expand All @@ -178,13 +164,10 @@ export class NaticoCommandHandler extends NaticoHandler {
await command.exec(message, data);
this.emit("commandEnded", message, command, data);
/**
* Adding the user to a set and deleting them later!
*/
* Adding the user to a set and deleting them later!
*/
this.cooldowns.add(message.authorId.toString());
setTimeout(
() => this.cooldowns.delete(message.authorId.toString()),
this.cooldown,
);
setTimeout(() => this.cooldowns.delete(message.authorId.toString()), this.cooldown);
} catch (e: unknown) {
this.emit("commandError", message, command, e);
}
Expand All @@ -207,23 +190,14 @@ export class NaticoCommandHandler extends NaticoHandler {
if (message.isBot) return;

/**
* Allowing pings to be used as prefix!
*/
* Allowing pings to be used as prefix!
*/
if (message.content.startsWith(`<@!${this.client.id}>`)) {
console.log("Command found w mention");
const command = message.content
.toLowerCase()
.slice(`<@!${this.client.id}>`.length)
.trim()
.split(" ")[0];
const command = message.content.toLowerCase().slice(`<@!${this.client.id}>`.length).trim().split(" ")[0];
const Command = this.findCommand(command);

if (Command) {
const args = message.content
.slice(`<@!${this.client.id}>`.length)
.trim()
.slice(command.length)
.trim();
const args = message.content.slice(`<@!${this.client.id}>`.length).trim().slice(command.length).trim();

return this.runCommand(Command, message, args);
}
Expand All @@ -241,59 +215,49 @@ export class NaticoCommandHandler extends NaticoHandler {
}
async prefixCheck(prefix: string, message: DiscordenoMessage) {
if (message.content.toLowerCase().startsWith(prefix)) {
const command = message.content
.toLowerCase()
.slice(prefix.length)
.trim()
.split(" ")[0];
const command = message.content.toLowerCase().slice(prefix.length).trim().split(" ")[0];
const Command = this.findCommand(command);
if (Command) {
const args = message.content
.slice(prefix.length)
.trim()
.slice(command.length)
.trim();
const args = message.content.slice(prefix.length).trim().slice(command.length).trim();
await this.runCommand(Command, message, args);
return true;
}
}
}
/**
* Simple function to find a command could be useful outside of the handler
* @param command - Command you want to search for
* @returns Command object or undefined
*/
* Simple function to find a command could be useful outside of the handler
* @param command - Command you want to search for
* @returns Command object or undefined
*/
public findCommand(command: string | undefined): NaticoCommand | undefined {
return this.modules.find((cmd) => {
if (cmd.name == command) {
return true;
}
if (cmd.aliases) {
if (cmd.aliases.includes(<string> command)) {
if (cmd.aliases.includes(<string>command)) {
return true;
}
}
return false;
});
}
/**
* Check if commands have slash data and if they do it will activete it
* be carefull to no accidentally enable them globally,
* first searches if the command is already enabled and if it changed since and edit it accordingly otherwise creates a command
* also deletes unused slash commands
* @param guildID - Specific guild to enable slash commands on
* @returns - List of enabled commands
*/
* Check if commands have slash data and if they do it will activete it
* be carefull to no accidentally enable them globally,
* first searches if the command is already enabled and if it changed since and edit it accordingly otherwise creates a command
* also deletes unused slash commands
* @param guildID - Specific guild to enable slash commands on
* @returns - List of enabled commands
*/
async enableSlash(guildID?: bigint) {
const slashed = this.slashed();
await upsertSlashCommands(slashed, guildID);
return slashed;
}
slashed() {
const commands: EditGlobalApplicationCommand[] = [];
const data = this.modules.filter(
(command) => (command.enabled && command.slash) || false,
);
const data = this.modules.filter((command) => (command.enabled && command.slash) || false);
data.forEach((command: NaticoCommand) => {
const slashdata: EditGlobalApplicationCommand = {
name: command.name || command.id,
Expand Down
5 changes: 1 addition & 4 deletions src/struct/inhibitors/Inhibitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ export class NaticoInhibitor extends NaticoModule {
super(id);
this.priority = priority;
}
exec(
_message: DiscordenoMessage,
_command: NaticoCommand,
): Promise<boolean> | boolean {
exec(_message: DiscordenoMessage, _command: NaticoCommand): Promise<boolean> | boolean {
throw new Error(`${this.id} no implementated`);
}
}
9 changes: 2 additions & 7 deletions src/struct/inhibitors/InhibitorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@ export class NaticoInhibitorHandler extends NaticoHandler {
this.directory = directory;
this.modules = new Collection();
}
async runChecks(
message: DiscordenoMessage,
command: NaticoCommand,
): Promise<boolean> {
const inhibitors = [...this.modules.entries()].sort(
(a, b) => b[1].priority - a[1].priority,
);
async runChecks(message: DiscordenoMessage, command: NaticoCommand): Promise<boolean> {
const inhibitors = [...this.modules.entries()].sort((a, b) => b[1].priority - a[1].priority);
for await (const [, inhibitor] of inhibitors) {
if (await inhibitor.exec(message, command)) return true;
else continue;
Expand Down
5 changes: 1 addition & 4 deletions src/struct/listeners/Listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ export class NaticoListener extends NaticoModule {
declare handler!: NaticoListenerHandler;
event: string;
emitter: string;
constructor(
id: string,
{ event, emitter }: { event: string; emitter: string },
) {
constructor(id: string, { event, emitter }: { event: string; emitter: string }) {
super(id);
this.emitter = emitter;
this.event = event;
Expand Down
5 changes: 1 addition & 4 deletions src/struct/tasks/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ export class NaticoTask extends NaticoModule {
declare handler: NaticoTaskHandler;
delay?: number;
runOnStart?: boolean;
constructor(
id: string,
{ delay, runOnStart = false }: { delay?: number; runOnStart?: boolean },
) {
constructor(id: string, { delay, runOnStart = false }: { delay?: number; runOnStart?: boolean }) {
super(id);
this.delay = delay;
this.runOnStart = runOnStart;
Expand Down
4 changes: 2 additions & 2 deletions src/util/ClientUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export class ClientUtil {
this.client = client;
}
/**
* @returns a sneaky embed
*/
* @returns a sneaky embed
*/
embed() {
return new NaticoEmbed();
}
Expand Down
Loading

0 comments on commit 6ae209a

Please sign in to comment.