|
| 1 | +import { getCommandKit, getContext } from '../context/async-context'; |
| 2 | +import { eventWorkerContext } from '../app/events/EventWorkerContext'; |
| 3 | +import { ParsedEvent } from '../app/router'; |
| 4 | +import { CommandKit } from '../CommandKit'; |
| 5 | +import { |
| 6 | + AutocompleteInteraction, |
| 7 | + ChatInputCommandInteraction, |
| 8 | + Client, |
| 9 | + ClientEvents, |
| 10 | + ContextMenuCommandInteraction, |
| 11 | + Events, |
| 12 | + Guild, |
| 13 | + Message, |
| 14 | + TextBasedChannel, |
| 15 | +} from 'discord.js'; |
| 16 | +import { LoadedCommand } from '../app'; |
| 17 | +import { FlagStore } from './store'; |
| 18 | + |
| 19 | +export type MaybePromise<T> = T | Promise<T>; |
| 20 | + |
| 21 | +export type IdentifyFunction<R> = ( |
| 22 | + context: EvaluationContext, |
| 23 | +) => MaybePromise<R>; |
| 24 | + |
| 25 | +export type DecideFunction<E, R> = (data: { entities: E }) => MaybePromise<R>; |
| 26 | + |
| 27 | +export interface FeatureFlagDefinition<R, Entity> { |
| 28 | + key: string; |
| 29 | + description?: string; |
| 30 | + identify?: IdentifyFunction<Entity>; |
| 31 | + decide: DecideFunction<Entity, R>; |
| 32 | +} |
| 33 | + |
| 34 | +export interface CommandFlagContext { |
| 35 | + client: Client<true>; |
| 36 | + commandkit: CommandKit; |
| 37 | + command: { |
| 38 | + interaction?: |
| 39 | + | ChatInputCommandInteraction |
| 40 | + | AutocompleteInteraction |
| 41 | + | ContextMenuCommandInteraction; |
| 42 | + message?: Message; |
| 43 | + guild: Guild | null; |
| 44 | + channel: TextBasedChannel | null; |
| 45 | + command: LoadedCommand; |
| 46 | + }; |
| 47 | + event: null; |
| 48 | +} |
| 49 | + |
| 50 | +export interface EventFlagContext { |
| 51 | + client: Client<true>; |
| 52 | + commandkit: CommandKit; |
| 53 | + event: { |
| 54 | + data: ParsedEvent; |
| 55 | + event: string; |
| 56 | + namespace: string | null; |
| 57 | + arguments: any[]; |
| 58 | + argumentsAs<E extends keyof ClientEvents>(event: E): ClientEvents[E]; |
| 59 | + }; |
| 60 | + command: null; |
| 61 | +} |
| 62 | + |
| 63 | +export type EvaluationContext = CommandFlagContext | EventFlagContext; |
| 64 | + |
| 65 | +export type CustomEvaluationFunction<E> = () => MaybePromise<E>; |
| 66 | + |
| 67 | +export type CustomEvaluationContext<E> = { |
| 68 | + identify: E | CustomEvaluationFunction<E>; |
| 69 | +}; |
| 70 | + |
| 71 | +export interface FlagRunner<E, R> { |
| 72 | + (): Promise<R>; |
| 73 | + run(context: CustomEvaluationContext<E>): Promise<R>; |
| 74 | +} |
| 75 | + |
| 76 | +export class FeatureFlag<R, T> { |
| 77 | + public constructor(private options: FeatureFlagDefinition<R, T>) { |
| 78 | + const FlagStore = getCommandKit(true).flags; |
| 79 | + |
| 80 | + if (FlagStore.has(options.key)) { |
| 81 | + throw new Error(`Feature flag with key "${options.key}" already exists.`); |
| 82 | + } |
| 83 | + |
| 84 | + FlagStore.set(options.key, this); |
| 85 | + } |
| 86 | + |
| 87 | + private getContext(): EvaluationContext { |
| 88 | + const env = getContext(); |
| 89 | + |
| 90 | + if (env?.context) { |
| 91 | + const { |
| 92 | + client, |
| 93 | + commandkit, |
| 94 | + interaction, |
| 95 | + message, |
| 96 | + guild, |
| 97 | + channel, |
| 98 | + command, |
| 99 | + } = env.context; |
| 100 | + |
| 101 | + return { |
| 102 | + client: client as Client<true>, |
| 103 | + commandkit, |
| 104 | + command: { |
| 105 | + interaction, |
| 106 | + message, |
| 107 | + guild, |
| 108 | + channel, |
| 109 | + command, |
| 110 | + }, |
| 111 | + event: null, |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + const eventCtx = eventWorkerContext.getStore(); |
| 116 | + |
| 117 | + if (eventCtx) { |
| 118 | + const { commandkit, data, event, namespace } = eventCtx; |
| 119 | + |
| 120 | + return { |
| 121 | + client: commandkit.client as Client<true>, |
| 122 | + commandkit, |
| 123 | + event: { |
| 124 | + data, |
| 125 | + event, |
| 126 | + namespace, |
| 127 | + arguments: eventCtx.arguments, |
| 128 | + argumentsAs: (eventName) => { |
| 129 | + const args = eventCtx.arguments as ClientEvents[typeof eventName]; |
| 130 | + return args; |
| 131 | + }, |
| 132 | + }, |
| 133 | + command: null, |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + throw new Error( |
| 138 | + 'Could not determine the execution context. Feature flags may only be used inside a command or event.', |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + public async execute(res?: T): Promise<R> { |
| 143 | + const { decide, identify } = this.options; |
| 144 | + |
| 145 | + const entities = |
| 146 | + res ?? |
| 147 | + (await (async () => { |
| 148 | + const ctx = this.getContext(); |
| 149 | + return (await identify?.(ctx)) ?? ({} as T); |
| 150 | + })()); |
| 151 | + |
| 152 | + const decisionResult = await decide({ |
| 153 | + entities, |
| 154 | + }); |
| 155 | + |
| 156 | + return decisionResult as R; |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +export function flag<Returns = boolean, Entity = Record<any, any>>( |
| 161 | + options: FeatureFlagDefinition<Returns, Entity>, |
| 162 | +): FlagRunner<Entity, Returns> { |
| 163 | + const flag = new FeatureFlag<Returns, Entity>(options); |
| 164 | + const runner = flag.execute.bind(flag, undefined) as FlagRunner< |
| 165 | + Entity, |
| 166 | + Returns |
| 167 | + >; |
| 168 | + |
| 169 | + runner.run = async function (ctx) { |
| 170 | + if (!ctx?.identify) { |
| 171 | + throw new Error( |
| 172 | + 'Custom evaluation context must have an identify function or object.', |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + const context = ( |
| 177 | + typeof ctx === 'function' |
| 178 | + ? await (ctx as CustomEvaluationFunction<Entity>)() |
| 179 | + : ctx |
| 180 | + ) as Entity; |
| 181 | + |
| 182 | + const decisionResult = await flag.execute(context); |
| 183 | + |
| 184 | + return decisionResult; |
| 185 | + }; |
| 186 | + |
| 187 | + return runner; |
| 188 | +} |
0 commit comments