Skip to content

Commit

Permalink
feat(Monitor): add decorators (#7)
Browse files Browse the repository at this point in the history
* feat(monitor): define decorators

* docs: add param definitions

* chore: update lock file

* chore: remove @skyra/decorators

* chore: enable const enum preservation

* feat: optional status
  • Loading branch information
Quantumlyy committed Jul 22, 2020
1 parent 88afac3 commit 5d3accd
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 80 deletions.
38 changes: 19 additions & 19 deletions src/lib/structures/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,104 +11,104 @@ import { CooldownLevel } from '../types/Enums';
export abstract class Command extends AliasPiece {
/**
* Permissions required by the bot to run the command
* @since 0.0.1
* @since 1.0.0
*/
public requiredPermissions: Permissions;

/**
* Delete command's response if the trigger message was deleted
* @since 0.0.1
* @since 1.0.0
*/
public deletable: boolean;

/**
* A basic summary about the command
* @since 0.0.1
* @since 1.0.0
*/
public description: string;

/**
* Longer version of command's summary and how to use it
* @since 0.0.1
* @since 1.0.0
*/
public extendedHelp: string;

/**
* Full category name of the command
* @since 0.0.1
* @since 1.0.0
*/
public fullCategory: string[];

/**
* Allow disabling of the command in a guild or not
* @since 0.0.1
* @since 1.0.0
*/
public guarded: boolean;

/**
* Whehter to show the command in the help message or not
* @since 0.0.1
* @since 1.0.0
*/
public hidden: boolean;

/**
* If the command will only work in NSFW channels
* @since 0.0.1
* @since 1.0.0
*/
public nsfw: boolean;

/**
* Required level of permission to use the command
* @since 0.0.1
* @since 1.0.0
*/
public permissionLevel: number;

/**
* Number of re-prompts of an argument
* @since 0.0.1
* @since 1.0.0
*/
public promptLimit: number;

/**
* Time allowed for re-prompts
* @since 0.0.1
* @since 1.0.0
*/
public promptTime: number;

/**
* Accepted flags for the command
* @since 0.0.1
* @since 1.0.0
*/
public flags: string[];

/**
* Allow use of quoted strings for arguments
* @since 0.0.1
* @since 1.0.0
*/
public quotedStringSupport: boolean;

/**
* Which type of channel the command can execute in
* @since 0.0.1
* @since 1.0.0
*/
public runIn: ChannelType[];

// todo - public usage: CommandUsage;

/**
* Whether the cooldown applies to author, channel or guild
* @since 0.0.1
* @since 1.0.0
*/
public cooldownLevel: CooldownLevel;

/**
* The time and limit for cooldown
* @since 0.0.1
* @since 1.0.0
*/
public cooldowns: RateLimitManager;

/**
* @since 0.0.1
* @since 1.0.0
* @param store The command store
* @param directory The base directory to the pieces folder
* @param file The path from the pieces folder to the command file
Expand Down Expand Up @@ -144,7 +144,7 @@ export abstract class Command extends AliasPiece {

/**
* The main category for the command
* @since 0.0.1
* @since 1.0.0
* @readonly
*/
public get category(): string {
Expand All @@ -153,7 +153,7 @@ export abstract class Command extends AliasPiece {

/**
* The sub category for the command
* @since 0.0.1
* @since 1.0.0
* @readonly
*/
public get subCategory(): string {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/structures/CommandStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { AliasStore, PieceConstructor, Client } from '@klasa/core';

/**
* Stores all Command pieces
* @since 0.0.1
* @since 1.0.0
*/
export class CommandStore extends AliasStore<Command> {
/**
* Constructs the Command Store for use
* @since 0.0.1
* @since 1.0.0
* @param client The framework client
*/
public constructor(client: Client) {
Expand Down
84 changes: 48 additions & 36 deletions src/lib/structures/Monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,6 @@ import { Piece, PieceOptions, Message } from '@klasa/core';
import type { MonitorStore } from './MonitorStore';

export abstract class Monitor extends Piece {
/**
* Should this monitor ignore bots
* @default false
*/
public ignoreBots = false;

/**
* Should this monitor ignore users
* @default false
*/
public ignoreUsers = false;

/**
* Should this monitor ignore messages sent by the bot itself
* @default false
*/
public ignoreSelf = false;

/**
* Should this monitor ignore everyone except itself
* @default false
*/
public ignoreOthers = false;

/**
* Should this monitor ignore webhook messages
* @default false
*/
public ignoreWebhooks = false;

/**
* Should this monitor ignore message edits
* @default false
*/
public ignoreEdits = false;

public constructor(store: MonitorStore, directory: string, file: string[], options: PieceOptions) {
super(store, directory, file, options);
}
Expand Down Expand Up @@ -67,3 +31,51 @@ export abstract class Monitor extends Piece {
}
}
}

/**
* The Interface defining all ignore groups.
* @since 1.0.0
*/
export interface Monitor {
/**
* Should this monitor ignore bots
* @since 1.0.0
* @public
*/
ignoreBots?: boolean;

/**
* Should this monitor ignore users
* @since 1.0.0
* @public
*/
ignoreUsers?: boolean;

/**
* Should this monitor ignore messages sent by the bot itself
* @since 1.0.0
* @public
*/
ignoreSelf?: boolean;

/**
* Should this monitor ignore everyone except itself
* @since 1.0.0
* @public
*/
ignoreOthers?: boolean;

/**
* Should this monitor ignore webhook messages
* @since 1.0.0
* @public
*/
ignoreWebhooks?: boolean;

/**
* Should this monitor ignore message edits
* @since 1.0.0
* @public
*/
ignoreEdits?: boolean;
}
4 changes: 2 additions & 2 deletions src/lib/structures/MonitorStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { Monitor } from './Monitor';

/**
* Stores all Monitor pieces
* @since 0.0.1
* @since 1.0.0
*/
export class MonitorStore extends Store<Monitor> {
/**
* Constructs the Monitor Store for use
* @since 0.0.1
* @since 1.0.0
* @param client The framework client
*/
public constructor(client: Client) {
Expand Down
File renamed without changes.
File renamed without changes.
68 changes: 68 additions & 0 deletions src/lib/utils/decorators/monitor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { Constructor } from '@klasa/core';
import { createClassDecorator, createProxy } from './skyra-decorators-utils';
import type { Monitor } from '../../structures/Monitor';

/**
* @since 1.0.0
* @param prop The property on which the status should be assigned.
* @param status Wheter the property should be ignored or not.
* @private
*/
export function monitorIgnoreOptions(prop: string, status?: boolean) {
return createClassDecorator((target: Constructor<Monitor>) =>
createProxy(target, {
construct: (ctor, [store, directory, files, options]): Monitor => {
const command = new ctor(store, directory, files, options);
Reflect.defineProperty(command, prop, {
value: status ?? false,
enumerable: true,
configurable: true,
writable: true
});
return command;
}
})
);
}

/**
* Should the decorated monitor ignore bots.
* @since 1.0.0
* @param status Whether the given group should be ignored or not.
*/
export const ignoreBots = (status?: boolean) => monitorIgnoreOptions('ignoreBots', status);

/**
* Should the decorated monitor ignore users.
* @since 1.0.0
* @param status Whether the given group should be ignored or not.
*/
export const ignoreUsers = (status?: boolean) => monitorIgnoreOptions('ignoreUsers', status);

/**
* Should the decorated monitor ignore its self.
* @since 1.0.0
* @param status Whether the given group should be ignored or not.
*/
export const ignoreSelf = (status?: boolean) => monitorIgnoreOptions('ignoreSelf', status);

/**
* Should the decorated monitor ignore others.
* @since 1.0.0
* @param status Whether the given group should be ignored or not.
*/
export const ignoreOthers = (status?: boolean) => monitorIgnoreOptions('ignoreOthers', status);

/**
* Should the decorated monitor ignore webhooks.
* @since 1.0.0
* @param status Whether the given group should be ignored or not.
*/
export const ignoreWebhooks = (status?: boolean) => monitorIgnoreOptions('ignoreWebhooks', status);

/**
* Should the decorated monitor ignore edits.
* @since 1.0.0
* @param status Whether the given group should be ignored or not.
*/
export const ignoreEdits = (status?: boolean) => monitorIgnoreOptions('ignoreEdits', status);
53 changes: 53 additions & 0 deletions src/lib/utils/decorators/skyra-decorators-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* MIT License
*
* Copyright (c) 2018-2020 Kyra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
* Utility to make a class decorator with lighter syntax and inferred types.
* @since 1.0.0
* @copyright 2018-2020 Kyra
* @license MIT
* @param fn The class to decorate
*/
export function createClassDecorator<TFunction extends (...args: any[]) => void>(fn: TFunction): ClassDecorator {
return fn;
}

/**
* Creates a new proxy to efficiently add properties to class without creating subclasses
* @since 1.0.0
* @copyright 2018-2020 Kyra
* @license MIT
* @param target The constructor of the class to modify
* @param handler The handler function to modify the constructor behaviour for the target
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function createProxy<T extends object>(target: T, handler: Omit<ProxyHandler<T>, 'get'>): T {
return new Proxy(target, {
...handler,
get: (target, property) => {
const value = Reflect.get(target, property);
return typeof value === 'function' ? (...args: readonly unknown[]) => value.apply(target, args) : value;
}
});
}
3 changes: 2 additions & 1 deletion src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"compilerOptions": {
"rootDir": "./",
"outDir": "../dist",
"composite": true
"composite": true,
"preserveConstEnums": true
},
"include": ["."]
}
Loading

0 comments on commit 5d3accd

Please sign in to comment.