This repository has been archived by the owner on Dec 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputManager.ts
63 lines (60 loc) · 2.11 KB
/
InputManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Interaction, Message } from "discord.js";
import { FunctionCommand } from "../commands/base/FunctionCommand.js";
import { ParameterType, InputParameterValue, InputParameter, TargetID } from "./Parameter.js";
/**
* Object that stores all interaction input data (target, arguments, content and the interaction itself)
* @class
*/
export class InputManager {
/**
* Command related to this manager
* @type {FunctionCommand}
* @public
* @readonly
*/
public readonly command: FunctionCommand;
/**
* Command interaction or message
* @type {Interaction | Message}
* @public
* @readonly
*/
public readonly interaction: Interaction | Message;
/**
* Command target (when using context menu interactions)
* @type {?TargetID<any>}
* @public
* @readonly
*/
public readonly target?: TargetID<any>;
/**
* All input arguments
* @type {Array<InputParameter<any>>}
* @private
* @readonly
*/
private readonly arguments: InputParameter<any>[];
/**
* @constructor
* @param {FunctionCommand} command - command related to this manager
* @param {Interaction | Message} interaction - interaction or message
* @param {Array<InputManager<any>>} args - list of input arguments
* @param {?TargetID<any>} [target] - interaction target (when using context menu interactions)
*/
constructor(command: FunctionCommand, interaction: Interaction | Message, args: InputParameter<any>[], target?: TargetID<any>) {
this.command = command;
this.interaction = interaction;
this.arguments = args;
this.target = target;
}
/**
* Get input values
* @param {string} query - parameter name
* @param {T} type - parameter type
* @returns {ParameterResolvable} Argument value bound to a parameter
* @public
*/
public get<T extends ParameterType>(query: string, type: T): InputParameterValue<T> | null {
return (this.arguments.filter((arg) => arg.type === type).find((arg) => arg.name === query)?.value as InputParameterValue<T>) ?? null;
}
}