Skip to content
This repository was archived by the owner on Oct 13, 2022. It is now read-only.

Commit d445432

Browse files
committed
Add main classes (builder)
1 parent 445ea13 commit d445432

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed

src/main.ts

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import { isUndefined } from 'lodash';
2+
3+
import {
4+
ApplicationCommandJSON,
5+
ApplicationCommandOptionStructure,
6+
ApplicationCommandOptionTypes,
7+
ApplicationCommandTypes,
8+
CommonPropertiesAndMethods,
9+
} from '@src/interfaces';
10+
11+
import SlashCommandMethods from '@common/SlashCommandMethods';
12+
13+
/*
14+
|--------------------------------------------------------------------------
15+
| SlashCommandSubcommand
16+
|--------------------------------------------------------------------------
17+
|
18+
| ...
19+
|
20+
*/
21+
22+
export class SlashCommandSubcommand extends SlashCommandMethods implements CommonPropertiesAndMethods {
23+
public readonly type: ApplicationCommandOptionTypes = ApplicationCommandOptionTypes.Subcommand;
24+
public readonly name: string = '';
25+
public readonly description: string = '';
26+
27+
public setName(value: string): this {
28+
Reflect.set(this, 'name', value);
29+
30+
return this;
31+
}
32+
33+
public setDescription(value: string): this {
34+
Reflect.set(this, 'description', value);
35+
36+
return this;
37+
}
38+
39+
public toJSON(): ApplicationCommandOptionStructure {
40+
if (isUndefined(this.type)) {
41+
throw new Error('The `type` parameter should not be empty');
42+
}
43+
44+
const response: ApplicationCommandOptionStructure = {
45+
type: this.type,
46+
name: this.name,
47+
description: this.description,
48+
options: this.options,
49+
};
50+
51+
return response;
52+
}
53+
}
54+
55+
/*
56+
|--------------------------------------------------------------------------
57+
| SlashCommandSubcommandGroup
58+
|--------------------------------------------------------------------------
59+
|
60+
| ...
61+
|
62+
*/
63+
64+
export class SlashCommandSubcommandGroup implements CommonPropertiesAndMethods {
65+
public readonly type: ApplicationCommandOptionTypes = ApplicationCommandOptionTypes.SubcommandGroup;
66+
public readonly name: string = '';
67+
public readonly description: string = '';
68+
public readonly options: Array<ApplicationCommandOptionStructure> = [];
69+
70+
public setName(value: string): this {
71+
Reflect.set(this, 'name', value);
72+
73+
return this;
74+
}
75+
76+
public setDescription(value: string): this {
77+
Reflect.set(this, 'description', value);
78+
79+
return this;
80+
}
81+
82+
public addSubcommand(
83+
callback: (builder: SlashCommandSubcommand) => SlashCommandSubcommand
84+
): this {
85+
this.options.push(callback(new SlashCommandSubcommand()).toJSON());
86+
87+
return this;
88+
}
89+
90+
public toJSON(): ApplicationCommandOptionStructure {
91+
if (isUndefined(this.type)) {
92+
throw new Error('The `type` parameter should not be empty');
93+
}
94+
95+
const response: ApplicationCommandOptionStructure = {
96+
type: this.type,
97+
name: this.name,
98+
description: this.description,
99+
options: this.options,
100+
};
101+
102+
return response;
103+
}
104+
}
105+
106+
/*
107+
|--------------------------------------------------------------------------
108+
| SlashCommandBuilder
109+
|--------------------------------------------------------------------------
110+
|
111+
| ...
112+
|
113+
*/
114+
115+
export class SlashCommandBuilder extends SlashCommandMethods implements CommonPropertiesAndMethods {
116+
public readonly type: ApplicationCommandTypes = ApplicationCommandTypes.ChatInput;
117+
public readonly name: string = '';
118+
public readonly description: string = '';
119+
public readonly defaultPermission: boolean = true;
120+
121+
public setType(value: ApplicationCommandTypes): this {
122+
Reflect.set(this, 'type', value);
123+
124+
return this;
125+
}
126+
127+
public setName(value: string): this {
128+
Reflect.set(this, 'name', value);
129+
130+
return this;
131+
}
132+
133+
public setDescription(value: string): this {
134+
Reflect.set(this, 'description', value);
135+
136+
return this;
137+
}
138+
139+
public setDefaultPermission(value: boolean): this {
140+
Reflect.set(this, 'defaultPermission', value);
141+
142+
return this;
143+
}
144+
145+
public addSubcommand(
146+
callback: (builder: SlashCommandSubcommand) => SlashCommandSubcommand
147+
): this {
148+
this.options.push(callback(new SlashCommandSubcommand()).toJSON());
149+
150+
return this;
151+
}
152+
153+
public addSubcommandGroup(
154+
callback: (builder: SlashCommandSubcommandGroup) => SlashCommandSubcommandGroup
155+
): this {
156+
this.options.push(callback(new SlashCommandSubcommandGroup()).toJSON());
157+
158+
return this;
159+
}
160+
161+
public toJSON(): ApplicationCommandJSON {
162+
return {
163+
type: this.type,
164+
name: this.name,
165+
description: this.description,
166+
options: this.options,
167+
default_permission: this.defaultPermission,
168+
};
169+
}
170+
}

0 commit comments

Comments
 (0)