aboutsummaryrefslogtreecommitdiff
path: root/core/CommandAction.ts
blob: ccc997bba83169999bc2275a53b11754d951dd4c (plain)
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
import {
    CommandInteraction,
    CommandInteractionOptionResolver,
    MessageComponentInteraction,
    ModalSubmitInteraction
} from "discord.js";

class CommandName extends String {
    constructor(...args) {
        super(...args);
    }
}

export class CommandAction {
    private readonly command: CommandName;
    private readonly args: object | CommandInteractionOptionResolver;
    private readonly interaction: CommandInteraction | ModalSubmitInteraction | MessageComponentInteraction;

    constructor(command: string, interaction: CommandInteraction | ModalSubmitInteraction | MessageComponentInteraction, args?: object) {
        this.command = new CommandName(command);
        this.interaction = interaction;
        if (args) {
            this.args = args;
        } else if (interaction instanceof CommandInteraction) {
            this.args = interaction.options;
        } else {
            this.args = {};
        }
    }

    public getCommand(): CommandName {
        return this.command;
    }

    public getInteraction(): CommandInteraction | ModalSubmitInteraction | MessageComponentInteraction {
        return this.interaction;
    }

    public getArgument(argument: string): any {
        if (this.args instanceof CommandInteractionOptionResolver) {
            return this.args.get(argument, true).value;
        } else {
            return this.args[argument];
        }
    }
}