diff options
Diffstat (limited to 'lib/utils')
-rw-r--r-- | lib/utils/Arg.ts | 8 | ||||
-rw-r--r-- | lib/utils/BotClientUtils.ts | 8 | ||||
-rw-r--r-- | lib/utils/Constants.ts | 220 | ||||
-rw-r--r-- | lib/utils/Format.ts | 2 | ||||
-rw-r--r-- | lib/utils/Utils.ts | 88 |
5 files changed, 201 insertions, 125 deletions
diff --git a/lib/utils/Arg.ts b/lib/utils/Arg.ts index 99060fb..80ca878 100644 --- a/lib/utils/Arg.ts +++ b/lib/utils/Arg.ts @@ -1,10 +1,4 @@ -import { - type BaseBotArgumentType, - type BotArgumentType, - type BotArgumentTypeCaster, - type CommandMessage, - type SlashMessage -} from '#lib'; +import type { BaseBotArgumentType, BotArgumentType, BotArgumentTypeCaster, CommandMessage, SlashMessage } from '#lib'; import { Argument, type Command, type Flag, type ParsedValuePredicate } from 'discord-akairo'; import { type Message } from 'discord.js'; diff --git a/lib/utils/BotClientUtils.ts b/lib/utils/BotClientUtils.ts index 1dd46bf..4b2c99b 100644 --- a/lib/utils/BotClientUtils.ts +++ b/lib/utils/BotClientUtils.ts @@ -1,8 +1,8 @@ -import { ConfigChannelKey } from '#config'; +import type { ConfigChannelKey } from '#config'; import type { CodeBlockLang, CustomInspectOptions } from '#lib'; -import { GlobalCache, SharedCache } from '#lib/common/BotCache.js'; -import { CommandMessage } from '#lib/extensions/discord-akairo/BotCommand.js'; -import { SlashMessage } from '#lib/extensions/discord-akairo/SlashMessage.js'; +import type { GlobalCache, SharedCache } from '#lib/common/BotCache.js'; +import type { CommandMessage } from '#lib/extensions/discord-akairo/BotCommand.js'; +import type { SlashMessage } from '#lib/extensions/discord-akairo/SlashMessage.js'; import { Global, Shared } from '#lib/models/index.js'; import assert from 'assert/strict'; import { diff --git a/lib/utils/Constants.ts b/lib/utils/Constants.ts index 8e4871b..c30d01d 100644 --- a/lib/utils/Constants.ts +++ b/lib/utils/Constants.ts @@ -1,10 +1,4 @@ import { default as deepLock } from 'deep-lock'; -import { - ArgumentMatches as AkairoArgumentMatches, - ArgumentTypes as AkairoArgumentTypes, - BuiltInReasons, - CommandHandlerEvents as AkairoCommandHandlerEvents -} from 'discord-akairo/dist/src/util/Constants.js'; import { Colors, GuildFeature, Snowflake } from 'discord.js'; const rawCapeUrl = 'https://raw.githubusercontent.com/NotEnoughUpdates/capes/master/'; @@ -489,39 +483,6 @@ export const bots: Record<Snowflake, { applicationId: Snowflake }> = { } }; -export const ArgumentMatches = Object.freeze({ - ...AkairoArgumentMatches -} as const); - -export const ArgumentTypes = Object.freeze({ - ...AkairoArgumentTypes, - DURATION: 'duration', - CONTENT_WITH_DURATION: 'contentWithDuration', - PERMISSION: 'permission', - SNOWFLAKE: 'snowflake', - DISCORD_EMOJI: 'discordEmoji', - ROLE_WITH_DURATION: 'roleWithDuration', - ABBREVIATED_NUMBER: 'abbreviatedNumber', - GLOBAL_USER: 'globalUser' -} as const); - -export const BlockedReasons = Object.freeze({ - ...BuiltInReasons, - DISABLED_GUILD: 'disabledGuild', - DISABLED_GLOBAL: 'disabledGlobal', - ROLE_BLACKLIST: 'roleBlacklist', - USER_GUILD_BLACKLIST: 'userGuildBlacklist', - USER_GLOBAL_BLACKLIST: 'userGlobalBlacklist', - RESTRICTED_GUILD: 'restrictedGuild', - CHANNEL_GUILD_BLACKLIST: 'channelGuildBlacklist', - CHANNEL_GLOBAL_BLACKLIST: 'channelGlobalBlacklist', - RESTRICTED_CHANNEL: 'restrictedChannel' -} as const); - -export const CommandHandlerEvents = Object.freeze({ - ...AkairoCommandHandlerEvents -} as const); - export const moulberryBushRoleMap = deepLock([ { name: '*', id: '792453550768390194' }, { name: 'Admin Perms', id: '746541309853958186' }, @@ -552,3 +513,184 @@ export const moulberryBushRoleMap = deepLock([ export type PronounCode = keyof typeof pronounMapping; export type Pronoun = typeof pronounMapping[PronounCode]; + +export const enum ArgumentMatches { + Phrase = 'phrase', + Flag = 'flag', + Option = 'option', + Rest = 'rest', + Separate = 'separate', + Text = 'text', + Content = 'content', + RestContent = 'restContent', + None = 'none' +} + +export const enum ArgumentTypes { + String = 'string', + Lowercase = 'lowercase', + Uppercase = 'uppercase', + CharCodes = 'charCodes', + Number = 'number', + Integer = 'integer', + Bigint = 'bigint', + Emojint = 'emojint', + Url = 'url', + Date = 'date', + Color = 'color', + User = 'user', + Users = 'users', + Member = 'member', + Members = 'members', + Relevant = 'relevant', + Relevants = 'relevants', + Channel = 'channel', + Channels = 'channels', + TextChannel = 'textChannel', + TextChannels = 'textChannels', + VoiceChannel = 'voiceChannel', + VoiceChannels = 'voiceChannels', + CategoryChannel = 'categoryChannel', + CategoryChannels = 'categoryChannels', + NewsChannel = 'newsChannel', + NewsChannels = 'newsChannels', + StageChannel = 'stageChannel', + StageChannels = 'stageChannels', + ThreadChannel = 'threadChannel', + ThreadChannels = 'threadChannels', + DirectoryChannel = 'directoryChannel', + DirectoryChannels = 'directoryChannels', + ForumChannel = 'forumChannel', + ForumChannels = 'forumChannels', + TextBasedChannel = 'textBasedChannel', + TextBasedChannels = 'textBasedChannels', + VoiceBasedChannel = 'voiceBasedChannel', + VoiceBasedChannels = 'voiceBasedChannels', + Role = 'role', + Roles = 'roles', + Emoji = 'emoji', + Emojis = 'emojis', + Guild = 'guild', + Guilds = 'guilds', + Message = 'message', + GuildMessage = 'guildMessage', + RelevantMessage = 'relevantMessage', + Invite = 'invite', + UserMention = 'userMention', + MemberMention = 'memberMention', + ChannelMention = 'channelMention', + RoleMention = 'roleMention', + EmojiMention = 'emojiMention', + CommandAlias = 'commandAlias', + Command = 'command', + Inhibitor = 'inhibitor', + Listener = 'listener', + Task = 'task', + ContextMenuCommand = 'contextMenuCommand', + Duration = 'duration', + contentWithDuration = 'contentWithDuration', + Permission = 'permission', + DiscordEmoji = 'discordEmoji', + RoleWithDuration = 'roleWithDuration', + AbbreviatedNumber = 'abbreviatedNumber', + GlobalUser = 'globalUser' +} + +export const enum InhibitorReason { + Client = 'client', + Bot = 'bot', + Owner = 'owner', + SuperUser = 'superUser', + Guild = 'guild', + Dm = 'dm', + AuthorNotFound = 'authorNotFound', + NotNsfw = 'notNsfw', + DisabledGuild = 'disabledGuild', + DisabledGlobal = 'disabledGlobal', + RoleBlacklist = 'roleBlacklist', + UserGuildBlacklist = 'userGuildBlacklist', + UserGlobalBlacklist = 'userGlobalBlacklist', + RestrictedGuild = 'restrictedGuild', + ChannelGuildBlacklist = 'channelGuildBlacklist', + ChannelGlobalBlacklist = 'channelGlobalBlacklist', + RestrictedChannel = 'restrictedChannel', + GuildBlacklist = 'guildBlacklist', + Fatal = 'fatal', + CannotSend = 'cannotSend', + GuildUnavailable = 'guildUnavailable' +} + +export const enum InhibitorType { + /** + * Run on all messages + */ + All = 'all', + + /** + * Run on messages not blocked by the built-in inhibitors + */ + Pre = 'pre', + + /** + * Run on messages that are commands + */ + Post = 'post' +} + +export const enum CommandHandlerEvent { + CommandBlocked = 'commandBlocked', + CommandBreakout = 'commandBreakout', + CommandCancelled = 'commandCancelled', + CommandTimeout = 'commandTimeout', + CommandFinished = 'commandFinished', + CommandInvalid = 'commandInvalid', + CommandLocked = 'commandLocked', + CommandStarted = 'commandStarted', + Cooldown = 'cooldown', + Error = 'error', + InPrompt = 'inPrompt', + MessageBlocked = 'messageBlocked', + MessageInvalid = 'messageInvalid', + MissingPermissions = 'missingPermissions', + SlashBlocked = 'slashBlocked', + SlashError = 'slashError', + SlashFinished = 'slashFinished', + SlashMissingPermissions = 'slashMissingPermissions', + SlashNotFound = 'slashNotFound', + SlashStarted = 'slashStarted', + SlashOnly = 'slashOnly' +} + +export const enum ContextCommandHandlerEvent { + Error = 'error', + Finished = 'finished', + NotFound = 'notFound', + Started = 'started', + Blocked = 'blocked' +} + +export const enum TanzaniteEvent { + Ban = 'customBan', + Block = 'customBlock', + Kick = 'customKick', + Mute = 'customMute', + PunishRoleAdd = 'punishRoleAdd', + PunishRoleRemove = 'punishRoleRemove', + Purge = 'customPurge', + RemoveTimeout = 'customRemoveTimeout', + Timeout = 'customTimeout', + Unban = 'customUnban', + Unblock = 'customUnblock', + Unmute = 'customUnmute', + UpdateModlog = 'updateModlog', + UpdateSettings = 'updateSettings', + Warn = 'customWarn', + LevelUpdate = 'levelUpdate', + Lockdown = 'lockdown', + Unlockdown = 'unlockdown', + MassBan = 'massBan', + MassEvidence = 'massEvidence', + Button = 'button', + SelectMenu = 'selectMenu', + ModalSubmit = 'modal' +} diff --git a/lib/utils/Format.ts b/lib/utils/Format.ts index debaf4b..d1c0a51 100644 --- a/lib/utils/Format.ts +++ b/lib/utils/Format.ts @@ -1,4 +1,4 @@ -import { type CodeBlockLang } from '#lib'; +import type { CodeBlockLang } from '#lib'; import { bold as discordBold, codeBlock as discordCodeBlock, diff --git a/lib/utils/Utils.ts b/lib/utils/Utils.ts index f7404e1..9280e05 100644 --- a/lib/utils/Utils.ts +++ b/lib/utils/Utils.ts @@ -1,14 +1,3 @@ -import { - Arg, - CommandMessage, - SlashEditMessageType, - SlashSendMessageType, - TanzaniteClient, - timeUnits, - type BaseBotArgumentType, - type CustomInspectOptions, - type SlashMessage -} from '#lib'; import { humanizeDuration as humanizeDurationMod } from '@notenoughupdates/humanize-duration'; import assert from 'assert/strict'; import cp from 'child_process'; @@ -21,14 +10,21 @@ import { OAuth2Scopes, PermissionFlagsBits, PermissionsBitField, + PermissionsString, type APIEmbed, type APIMessage, type CommandInteraction, - type InteractionReplyOptions, - type PermissionsString + type InteractionReplyOptions } from 'discord.js'; import { DeepWritable } from 'ts-essentials'; import { inspect as inspectUtil, promisify } from 'util'; +import { BaseBotArgumentType, CommandMessage } from '../extensions/discord-akairo/BotCommand.js'; +import { SlashMessage } from '../extensions/discord-akairo/SlashMessage.js'; +import { TanzaniteClient } from '../extensions/discord-akairo/TanzaniteClient.js'; +import { CustomInspectOptions } from '../types/InspectOptions.js'; +import { SlashEditMessageType, SlashSendMessageType } from '../types/misc.js'; +import * as Arg from './Arg.js'; +import { mappings, timeUnits } from './Constants.js'; import * as Format from './Format.js'; export type StripPrivate<T> = { [K in keyof T]: T[K] extends Record<string, any> ? StripPrivate<T[K]> : T[K] }; @@ -428,68 +424,8 @@ export function getSymbols(obj: Record<string, any>): symbol[] { return symbols; } -/** - * Checks if a user has a certain guild permission (doesn't check channel permissions). - * @param message The message to check the user from. - * @param permissions The permissions to check for. - * @returns The missing permissions or null if none are missing. - */ -export function userGuildPermCheck( - message: CommandMessage | SlashMessage, - permissions: typeof PermissionFlagsBits[keyof typeof PermissionFlagsBits][] -): PermissionsString[] | null { - if (!message.inGuild()) return null; - const missing = message.member?.permissions.missing(permissions) ?? []; - - return missing.length ? missing : null; -} - -/** - * Check if the client has certain permissions in the guild (doesn't check channel permissions). - * @param message The message to check the client user from. - * @param permissions The permissions to check for. - * @returns The missing permissions or null if none are missing. - */ -export function clientGuildPermCheck(message: CommandMessage | SlashMessage, permissions: bigint[]): PermissionsString[] | null { - const missing = message.guild?.members.me?.permissions.missing(permissions) ?? []; - - return missing.length ? missing : null; -} - -/** - * Check if the client has permission to send messages in the channel as well as check if they have other permissions - * in the guild (or the channel if `checkChannel` is `true`). - * @param message The message to check the client user from. - * @param permissions The permissions to check for. - * @param checkChannel Whether to check the channel permissions instead of the guild permissions. - * @returns The missing permissions or null if none are missing. - */ -export function clientSendAndPermCheck( - message: CommandMessage | SlashMessage, - permissions: bigint[] = [], - checkChannel = false -): PermissionsString[] | null { - if (!message.inGuild() || !message.channel) return null; - - const missing: PermissionsString[] = []; - const sendPerm = message.channel.isThread() ? 'SendMessages' : 'SendMessagesInThreads'; - - // todo: remove once forum channels are fixed - if (message.channel.parent === null && message.channel.isThread()) return null; - - if (!message.guild.members.me!.permissionsIn(message.channel!.id).has(sendPerm)) missing.push(sendPerm); - - missing.push( - ...(checkChannel - ? message.guild!.members.me!.permissionsIn(message.channel!.id!).missing(permissions) - : clientGuildPermCheck(message, permissions) ?? []) - ); - - return missing.length ? missing : null; -} - +export * as arg from './Arg.js'; export { deepLock as deepFreeze }; -export { Arg as arg }; export { Format as format }; export { DiscordConstants as discordConstants }; export { AkairoUtil as akairo }; @@ -613,3 +549,7 @@ export function formatError(error: Error | any, colors = false): string { export function deepWriteable<T>(obj: T): DeepWritable<T> { return obj as DeepWritable<T>; } + +export function formatPerms(permissions: PermissionsString[]) { + return permissions.map((p) => `\`${mappings.permissions[p]?.name ?? p}\``).join(', '); +} |