/[sudobot]/trunk/src/events/interaction/InteractionCreateEvent.ts
ViewVC logotype

Annotation of /trunk/src/events/interaction/InteractionCreateEvent.ts

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26