diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-07-30 19:38:16 -0400 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-07-30 19:38:16 -0400 |
commit | 70fac30661ee06b07baceed6e44880b16e244626 (patch) | |
tree | 44b4c573043fc369ba9c79ef368da2f08f34e442 /src/lib | |
parent | f5c2b7b946487c2828365cc63bc6f471dd6cfc2f (diff) | |
download | tanzanite-70fac30661ee06b07baceed6e44880b16e244626.tar.gz tanzanite-70fac30661ee06b07baceed6e44880b16e244626.tar.bz2 tanzanite-70fac30661ee06b07baceed6e44880b16e244626.zip |
general clean up and fix automod
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/badwords.json | 3 | ||||
-rw-r--r-- | src/lib/extensions/discord-akairo/BushClient.ts | 4 | ||||
-rw-r--r-- | src/lib/extensions/discord-akairo/BushClientUtil.ts | 120 | ||||
-rw-r--r-- | src/lib/extensions/discord-akairo/BushCommand.ts | 81 |
4 files changed, 191 insertions, 17 deletions
diff --git a/src/lib/badwords.json b/src/lib/badwords.json index 94c854f..6ea3618 100644 --- a/src/lib/badwords.json +++ b/src/lib/badwords.json @@ -11,5 +11,6 @@ "hi, bro h am leaving cs:go and giving away my skin": 3, "hi friend, today i am leaving this fucking game": 3, "hi guys, i'm leaving this fucking game, take my": 3, - "you can choose any skin for yourself": 3 + "you can choose any skin for yourself": 3, + "ironmoon": 1 } diff --git a/src/lib/extensions/discord-akairo/BushClient.ts b/src/lib/extensions/discord-akairo/BushClient.ts index 54b5250..7b270f6 100644 --- a/src/lib/extensions/discord-akairo/BushClient.ts +++ b/src/lib/extensions/discord-akairo/BushClient.ts @@ -20,6 +20,7 @@ import readline from 'readline'; import { Sequelize } from 'sequelize'; import { contentWithDurationTypeCaster } from '../../../arguments/contentWithDuration'; import { durationTypeCaster } from '../../../arguments/duration'; +import { permissionTypeCaster } from '../../../arguments/permission'; import { UpdateCacheTask } from '../../../tasks/updateCache'; import { ActivePunishment } from '../../models/ActivePunishment'; import { Global } from '../../models/Global'; @@ -228,7 +229,8 @@ export class BushClient extends AkairoClient { }); this.commandHandler.resolver.addTypes({ duration: durationTypeCaster, - contentWithDuration: contentWithDurationTypeCaster + contentWithDuration: contentWithDurationTypeCaster, + permission: permissionTypeCaster }); // loads all the handlers const loaders = { diff --git a/src/lib/extensions/discord-akairo/BushClientUtil.ts b/src/lib/extensions/discord-akairo/BushClientUtil.ts index b6c9c1f..aecc635 100644 --- a/src/lib/extensions/discord-akairo/BushClientUtil.ts +++ b/src/lib/extensions/discord-akairo/BushClientUtil.ts @@ -2,6 +2,7 @@ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { + BushArgumentType, BushCache, BushClient, BushConstants, @@ -16,7 +17,7 @@ import { ModLogType } from '@lib'; import { exec } from 'child_process'; -import { ClientUtil } from 'discord-akairo'; +import { Argument, ArgumentTypeCaster, ClientUtil, Flag, ParsedValuePredicate, TypeResolver } from 'discord-akairo'; import { APIMessage } from 'discord-api-types'; import { ButtonInteraction, @@ -796,4 +797,121 @@ export class BushClientUtil extends ClientUtil { public capitalizeFirstLetter(string: string): string { return string.charAt(0)?.toUpperCase() + string.slice(1); } + + public arg = new (class Arg { + /** + * Casts a phrase to this argument's type. + * @param type - The type to cast to. + * @param resolver - The type resolver. + * @param message - Message that called the command. + * @param phrase - Phrase to process. + */ + public cast(type: BushArgumentType, resolver: TypeResolver, message: Message, phrase: string): Promise<any> { + return Argument.cast(type, resolver, message, phrase); + } + + /** + * Creates a type that is the left-to-right composition of the given types. + * If any of the types fails, the entire composition fails. + * @param types - Types to use. + */ + public compose(...types: BushArgumentType[]): ArgumentTypeCaster { + return Argument.compose(types); + } + + /** + * Creates a type that is the left-to-right composition of the given types. + * If any of the types fails, the composition still continues with the failure passed on. + * @param types - Types to use. + */ + public composeWithFailure(...types: BushArgumentType[]): ArgumentTypeCaster { + return Argument.composeWithFailure(types); + } + + /** + * Checks if something is null, undefined, or a fail flag. + * @param value - Value to check. + */ + public isFailure(value: any): value is null | undefined | (Flag & { value: any }) { + return Argument.isFailure(value); + } + + /** + * Creates a type from multiple types (product type). + * Only inputs where each type resolves with a non-void value are valid. + * @param types - Types to use. + */ + public product(...types: BushArgumentType[]): ArgumentTypeCaster { + return Argument.product(types); + } + + /** + * Creates a type where the parsed value must be within a range. + * @param type - The type to use. + * @param min - Minimum value. + * @param max - Maximum value. + * @param inclusive - Whether or not to be inclusive on the upper bound. + */ + public range(type: BushArgumentType, min: number, max: number, inclusive?: boolean): ArgumentTypeCaster { + return Argument.range(type, min, max, inclusive); + } + + /** + * Creates a type that parses as normal but also tags it with some data. + * Result is in an object `{ tag, value }` and wrapped in `Flag.fail` when failed. + * @param type - The type to use. + * @param tag - Tag to add. Defaults to the `type` argument, so useful if it is a string. + */ + public tagged(type: BushArgumentType, tag?: any): ArgumentTypeCaster { + return Argument.tagged(type, tag); + } + + /** + * Creates a type from multiple types (union type). + * The first type that resolves to a non-void value is used. + * Each type will also be tagged using `tagged` with themselves. + * @param types - Types to use. + */ + public taggedUnion(...types: BushArgumentType[]): ArgumentTypeCaster { + return Argument.taggedUnion(types); + } + + /** + * Creates a type that parses as normal but also tags it with some data and carries the original input. + * Result is in an object `{ tag, input, value }` and wrapped in `Flag.fail` when failed. + * @param type - The type to use. + * @param tag - Tag to add. Defaults to the `type` argument, so useful if it is a string. + */ + public taggedWithInput(type: BushArgumentType, tag?: any): ArgumentTypeCaster { + return Argument.taggedWithInput(type, tag); + } + + /** + * Creates a type from multiple types (union type). + * The first type that resolves to a non-void value is used. + * @param types - Types to use. + */ + public union(...types: BushArgumentType[]): ArgumentTypeCaster { + return Argument.union(types); + } + + /** + * Creates a type with extra validation. + * If the predicate is not true, the value is considered invalid. + * @param type - The type to use. + * @param predicate - The predicate function. + */ + public validate(type: BushArgumentType, predicate: ParsedValuePredicate): ArgumentTypeCaster { + return Argument.validate(type, predicate); + } + + /** + * Creates a type that parses as normal but also carries the original input. + * Result is in an object `{ input, value }` and wrapped in `Flag.fail` when failed. + * @param type - The type to use. + */ + public withInput(type: BushArgumentType): ArgumentTypeCaster { + return Argument.withInput(type); + } + })(); } diff --git a/src/lib/extensions/discord-akairo/BushCommand.ts b/src/lib/extensions/discord-akairo/BushCommand.ts index 3f79aeb..6616d1d 100644 --- a/src/lib/extensions/discord-akairo/BushCommand.ts +++ b/src/lib/extensions/discord-akairo/BushCommand.ts @@ -1,20 +1,13 @@ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ /* eslint-disable @typescript-eslint/no-explicit-any */ -import { - ArgumentGenerator, - ArgumentOptions, - ArgumentPromptOptions, - ArgumentTypeCaster, - Command, - CommandOptions -} from 'discord-akairo'; +import { ArgumentOptions, ArgumentPromptOptions, ArgumentTypeCaster, Command, CommandOptions } from 'discord-akairo'; import { Snowflake } from 'discord.js'; import { BushMessage } from '../discord.js/BushMessage'; import { BushClient } from './BushClient'; import { BushCommandHandler } from './BushCommandHandler'; import { BushSlashMessage } from './BushSlashMessage'; -type BushArgumentType = +export type BushArgumentType = | 'string' | 'lowercase' | 'uppercase' @@ -67,17 +60,74 @@ type BushArgumentType = | 'command' | 'inhibitor' | 'listener' - | 'duration'; - -interface BaseBushArgumentOptions extends ArgumentOptions { + | 'duration' + | 'contentWithDuration' + | 'permission'; +interface BaseBushArgumentOptions extends Omit<ArgumentOptions, 'type'> { id: string; description?: string; prompt?: ArgumentPromptOptions; } export interface BushArgumentOptions extends BaseBushArgumentOptions { + /** + * The type that the argument should be cast to. + * - `string` does not cast to any type. + * - `lowercase` makes the input lowercase. + * - `uppercase` makes the input uppercase. + * - `charCodes` transforms the input to an array of char codes. + * - `number` casts to a number. + * - `integer` casts to an integer. + * - `bigint` casts to a big integer. + * - `url` casts to an `URL` object. + * - `date` casts to a `Date` object. + * - `color` casts a hex code to an integer. + * - `commandAlias` tries to resolve to a command from an alias. + * - `command` matches the ID of a command. + * - `inhibitor` matches the ID of an inhibitor. + * - `listener` matches the ID of a listener. + * + * Possible Discord-related types. + * These types can be plural (add an 's' to the end) and a collection of matching objects will be used. + * - `user` tries to resolve to a user. + * - `member` tries to resolve to a member. + * - `relevant` tries to resolve to a relevant user, works in both guilds and DMs. + * - `channel` tries to resolve to a channel. + * - `textChannel` tries to resolve to a text channel. + * - `voiceChannel` tries to resolve to a voice channel. + * - `stageChannel` tries to resolve to a stage channel. + * - `threadChannel` tries to resolve a thread channel. + * - `role` tries to resolve to a role. + * - `emoji` tries to resolve to a custom emoji. + * - `guild` tries to resolve to a guild. + * - `permission` tries to resolve to a permissions. + * + * Other Discord-related types: + * - `message` tries to fetch a message from an ID within the channel. + * - `guildMessage` tries to fetch a message from an ID within the guild. + * - `relevantMessage` is a combination of the above, works in both guilds and DMs. + * - `invite` tries to fetch an invite object from a link. + * - `userMention` matches a mention of a user. + * - `memberMention` matches a mention of a guild member. + * - `channelMention` matches a mention of a channel. + * - `roleMention` matches a mention of a role. + * - `emojiMention` matches a mention of an emoji. + * + * Misc: + * - `duration` tries to parse duration in milliseconds + * - `contentWithDuration` tries to parse duration in milliseconds and returns the remaining content with the duration + * removed + */ type?: BushArgumentType; } export interface CustomBushArgumentOptions extends BaseBushArgumentOptions { + /** + * An array of strings can be used to restrict input to only those strings, case insensitive. + * The array can also contain an inner array of strings, for aliases. + * If so, the first entry of the array will be used as the final argument. + * + * A regular expression can also be used. + * The evaluated argument will be an object containing the `match` and `matches` if global. + */ customType?: ArgumentTypeCaster | (string | string[])[] | RegExp | string; } @@ -90,7 +140,7 @@ export interface BushCommandOptions extends CommandOptions { usage: string | string[]; examples: string | string[]; }; - args?: BushArgumentOptions[] | CustomBushArgumentOptions[] | ArgumentGenerator; + args?: BushArgumentOptions[] & CustomBushArgumentOptions[]; category: string; completelyHide?: boolean; } @@ -124,7 +174,10 @@ export class BushCommand extends Command { this.completelyHide = options.completelyHide; if (options.args && typeof options.args !== 'function') { options.args.forEach((arg: BushArgumentOptions | CustomBushArgumentOptions) => { - if (arg['customType']) arg.type = arg['customType']; + if (arg['customType']) { + arg['type'] = arg['customType']; + delete arg['customType']; + } }); } } |