UNPKG

@discordx/utilities

Version:

Utilities package for enhancing discordx functionality

213 lines (199 loc) 7.11 kB
import { ButtonInteraction, ChannelSelectMenuInteraction, CommandInteraction, ContextMenuCommandInteraction, MentionableSelectMenuInteraction, ModalSubmitInteraction, RoleSelectMenuInteraction, StringSelectMenuInteraction, UserSelectMenuInteraction, Guild, User, APIUser, PermissionsString, BaseMessageOptions } from 'discord.js'; import * as discordx from 'discordx'; import { ArgsOf, SimpleCommandMessage, Client, Awaitable, GuardFunction, ClassMethodDecorator, MethodDecoratorEx, SlashChoiceType } from 'discordx'; import myDayJS from 'dayjs'; type IsGuildUserArg = ArgsOf<"messageCreate" | "messageReactionAdd" | "voiceStateUpdate"> | ButtonInteraction | ChannelSelectMenuInteraction | CommandInteraction | ContextMenuCommandInteraction | MentionableSelectMenuInteraction | ModalSubmitInteraction | RoleSelectMenuInteraction | StringSelectMenuInteraction | UserSelectMenuInteraction | SimpleCommandMessage; type IsGuardUserCallback<T extends Client = Client> = (options: { arg: IsGuildUserArg; client: T; guild: Guild | null; user: User | APIUser | null; }) => Awaitable<boolean>; /** * A multi purpose guard for user * * @param arg * @param client * @param next */ declare const IsGuildUser: <T extends Client>(callback: IsGuardUserCallback<T>) => GuardFunction<IsGuildUserArg>; /** * Guard to prevent bot from executing discordx methods */ declare const NotBot: discordx.GuardFunction<IsGuildUserArg>; type PermissionHandler = CommandInteraction | SimpleCommandMessage; type PermissionsType = PermissionsString[] | ((interaction: PermissionHandler) => Promise<PermissionsString[]>); type PermissionOptions = BaseMessageOptions & { ephemeral?: boolean; }; /** * This is useful for global commands * * @param permissions - Permissions array or a function that resolves permissions * @param options - options */ declare function PermissionGuard(permissions: PermissionsType, options?: PermissionOptions): GuardFunction<PermissionHandler>; interface RateLimitOption<T extends CommandInteraction | SimpleCommandMessage> { /** * for interaction only */ ephemeral?: boolean; /** * the message to post when a command is called when the * user is in rate limit, defaults = "message being rate limited!, please try again at {time}". * use the placeholder {time} in your string to get the time you can next call it `<t:epoch:T>` * If a function is supplied, it will pass both the interaction and how many milliseconds are left until the rate limit is over */ message?: ((interaction: T, timeLeft: number) => Awaitable<string>) | string; /** * the value to specify how many messages can be called before it is rate limited, defaults to 1 */ rateValue?: number; } declare enum TIME_UNIT { days = 0, hours = 1, minutes = 2, seconds = 3 } /** * A set like object that evicts entries from the set after they have been in there for the set time */ interface ITimedSet<T> { /** * Get the time left until this item is removed from the set */ getTimeRemaining(key: T): number; /** * checks if this set is empty */ isEmpty(): boolean; /** * Refresh the timeout for this element (resets the timer for the items eviction) * * @param key - Key */ refresh(key: T): boolean; } /** * This set will evict items from the array after the set timeout. * This set can only contain unique items, items are unique when === is true */ declare class TimedSet<T> implements ITimedSet<T> { private _timeOut; private _map; /** * @param _timeOut - Timeout in milliseconds */ constructor(_timeOut: number); get size(): number; /** * Get the raw underlying set backing this times array. * NOTE: this set is Immutable */ get rawSet(): T[]; readonly [Symbol.toStringTag] = "Set"; isEmpty(): boolean; add(key: T): this; has(value: T): boolean; delete(key: T): boolean; refresh(key: T): boolean; clear(): void; [Symbol.iterator](): IterableIterator<T>; entries(): IterableIterator<[T, T]>; forEach(callbackfn: (value: T, value2: T, set: ITimedSet<T>) => void, thisArg?: unknown): void; keys(): IterableIterator<T>; values(): IterableIterator<T>; getTimeRemaining(key: T): number; } /** * Rate limit this command, specify the time unit and the value and optionally the threshold and the message * to post when someone calls the command within the rate limit * * @param timeout - the time unit to use * @param value - the value for the time unit * @param options - rate limit options * * @constructor */ declare function RateLimit<T extends CommandInteraction | SimpleCommandMessage>(timeout: TIME_UNIT, value: number, options?: RateLimitOption<T>): GuardFunction<T>; interface ICategory { category?: string; } declare function Category(category: string): ClassMethodDecorator; declare function Description(description: string): MethodDecoratorEx; declare function EnumChoice(choices: Record<string, string>): SlashChoiceType<string, string>[]; /** * Export dayjs */ declare const dayjs: typeof myDayJS; /** * Type for time */ type Time = myDayJS.ConfigType; /** * TimeFormat * * Format time to various discord time format. */ declare const TimeFormat: { /** * 12 Hour Clock: November 28, 2018 9:01 AM * * 24 Hour Clock: 28 November 2018 09:01 */ Default: (time: Time) => string; /** * 12 Hour Clock: November 28, 2018 * * 24 Hour Clock: 28 November 2018 */ LongDate: (time: Time) => string; /** * 12 Hour Clock: Wednesday, November 28, 2018 9:01 AM * * 24 Hour Clock: Wednesday, 28 November 2018 09:01 */ LongDateTime: (time: Time) => string; /** * 12 Hour Clock: 9:01:00 AM * * 24 Hour Clock: 09:01:00 */ LongTime: (time: Time) => string; /** * The Discord relative time updates every second. * * 12 Hour Clock: 3 years ago * * 24 Hour Clock: 3 years ago */ RelativeTime: (time: Time) => string; /** * 12 Hour Clock: 11/28/2018 * * 24 Hour Clock: 28/11/2018 */ ShortDate: (time: Time) => string; /** * 12 Hour Clock: November 28, 2018 9:01 AM * * 24 Hour Clock: 28 November 2018 09:01 */ ShortDateTime: (time: Time) => string; /** * 12 Hour Clock: 9:01 AM * * 24 Hour Clock: 09:01 */ ShortTime: (time: Time) => string; /** * Unlike Discord relative time which updates every second, this remain static. * * 12 Hour Clock: 3 years ago * * 24 Hour Clock: 3 years ago */ StaticRelativeTime: (time: Time, withoutSuffix?: boolean) => string; }; export { Category, Description, EnumChoice, type ICategory, type ITimedSet, type IsGuardUserCallback, IsGuildUser, type IsGuildUserArg, NotBot, PermissionGuard, type PermissionHandler, type PermissionOptions, type PermissionsType, RateLimit, type RateLimitOption, TIME_UNIT, type Time, TimeFormat, TimedSet, dayjs };