1 |
import BaseEvent from '../../utils/structures/BaseEvent'; |
2 |
import { GuildMember, Interaction, Message, MessageEmbed } from 'discord.js'; |
3 |
import DiscordClient from '../../client/Client'; |
4 |
import CommandOptions from '../../types/CommandOptions'; |
5 |
import InteractionOptions from '../../types/InteractionOptions'; |
6 |
import AutoCompleteOptions from '../../types/AutoCompleteOptions'; |
7 |
|
8 |
export default class InteractionCreateEvent extends BaseEvent { |
9 |
constructor() { |
10 |
super('interactionCreate'); |
11 |
} |
12 |
|
13 |
async run(client: DiscordClient, interaction: Interaction) { |
14 |
if (interaction.isCommand()) { |
15 |
await client.setMessage(interaction); |
16 |
|
17 |
const { commandName } = interaction; |
18 |
|
19 |
const command = await client.commands.get(commandName); |
20 |
|
21 |
if (command && command.supportsInteractions) { |
22 |
const allowed = await client.auth.verify(interaction.member! as GuildMember, command); |
23 |
|
24 |
if (!allowed) { |
25 |
await interaction.reply({ |
26 |
embeds: [ |
27 |
new MessageEmbed() |
28 |
.setColor('#f14a60') |
29 |
.setDescription(":x: You don't have permission to run this command.") |
30 |
], |
31 |
ephemeral: true |
32 |
}); |
33 |
|
34 |
return; |
35 |
} |
36 |
|
37 |
const options = { |
38 |
cmdName: commandName, |
39 |
options: interaction.options, |
40 |
isInteraction: true |
41 |
} as InteractionOptions; |
42 |
|
43 |
if (!await client.cooldown.start(interaction, options)) |
44 |
return; |
45 |
|
46 |
await command.run(client, interaction, options); |
47 |
} |
48 |
} |
49 |
else if (interaction.isAutocomplete()) { |
50 |
await client.setMessage(interaction); |
51 |
|
52 |
const { commandName } = interaction; |
53 |
|
54 |
const command = await client.commands.get(commandName); |
55 |
|
56 |
if (command && command.supportsInteractions) { |
57 |
const allowed = await client.auth.verify(interaction.member! as GuildMember, command); |
58 |
|
59 |
if (!allowed) { |
60 |
return; |
61 |
} |
62 |
|
63 |
const options = { |
64 |
cmdName: commandName, |
65 |
options: interaction.options, |
66 |
isInteraction: true, |
67 |
optionName: interaction.options.getFocused(true).name, |
68 |
query: interaction.options.getFocused(true).value.toString() |
69 |
} as AutoCompleteOptions; |
70 |
|
71 |
await command.autoComplete(client, interaction, options); |
72 |
} |
73 |
} |
74 |
} |
75 |
} |