-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
interactionCommandOrAutocompleteCreate.ts
93 lines (82 loc) · 3.92 KB
/
interactionCommandOrAutocompleteCreate.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
import { AutocompleteInteraction, Collection } from 'discord.js';
import { COMMAND_TYPE, INHIBITOR_TYPE, COMMAND_CHANNEL, COMMAND_PERMISSIONS, COMMAND_EVENTS } from '../constants/constants';
import { ShewenyError } from '../helpers';
import type { ShewenyClient } from '..';
import type { Inhibitor } from '../structures';
import type { CommandInteraction, ContextMenuInteraction } from 'discord.js';
export default async function run(
client: ShewenyClient,
interaction: CommandInteraction | ContextMenuInteraction | AutocompleteInteraction
) {
try {
if (!client.managers.commands) return;
/* -----------------COMMAND----------------- */
const command = client.collections.commands?.get(interaction.commandName);
//@ts-ignore
if (!command || (command && ![COMMAND_TYPE.cmdSlash, COMMAND_TYPE.ctxUser, COMMAND_TYPE.ctxMsg].includes(command.type)))
return;
if (interaction.isAutocomplete() && command.onAutocomplete) return await command.onAutocomplete(interaction);
if (command.before) await command.before(interaction as CommandInteraction | ContextMenuInteraction);
/**
* Handle inhibitors
*/
const inhibitors = client.collections.inhibitors?.filter(
(i: Inhibitor) => i.type.includes(INHIBITOR_TYPE.appCommand) || i.type.includes(INHIBITOR_TYPE.all)
);
if (inhibitors && inhibitors.size) {
const sorted = [...inhibitors.values()].sort((a, b) => b.priority - a.priority);
for (const i of sorted) {
if (!i.execute(client, interaction)) return i.onFailure(client, interaction);
}
}
/* ---------------PERMISSIONS--------------- */
if (command.adminsOnly && !client.admins?.includes(interaction.user.id))
return client.managers.commands.emit(COMMAND_EVENTS.userMissingPerm, interaction, COMMAND_PERMISSIONS.admin);
/* ---------------IN-GUILD--------------- */
if (interaction.inGuild()) {
if (command.channel === COMMAND_CHANNEL.dm) return;
if (!client.managers.commands.applicationPermissions) {
let member = interaction.guild!.members.cache.get(interaction.user.id);
if (!member) member = await interaction.guild!.members.fetch(interaction.user.id);
if (command.userPermissions.length) {
for (const permission of command.userPermissions) {
if (!member.permissions.has(permission)) {
return client.managers.commands?.emit(COMMAND_EVENTS.userMissingPerm, interaction, permission);
}
}
}
}
if (command.clientPermissions.length) {
for (const permission of command.clientPermissions) {
if (!interaction.guild!.me!.permissions.has(permission))
return client.managers.commands?.emit(COMMAND_EVENTS.clientMissingPerm, interaction, permission);
}
}
} else {
/* ---------------IN-DM--------------- */
if (command.channel === COMMAND_CHANNEL.guild) return;
}
/* ---------------COOLDOWNS--------------- */
if (!client.admins?.includes(interaction.user.id)) {
if (!command.cooldowns.has(command.name)) {
command.cooldowns.set(command.name, new Collection());
}
const timeNow = Date.now();
const tStamps = command.cooldowns.get(command.name)!;
const cdAmount = (command.cooldown || 0) * 1000;
if (tStamps.has(interaction.user.id)) {
const cdExpirationTime = (tStamps.get(interaction.user.id) || 0) + cdAmount;
if (timeNow < cdExpirationTime) {
// const timeLeft = (cdExpirationTime - timeNow) / 1000;
return client.managers.commands?.emit(COMMAND_EVENTS.cooldownLimit, interaction);
}
}
tStamps.set(interaction.user.id, timeNow);
setTimeout(() => tStamps.delete(interaction.user.id), cdAmount);
}
/* ---------------COMMAND--------------- */
await command.execute(interaction as CommandInteraction | ContextMenuInteraction);
} catch (e: any) {
new ShewenyError(client, e);
}
}