This repository has been archived by the owner on Dec 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.ts
197 lines (160 loc) · 6.42 KB
/
commands.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
let addedCommands:any = {};
const commandHistory:any = {};
import { triggerAsyncId } from 'async_hooks';
import { client } from './index';
export interface Schema {
details: {
commandName:string;
commandDescription:string;
};
roles: {
user:[]; // Roles that can call the actual command
menu:[]; // Roles that can interact with the menu attached to the command
button:[]; // Roles that can click the buttons attached to the command
reaction:[]; // Roles that can interact with reactions attached to the command
};
parameters: Array<ParamtersSchema> // Paramters = [{ type:'', name:'', description: '', required: false }]
executesInDm:boolean; // Can the command execute in the users DM, Will use role data from the server defined in the config.serverid, leave false otherwise
interactionsInDm:boolean; // If a msg is sent to the user with attached interactables, can the user use them?
isSlashCommand:boolean; // Can this command be executed with discord slash commands?
removeInvoker:boolean; // if true, removes the message that invoked the command.
linkedToGuild:boolean; // defines if the command can be executed without configuring a static guild id in the config
isMessageCommand:boolean;
buttonInteraction:Function;
slashInteraction:Function;
menuInteraction:Function;
mainFunc:Function
};
export interface InputSchema {
parameters: any;
interaction: any;
slashCommand: boolean;
speedTest: number;
performance: any;
directMessage: boolean;
removeInvoker?: Function;
}
export interface ParamtersSchema {
type: string;
name: string;
description: string;
required?: boolean;
choices?: [{
name: string;
value: string | number;
}]
}
const OPTION_TYPES:string[] = [
'STRING',
'INTEGER',
'NUMBER',
'BOOLEAN',
'USER',
'CHANNEL',
'ROLE',
'MENTIONABLE',
'SUB_COMMAND',
'SUB_COMMAND_GROUP'
];
const commandTemplate:Schema = {
details: {
commandName: '',
commandDescription: '',
},
roles: {
user: [], // Roles that can call the actual command
menu: [], // Roles that can interact with the menu attached to the command
button: [], // Roles that can click the buttons attached to the command
reaction: [], // Roles that can interact with reactions attached to the command
},
parameters: [], // Paramters = [{ type:'', name:'', description: '', required: false }]
executesInDm: false, // Can the command execute in the users DM, Will use role data from the server defined in the config.serverid, leave false otherwise
linkedToGuild: true,
removeInvoker: true,
interactionsInDm: true, // If a msg is sent to the user with attached interactables, can the user use them?
isSlashCommand: true, // Can this command be executed with discord slash commands?
isMessageCommand: true, // Can this command be executed with a plain text message in a guild?
buttonInteraction: function(input:InputSchema){},
slashInteraction: function(input:InputSchema){},
menuInteraction: function(input:InputSchema){},
mainFunc: function(input:InputSchema){}
};
function add(command:Schema) {
// Create a new Object
let clone:any = {};
// Clone the template into the new object
Object.assign(clone,
commandTemplate)
// Assign the command paramaters to that new Object
Object.assign(clone,
command);
// Add it to the command stack
addedCommands[clone.details.commandName.toLowerCase()] = clone as Schema;
// Add it to the command history
commandHistory[clone.details.commandName.toLowerCase()] = clone as Schema;
}
function get(toFind:any = undefined, temp:any = undefined):Schema | undefined {
// If a specific command to find (toFind) is not defined, return all commands.
if(toFind === undefined) return addedCommands;
Object.keys(addedCommands).forEach((key) => {
if(toFind.toLowerCase() === key) temp = addedCommands[key];
});
return temp;
}
function remove(toRemove:string):boolean {
toRemove = toRemove.toLowerCase();
let command = get(toRemove);
if(command === undefined) return false;
delete addedCommands[toRemove];
return true;
}
function clear(){
addedCommands = {}
}
function loadSlashCommands(global:boolean) {
client.on('ready', () => {
client.guilds.cache.forEach((guild:any) => {
let commands:any = guild.commands;
Object.keys(addedCommands).forEach(command => {
if(addedCommands[command]?.isSlashCommand !== true) return;
let slashCommand:any = {
name: command,
description: addedCommands[command].details.commandDescription,
options: [],
}
addedCommands[command].parameters.forEach((parameter:ParamtersSchema) => {
if(!OPTION_TYPES.includes(parameter.type.toUpperCase())) return;
let param:any = {};
param.type = parameter.type.toUpperCase();
param.name = parameter.name;
param.description = parameter.description;
param.required = parameter?.required || false;
param.choices = parameter.choices;
slashCommand.options = [...slashCommand.options, param ];
});
commands.create(slashCommand).catch((err:any) => {
// You need to allow the bot to add slash commands to your server, most people forget about this and get confused about the error.
if(err.httpStatus == 403) console.log(`You need to give the bot oauth the "bot" oauth2 premision to use slash commands on [${guild.id} / ${guild.name}] => https://discord.com/api/oauth2/authorize?client_id=${client.user.id}&permissions=8&scope=applications.commands%20bot`);
else console.log(err);
});
})
})
})
}
module.exports = {
add: (command:Schema) => {
add(command);
},
get: (toFind:any):Schema | undefined => {
return get(toFind);
},
remove: (toRemove:string):boolean => {
return remove(toRemove);
},
clear: () => {
clear();
},
loadSlashCommands: (global:boolean = false) => {
loadSlashCommands(global);
}
}