-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
264 lines (252 loc) · 7.13 KB
/
handler.js
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
const discord = require("discord.js");
const allCmd = {};
const { owners: validOwners, collab } = require("./config.json");
const contextMenuCmds = {};
/**
* @typedef ContextMenuCallbackObject
* @property {discord.ContextMenuInteraction} interaction
* @property {discord.Client} client
* @property {discord.Guild} guild
* @property {discord.GuildMember} member
* @property {discord.User} user
*/
/**
* @typedef ContextMenuOptions
* @property {discord.ApplicationCommandData | discord.ApplicationCommandSubCommandData | discord.ApplicationCommandSubGroupData} data
* @property {boolean} [owners]
* @property {boolean} [wholeCommand]
* @property {discord.PermissionString[]} [perms]
* @property {discord.PermissionString[]} [clientPermissions]
* @property {(obj: ContextMenuCallbackObject) => any} callback
*/
/**
* @typedef CallbackObject
* @property {discord.CommandInteraction} interaction
* @property {discord.Client} client
* @property {discord.Guild} guild
* @property {discord.GuildMember} member
* @property {discord.User} user
* @property {discord.CommandInteractionOptionResolver} options
*/
/**
* @typedef CommandOptions
* @property {discord.ApplicationCommandData | discord.ApplicationCommandSubCommandData | discord.ApplicationCommandSubGroupData} data
* @property {boolean} [owners]
* @property {boolean} [wholeCommand]
* @property {discord.PermissionString[]} [perms]
* @property {discord.PermissionString[]} [clientPermissions]
* @property {(obj: CallbackObject) => any} callback
*/
async function sweepCmds() {
for (const cmd in allCmd) {
await delete allCmd[cmd];
}
}
/**
*
* @param {CommandOptions} cmdOptions
* @param {string} groupName
*/
module.exports = async (cmdOptions, groupName) => {
let { data, perms = [], clientPermissions = [] } = cmdOptions;
allCmd[`${groupName.toLowerCase()}+${data.name}`] = {
...cmdOptions,
perms,
groupName,
clientPermissions,
};
};
/**
*
* @param {ContextMenuOptions} commandOptions
*/
module.exports.contextMenuRegister = async (commandOptions) => {
let { data, perms = [], clientPermissions = [] } = commandOptions;
contextMenuCmds[data.name] = {
...commandOptions,
perms,
clientPermissions,
};
};
/**
*
* @param {discord.Client} client
*/
module.exports.listen = async (client) => {
client.on("interactionCreate", async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const intMember = interaction.guild.members.cache.get(
interaction.member.user.id
);
const noGuildText = `Vui lòng sử dụng lệnh này trong một server.`;
if (!interaction.guild) {
return await interaction.reply({
content: noGuildText,
ephemeral: true,
});
}
for (const name in allCmd) {
/**
* @type {CommandOptions}
*/
const cmd = allCmd[name];
if (cmd.wholeCommand && cmd.wholeCommand === true) {
if (interaction.commandName === cmd.data.name) {
const { owners, perms, clientPermissions, callback } = cmd;
if (owners === true) {
if (
!validOwners.includes(interaction.user.id) &&
!collab.includes(interaction.user.id)
) {
return await interaction.reply({
content: `Lệnh này chỉ dành cho ${validOwners
.map((id) => {
return `${client.users.cache.get(id)}`;
})
.join(", ")}!`,
ephemeral: true,
});
}
}
for (const permission of perms) {
if (!intMember.permissions.has(permission)) {
return await interaction.reply({
content: `Bạn cần quyền \`${permission
.split("_")
.join(" ")
.toUpperCase()}\` để dùng lệnh này !`,
ephemeral: true,
});
}
}
for (const permission of clientPermissions) {
if (!interaction.guild.me.permissions.has(permission)) {
return await interaction.reply({
content: `Tớ cần quyền \`${permission
.split("_")
.join(" ")
.toUpperCase()}\` để thực hiện lệnh này !`,
});
}
}
try {
await callback({
interaction: interaction,
client: client,
guild: interaction.guild,
member: intMember,
user: interaction.user,
options: interaction.options,
});
} catch (e) {
console.log(e);
if (interaction.deferred || interaction.replied) {
await interaction.editReply({
content: `Đã có lỗi không muốn xảy ra, vui lòng thử lại sau.`,
});
} else {
await interaction.reply({
content: `Đã có lỗi không muốn xảy ra, vui lòng thử lại sau.`,
ephemeral: true,
});
}
return;
}
return;
}
}
}
});
client.on("interactionCreate", async (interaction) => {
if (!interaction.isContextMenuCommand()) return;
const { guild, member, user } = interaction;
const intMember = guild.members.cache.get(member.user.id);
const noGuildText = `Bạn vui lòng chạy lệnh này bên trong một server nhé!`;
if (!guild) {
return await interaction.reply({
content: noGuildText,
ephemeral: true,
});
}
const contextMenuCmdsArray = Object.keys(contextMenuCmds);
const commandName = contextMenuCmdsArray.find(
(commandName) => commandName === interaction.commandName
);
/**
* @type {ContextMenuOptions}
*/
const command = contextMenuCmds[commandName];
if (!command) return;
const { owners, perms, clientPermissions, callback } = command;
if (owners == true) {
if (
!validOwners.includes(user.id) &&
!collab.includes(interaction.user.id)
) {
return await interaction.reply({
content: `Lệnh này chỉ dành cho Developers !`,
});
}
}
for (const permission of perms) {
if (!intMember.permissions.has(permission)) {
return await interaction.reply({
content: `Cậu cần quyền \`${permission
.split("_")
.join(" ")
.toUpperCase()}\` để thực hiện lệnh này !`,
ephemeral: true,
});
}
}
for (const permission of clientPermissions) {
if (!guild.me.permissions.has(permission)) {
return await interaction.reply({
content: `Tớ cần quyền \`${permission
.split("_")
.join(" ")
.toUpperCase()}\` để thực hiện lệnh này !`,
});
}
}
try {
await callback({
interaction: interaction,
client: client,
guild: guild,
member: intMember,
user: user,
});
} catch (e) {
console.log(e);
if (interaction.deferred || interaction.replied) {
await interaction.editReply({
content: `Đã có lỗi xảy ra khi thực hiện lệnh, vui lòng thử lại sau.`,
});
} else {
await interaction.reply({
content: `Đã có lỗi xảy ra khi thực hiện lệnh, vui lòng thử lại sau.`,
ephemeral: true,
});
}
return;
}
return;
});
};
const getCmd = (command, groupName) => {
return allCmd[`${groupName} +${command}`];
};
module.exports.getCmd = getCmd;
module.exports.sweepCmds = sweepCmds;
async function sweepCmds() {
for (const key in allCmd) {
if (allCmd.hasOwnProperty(key)) {
delete allCmd[key];
}
}
}
const getAllCmd = () => {
return allCmd;
};
module.exports.getAllCmd = getAllCmd;