1 |
rakin |
51 |
import BaseEvent from '../../utils/structures/BaseEvent'; |
2 |
rakin |
344 |
import { GuildMember, Interaction, MessageEmbed } from 'discord.js'; |
3 |
rakin |
51 |
import DiscordClient from '../../client/Client'; |
4 |
|
|
import InteractionOptions from '../../types/InteractionOptions'; |
5 |
rakin |
64 |
import AutoCompleteOptions from '../../types/AutoCompleteOptions'; |
6 |
rakin |
51 |
|
7 |
|
|
export default class InteractionCreateEvent extends BaseEvent { |
8 |
|
|
constructor() { |
9 |
|
|
super('interactionCreate'); |
10 |
|
|
} |
11 |
|
|
|
12 |
|
|
async run(client: DiscordClient, interaction: Interaction) { |
13 |
rakin |
67 |
if (!interaction.guild || !interaction.channel || interaction.channel.type === 'DM') { |
14 |
|
|
if (interaction.isRepliable()) |
15 |
|
|
await interaction.reply({ |
16 |
|
|
content: 'You cannot use this bot on DMs.', |
17 |
|
|
ephemeral: true |
18 |
|
|
}); |
19 |
|
|
|
20 |
|
|
return; |
21 |
|
|
} |
22 |
rakin |
81 |
|
23 |
rakin |
125 |
if (interaction.isCommand() || interaction.isContextMenu()) { |
24 |
rakin |
51 |
await client.setMessage(interaction); |
25 |
|
|
|
26 |
|
|
const { commandName } = interaction; |
27 |
|
|
|
28 |
|
|
const command = await client.commands.get(commandName); |
29 |
|
|
|
30 |
rakin |
125 |
if (command && ((interaction.isCommand() && command.supportsInteractions) || (interaction.isContextMenu() && command.supportsContextMenu))) { |
31 |
rakin |
58 |
const allowed = await client.auth.verify(interaction.member! as GuildMember, command); |
32 |
|
|
|
33 |
rakin |
51 |
if (!allowed) { |
34 |
|
|
await interaction.reply({ |
35 |
|
|
embeds: [ |
36 |
|
|
new MessageEmbed() |
37 |
|
|
.setColor('#f14a60') |
38 |
|
|
.setDescription(":x: You don't have permission to run this command.") |
39 |
|
|
], |
40 |
|
|
ephemeral: true |
41 |
|
|
}); |
42 |
|
|
|
43 |
|
|
return; |
44 |
|
|
} |
45 |
|
|
|
46 |
rakin |
55 |
const options = { |
47 |
rakin |
51 |
cmdName: commandName, |
48 |
|
|
options: interaction.options, |
49 |
|
|
isInteraction: true |
50 |
rakin |
55 |
} as InteractionOptions; |
51 |
|
|
|
52 |
rakin |
207 |
await command.execute(client, interaction, options); |
53 |
rakin |
81 |
(global as any).lastCommand = commandName; |
54 |
rakin |
51 |
} |
55 |
|
|
} |
56 |
rakin |
64 |
else if (interaction.isAutocomplete()) { |
57 |
|
|
await client.setMessage(interaction); |
58 |
|
|
|
59 |
|
|
const { commandName } = interaction; |
60 |
|
|
|
61 |
|
|
const command = await client.commands.get(commandName); |
62 |
|
|
|
63 |
|
|
if (command && command.supportsInteractions) { |
64 |
|
|
const allowed = await client.auth.verify(interaction.member! as GuildMember, command); |
65 |
|
|
|
66 |
|
|
if (!allowed) { |
67 |
|
|
return; |
68 |
|
|
} |
69 |
|
|
|
70 |
rakin |
207 |
if (!(await command.perms(client, interaction))) { |
71 |
|
|
return; |
72 |
|
|
} |
73 |
|
|
|
74 |
rakin |
64 |
const options = { |
75 |
|
|
cmdName: commandName, |
76 |
|
|
options: interaction.options, |
77 |
|
|
isInteraction: true, |
78 |
|
|
optionName: interaction.options.getFocused(true).name, |
79 |
|
|
query: interaction.options.getFocused(true).value.toString() |
80 |
|
|
} as AutoCompleteOptions; |
81 |
|
|
|
82 |
|
|
await command.autoComplete(client, interaction, options); |
83 |
rakin |
81 |
(global as any).lastCommand = commandName; |
84 |
rakin |
64 |
} |
85 |
|
|
} |
86 |
rakin |
81 |
else { |
87 |
|
|
if (!(global as any).commandName) |
88 |
|
|
return; |
89 |
|
|
|
90 |
|
|
await client.setMessage(interaction); |
91 |
|
|
|
92 |
|
|
const command = await client.commands.get((global as any).commandName); |
93 |
|
|
|
94 |
|
|
if (command && command.supportsInteractions) { |
95 |
|
|
const allowed = await client.auth.verify(interaction.member! as GuildMember, command); |
96 |
|
|
|
97 |
|
|
if (!allowed) { |
98 |
|
|
return; |
99 |
|
|
} |
100 |
rakin |
207 |
|
101 |
|
|
if (!(await command.perms(client, interaction))) { |
102 |
|
|
return; |
103 |
|
|
} |
104 |
rakin |
81 |
|
105 |
|
|
await command.default(client, interaction); |
106 |
|
|
} |
107 |
|
|
} |
108 |
rakin |
51 |
} |
109 |
|
|
} |