diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-06-24 00:56:16 -0400 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-06-24 00:56:16 -0400 |
commit | 4176b6258e44e4a095376aaf0f4c687244243a69 (patch) | |
tree | 3b9144be9a2045483c90d92fff05b3ca0b288e52 /src/lib/extensions | |
parent | e80446e23060c0325bbd6db620920d86694ec3ce (diff) | |
download | tanzanite-4176b6258e44e4a095376aaf0f4c687244243a69.tar.gz tanzanite-4176b6258e44e4a095376aaf0f4c687244243a69.tar.bz2 tanzanite-4176b6258e44e4a095376aaf0f4c687244243a69.zip |
feat(*): Began working on other punishment commands etc
Diffstat (limited to 'src/lib/extensions')
-rw-r--r-- | src/lib/extensions/BushArgumentOptions.ts | 59 | ||||
-rw-r--r-- | src/lib/extensions/BushArgumentTypeCaster.ts | 4 | ||||
-rw-r--r-- | src/lib/extensions/BushClient.ts | 44 | ||||
-rw-r--r-- | src/lib/extensions/BushClientUtil.ts | 66 | ||||
-rw-r--r-- | src/lib/extensions/BushCommand.ts | 20 | ||||
-rw-r--r-- | src/lib/extensions/BushCommandHandler.ts | 5 | ||||
-rw-r--r-- | src/lib/extensions/BushInhibitor.ts | 9 | ||||
-rw-r--r-- | src/lib/extensions/BushInteractionMessage.ts | 5 | ||||
-rw-r--r-- | src/lib/extensions/BushTaskHandler.ts | 1 |
9 files changed, 155 insertions, 58 deletions
diff --git a/src/lib/extensions/BushArgumentOptions.ts b/src/lib/extensions/BushArgumentOptions.ts new file mode 100644 index 0000000..bbbc04b --- /dev/null +++ b/src/lib/extensions/BushArgumentOptions.ts @@ -0,0 +1,59 @@ +import { ArgumentOptions, ArgumentTypeCaster } from 'discord-akairo'; + +type BushArgumentType = + | 'string' + | 'lowercase' + | 'uppercase' + | 'charCodes' + | 'number' + | 'integer' + | 'bigint' + | 'emojint' + | 'url' + | 'date' + | 'color' + | 'user' + | 'users' + | 'member' + | 'members' + | 'relevant' + | 'relevants' + | 'channel' + | 'channels' + | 'textChannel' + | 'textChannels' + | 'voiceChannel' + | 'voiceChannels' + | 'categoryChannel' + | 'categoryChannels' + | 'newsChannel' + | 'newsChannels' + | 'storeChannel' + | 'storeChannels' + | 'role' + | 'roles' + | 'emoji' + | 'emojis' + | 'guild' + | 'guilds' + | 'message' + | 'guildMessage' + | 'relevantMessage' + | 'invite' + | 'userMention' + | 'memberMention' + | 'channelMention' + | 'roleMention' + | 'emojiMention' + | 'commandAlias' + | 'command' + | 'inhibitor' + | 'listener' + | 'duration' + | (string | string[])[] + | RegExp + | string; + +export interface BushArgumentOptions extends ArgumentOptions { + type?: BushArgumentType | ArgumentTypeCaster; +} diff --git a/src/lib/extensions/BushArgumentTypeCaster.ts b/src/lib/extensions/BushArgumentTypeCaster.ts new file mode 100644 index 0000000..e000063 --- /dev/null +++ b/src/lib/extensions/BushArgumentTypeCaster.ts @@ -0,0 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +import { BushMessage } from './BushMessage'; + +export type BushArgumentTypeCaster = (message: BushMessage, phrase: string) => any; diff --git a/src/lib/extensions/BushClient.ts b/src/lib/extensions/BushClient.ts index b12fd52..73b0864 100644 --- a/src/lib/extensions/BushClient.ts +++ b/src/lib/extensions/BushClient.ts @@ -1,11 +1,13 @@ import chalk from 'chalk'; -import { AkairoClient, TaskHandler } from 'discord-akairo'; +import { AkairoClient } from 'discord-akairo'; import { APIMessage, Guild, Intents, Message, MessageOptions, Snowflake, UserResolvable } from 'discord.js'; import * as path from 'path'; import { exit } from 'process'; import readline from 'readline'; import { Sequelize } from 'sequelize'; +import { durationTypeCaster } from '../../arguments/duration'; import * as config from '../../config/options'; +import UpdateCacheTask from '../../tasks/updateCache'; import * as Models from '../models'; import AllowedMentions from '../utils/AllowedMentions'; import { BushCache } from '../utils/BushCache'; @@ -15,6 +17,7 @@ import { BushClientUtil } from './BushClientUtil'; import { BushCommandHandler } from './BushCommandHandler'; import { BushInhibitorHandler } from './BushInhinitorHandler'; import { BushListenerHandler } from './BushListenerHandler'; +import { BushTaskHandler } from './BushTaskHandler'; export type BotConfig = typeof config; export type BushMessageType = string | APIMessage | (MessageOptions & { split?: false }); @@ -30,7 +33,7 @@ export class BushClient extends AkairoClient { public listenerHandler: BushListenerHandler; public inhibitorHandler: BushInhibitorHandler; public commandHandler: BushCommandHandler; - public taskHandler: TaskHandler; + public taskHandler: BushTaskHandler; public declare util: BushClientUtil; public declare ownerID: Snowflake[]; public db: Sequelize; @@ -68,7 +71,7 @@ export class BushClient extends AkairoClient { }); // Create task handler - this.taskHandler = new TaskHandler(this, { + this.taskHandler = new BushTaskHandler(this, { directory: path.join(__dirname, '..', '..', 'tasks') }); @@ -76,14 +79,14 @@ export class BushClient extends AkairoClient { this.commandHandler = new BushCommandHandler(this, { directory: path.join(__dirname, '..', '..', 'commands'), prefix: async ({ guild }: { guild: Guild }) => { - if (this.config.dev) return 'dev'; + if (this.config.dev) return 'dev '; const row = await Models.Guild.findByPk(guild.id); return (row?.prefix || this.config.prefix) as string; }, allowMention: true, handleEdits: true, commandUtil: true, - commandUtilLifetime: 3e5, + commandUtilLifetime: 300_000, argumentDefaults: { prompt: { start: 'Placeholder argument prompt. If you see this please tell the devs.', @@ -99,9 +102,8 @@ export class BushClient extends AkairoClient { }, otherwise: '' }, - ignorePermissions: this.config.owners, - ignoreCooldown: this.config.owners, - automateCategories: true, + + automateCategories: false, autoRegisterSlashCommands: true }); @@ -110,7 +112,7 @@ export class BushClient extends AkairoClient { dialect: 'postgres', host: this.config.db.host, port: this.config.db.port, - logging: this.config.logging ? (a) => this.logger.debug(a) : false + logging: this.config.logging.db ? (a) => this.logger.debug(a) : false }); this.logger = new BushLogger(this); } @@ -127,6 +129,8 @@ export class BushClient extends AkairoClient { private async _init(): Promise<void> { this.commandHandler.useListenerHandler(this.listenerHandler); this.commandHandler.useInhibitorHandler(this.inhibitorHandler); + this.commandHandler.ignorePermissions = this.config.owners; + this.commandHandler.ignoreCooldown = this.config.owners.concat(this.cache.global.superUsers); this.listenerHandler.setEmitters({ client: this, commandHandler: this.commandHandler, @@ -137,6 +141,9 @@ export class BushClient extends AkairoClient { stdin: rl, gateway: this.ws }); + this.commandHandler.resolver.addTypes({ + duration: durationTypeCaster + }); // loads all the handlers const loaders = { commands: this.commandHandler, @@ -147,13 +154,15 @@ export class BushClient extends AkairoClient { for (const loader of Object.keys(loaders)) { try { loaders[loader].loadAll(); - this.logger.success('Startup', `Successfully loaded <<${loader}>>.`, false); + await this.logger.success('Startup', `Successfully loaded <<${loader}>>.`, false); } catch (e) { - this.logger.error('Startup', `Unable to load loader <<${loader}>> with error:\n${e?.stack}`, false); + await this.logger.error('Startup', `Unable to load loader <<${loader}>> with error:\n${e?.stack}`, false); } } - this.taskHandler.startAll(); await this.dbPreInit(); + await new UpdateCacheTask().init(this); + this.console.success('Startup', `Successfully created <<global cache>>.`, false); + this.taskHandler.startAll(); } public async dbPreInit(): Promise<void> { @@ -161,14 +170,15 @@ export class BushClient extends AkairoClient { await this.db.authenticate(); Models.Global.initModel(this.db); Models.Guild.initModel(this.db, this); - Models.Modlog.initModel(this.db); + Models.ModLog.initModel(this.db); Models.Ban.initModel(this.db); + Models.Mute.initModel(this.db); Models.Level.initModel(this.db); Models.StickyRole.initModel(this.db); await this.db.sync({ alter: true }); // Sync all tables to fix everything if updated - this.console.success('Startup', `Successfully connected to <<database>>.`, false); + await this.console.success('Startup', `Successfully connected to <<database>>.`, false); } catch (error) { - this.console.error('Startup', `Failed to connect to <<database>> with error:\n` + error?.stack, false); + await this.console.error('Startup', `Failed to connect to <<database>> with error:\n` + error?.stack, false); } } @@ -178,7 +188,7 @@ export class BushClient extends AkairoClient { await this._init(); await this.login(this.token); } catch (e) { - this.console.error('Start', chalk.red(e.stack), false); + await this.console.error('Start', chalk.red(e.stack), false); exit(2); } } @@ -196,6 +206,6 @@ export class BushClient extends AkairoClient { } public isSuperUser(user: UserResolvable): boolean { const userID = this.users.resolveID(user); - return !!BushCache?.superUsers?.includes(userID) || this.config.owners.includes(userID); + return !!BushCache?.global?.superUsers?.includes(userID) || this.config.owners.includes(userID); } } diff --git a/src/lib/extensions/BushClientUtil.ts b/src/lib/extensions/BushClientUtil.ts index a6b049a..34a9e83 100644 --- a/src/lib/extensions/BushClientUtil.ts +++ b/src/lib/extensions/BushClientUtil.ts @@ -2,19 +2,11 @@ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { exec } from 'child_process'; import { ClientUtil } from 'discord-akairo'; -import { - APIInteractionDataResolvedChannel, - APIInteractionDataResolvedGuildMember, - APIMessage, - APIRole, - ApplicationCommandOptionType -} from 'discord-api-types'; +import { APIMessage } from 'discord-api-types'; import { ButtonInteraction, CommandInteraction, - CommandInteractionOption, Constants, - GuildChannel, GuildMember, InteractionReplyOptions, Message, @@ -24,7 +16,6 @@ import { MessageEditOptions, MessageEmbed, MessageOptions, - Role, Snowflake, TextChannel, User, @@ -33,7 +24,7 @@ import { } from 'discord.js'; import got from 'got'; import { promisify } from 'util'; -import { Global } from '../models/Global'; +import { Global } from '../models'; import { BushCache } from '../utils/BushCache'; import { BushClient } from './BushClient'; import { BushMessage } from './BushMessage'; @@ -61,17 +52,6 @@ export interface uuidRes { created_at: string; } -export interface SlashCommandOption<T> { - name: string; - type: ApplicationCommandOptionType; - value?: T; - options?: CommandInteractionOption[]; - user?: User; - member?: GuildMember | APIInteractionDataResolvedGuildMember; - channel?: GuildChannel | APIInteractionDataResolvedChannel; - role?: Role | APIRole; -} - export class BushClientUtil extends ClientUtil { /** The client of this ClientUtil */ public declare client: BushClient; @@ -147,7 +127,6 @@ export class BushClientUtil extends ClientUtil { return `${url}/${res.key}`; } catch (e) { this.client.console.error('Haste', `Unable to upload haste to ${url}`); - continue; } } return 'Unable to post'; @@ -163,8 +142,7 @@ export class BushClientUtil extends ClientUtil { const idMatch = text.match(idReg); if (idMatch) { try { - const user = await this.client.users.fetch(text as Snowflake); - return user; + return await this.client.users.fetch(text as Snowflake); } catch { // pass } @@ -173,8 +151,7 @@ export class BushClientUtil extends ClientUtil { const mentionMatch = text.match(mentionReg); if (mentionMatch) { try { - const user = await this.client.users.fetch(mentionMatch.groups.id as Snowflake); - return user; + return await this.client.users.fetch(mentionMatch.groups.id as Snowflake); } catch { // pass } @@ -460,8 +437,8 @@ export class BushClientUtil extends ClientUtil { } /** Gets the channel configs as a TextChannel */ - public getConfigChannel(channel: 'log' | 'error' | 'dm'): Promise<TextChannel> { - return this.client.channels.fetch(this.client.config.channels[channel]) as Promise<TextChannel>; + public async getConfigChannel(channel: 'log' | 'error' | 'dm'): Promise<TextChannel> { + return (await this.client.channels.fetch(this.client.config.channels[channel])) as TextChannel; } /** @@ -488,7 +465,7 @@ export class BushClientUtil extends ClientUtil { public async insertOrRemoveFromGlobal( action: 'add' | 'remove', - key: keyof typeof BushCache, + key: keyof typeof BushCache['global'], value: any ): Promise<Global | void> { const environment = this.client.config.dev ? 'development' : 'production'; @@ -502,7 +479,34 @@ export class BushClientUtil extends ClientUtil { newValue = oldValue.filter((ae) => ae !== value); } row[key] = newValue; - this.client.cache[key] = newValue; + this.client.cache.global[key] = newValue; return await row.save().catch((e) => this.client.logger.error('insertOrRemoveFromGlobal', e)); } + + /** + * Surrounds a string to the begging an end of each element in an array. + * + * @param {string[]} array The array you want to surround. + * @param {string} surroundChar1 The character placed in the beginning of the element (or end if surroundChar2 isn't supplied). + * @param {string} [surroundChar2=surroundChar1] The character placed in the end of the element. + * @returns {string[]} + */ + public surroundArray(array: string[], surroundChar1: string, surroundChar2?: string): string[] { + const newArray = []; + array.forEach((a) => { + newArray.push(`${surroundChar1}${a}${surroundChar2 || surroundChar1}`); + }); + return newArray; + } + + // public createModLogEntry( + // user: User | Snowflake, + // guild: Guild | Snowflake, + // reason?: string, + // type?: ModLogType, + // duration?: number, + // moderator: User | Snowflake + // ): ModLog { + + // } } diff --git a/src/lib/extensions/BushCommand.ts b/src/lib/extensions/BushCommand.ts index bc6ff68..b62d26e 100644 --- a/src/lib/extensions/BushCommand.ts +++ b/src/lib/extensions/BushCommand.ts @@ -1,33 +1,47 @@ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ /* eslint-disable @typescript-eslint/no-explicit-any */ -import { Command, CommandOptions } from 'discord-akairo'; +import { ArgumentGenerator, ArgumentOptions, ArgumentPromptOptions, Command, CommandOptions } from 'discord-akairo'; import { Snowflake } from 'discord.js'; import { BushClient } from './BushClient'; import { BushCommandHandler } from './BushCommandHandler'; import { BushSlashMessage } from './BushInteractionMessage'; import { BushMessage } from './BushMessage'; +export interface BushArgumentOptions extends ArgumentOptions { + id: string; + description?: string; + prompt?: ArgumentPromptOptions; +} + export interface BushCommandOptions extends CommandOptions { hidden?: boolean; restrictedChannels?: Snowflake[]; restrictedGuilds?: Snowflake[]; description: { content: string; - usage: string; - examples: string[]; + usage: string | string[]; + examples: string | string[]; }; + args?: BushArgumentOptions[] | ArgumentGenerator; + category: string; } export class BushCommand extends Command { public declare client: BushClient; + public declare handler: BushCommandHandler; + public options: BushCommandOptions; + /** The channels the command is limited to run in. */ public restrictedChannels: Snowflake[]; + /** The guilds the command is limited to run in. */ public restrictedGuilds: Snowflake[]; + /** Whether the command is hidden from the help command. */ public hidden: boolean; + constructor(id: string, options?: BushCommandOptions) { super(id, options); this.options = options; diff --git a/src/lib/extensions/BushCommandHandler.ts b/src/lib/extensions/BushCommandHandler.ts index 8e8936e..aeea101 100644 --- a/src/lib/extensions/BushCommandHandler.ts +++ b/src/lib/extensions/BushCommandHandler.ts @@ -86,9 +86,6 @@ export class BushCommandHandler extends CommandHandler { this.emit(CommandHandlerEvents.COMMAND_BLOCKED, message, command, reason); return true; } - if (this.runCooldowns(message, command)) { - return true; - } - return false; + return !!this.runCooldowns(message, command); } } diff --git a/src/lib/extensions/BushInhibitor.ts b/src/lib/extensions/BushInhibitor.ts index 85d6de8..8a31abf 100644 --- a/src/lib/extensions/BushInhibitor.ts +++ b/src/lib/extensions/BushInhibitor.ts @@ -1,6 +1,15 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { Inhibitor } from 'discord-akairo'; import { BushClient } from './BushClient'; +import { BushCommand } from './BushCommand'; +import { BushSlashMessage } from './BushInteractionMessage'; +import { BushMessage } from './BushMessage'; export class BushInhibitor extends Inhibitor { public declare client: BushClient; + + public exec(message: BushMessage, command: BushCommand): any; + public exec(message: BushMessage | BushSlashMessage, command: BushCommand): any { + super.exec(message, command); + } } diff --git a/src/lib/extensions/BushInteractionMessage.ts b/src/lib/extensions/BushInteractionMessage.ts index ade11ea..62d2519 100644 --- a/src/lib/extensions/BushInteractionMessage.ts +++ b/src/lib/extensions/BushInteractionMessage.ts @@ -1,15 +1,16 @@ import { AkairoMessage } from 'discord-akairo'; import { CommandInteraction } from 'discord.js'; import { BushClient } from './BushClient'; +import { BushCommandUtil } from './BushCommandUtil'; export class BushSlashMessage extends AkairoMessage { + public declare client: BushClient; + public declare util: BushCommandUtil; public constructor( client: BushClient, interaction: CommandInteraction, { slash, replied }: { slash?: boolean; replied?: boolean } ) { super(client, interaction, { slash, replied }); - this.client = client; - this.interaction = interaction; } } diff --git a/src/lib/extensions/BushTaskHandler.ts b/src/lib/extensions/BushTaskHandler.ts index 923e42b..588988d 100644 --- a/src/lib/extensions/BushTaskHandler.ts +++ b/src/lib/extensions/BushTaskHandler.ts @@ -6,7 +6,6 @@ export type BushTaskHandlerOptions = AkairoHandlerOptions; export class BushTaskHandler extends TaskHandler { public constructor(client: BushClient, options: BushTaskHandlerOptions) { super(client, options); - this.client; } declare client: BushClient; } |