|
| 1 | +import { Collection, Events, inlineCode, time, type InteractionReplyOptions } from 'discord.js'; |
| 2 | +import logger from '../functions/logger.js'; |
| 3 | +import notifiarrWebhook from '../functions/notifiarrWebhook.js'; |
| 4 | +import { type EventModule } from '../types.js'; |
| 5 | + |
| 6 | +const event: EventModule<Events.InteractionCreate> = { |
| 7 | + name: Events.InteractionCreate, |
| 8 | + async execute(interaction) { |
| 9 | + logger.debug(`${this.name}->${interaction.guild?.id}`); |
| 10 | + try { |
| 11 | + await notifiarrWebhook({ |
| 12 | + event: this.name, |
| 13 | + botToken: interaction.client.token, |
| 14 | + server: interaction.guild?.id, |
| 15 | + member: interaction.user.id, |
| 16 | + channel: interaction.channel?.id, |
| 17 | + customId: interaction.isMessageComponent() ? interaction.customId : undefined, |
| 18 | + }); |
| 19 | + } catch (error) { |
| 20 | + logger.error('caught:', error); |
| 21 | + } |
| 22 | + |
| 23 | + if (interaction.isAutocomplete() || interaction.isChatInputCommand()) { |
| 24 | + const slashCommand = interaction.client.slashCommands.get(interaction.commandName); |
| 25 | + |
| 26 | + if (!slashCommand) { |
| 27 | + logger.warn(`No command matching ${interaction.commandName} was found.`); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + if (interaction.isAutocomplete()) { |
| 32 | + if (!slashCommand.autocomplete) { |
| 33 | + logger.warn(`No autocomplete matching ${interaction.commandName} was found.`); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + try { |
| 38 | + await slashCommand.autocomplete(interaction); |
| 39 | + } catch (error) { |
| 40 | + logger.error('caught:', error); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if (interaction.isChatInputCommand()) { |
| 45 | + const { cooldowns } = interaction.client; |
| 46 | + |
| 47 | + if (!cooldowns.has(slashCommand.data.name)) { |
| 48 | + cooldowns.set(slashCommand.data.name, new Collection()); |
| 49 | + } |
| 50 | + |
| 51 | + const now = Date.now(); |
| 52 | + const timestamps = cooldowns.get(slashCommand.data.name) ?? new Collection(); |
| 53 | + const defaultCooldownDuration = 3; |
| 54 | + const cooldownAmount = (slashCommand.cooldown ?? defaultCooldownDuration) * 1000; |
| 55 | + |
| 56 | + if (timestamps.has(interaction.user.id)) { |
| 57 | + const expirationTime = Number(timestamps.get(interaction.user.id)) + cooldownAmount; |
| 58 | + |
| 59 | + if (now < expirationTime) { |
| 60 | + const expiredTimestamp = Math.round(expirationTime / 1000); |
| 61 | + await interaction.reply({ |
| 62 | + content: `Please wait, you are on a cooldown for ${inlineCode( |
| 63 | + slashCommand.data.name, |
| 64 | + )}. You can use it again ${time(expiredTimestamp, 'R')}.`, |
| 65 | + ephemeral: true, |
| 66 | + }); |
| 67 | + return; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + timestamps.set(interaction.user.id, now); |
| 72 | + setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount); |
| 73 | + |
| 74 | + try { |
| 75 | + await slashCommand.execute(interaction); |
| 76 | + } catch (error) { |
| 77 | + logger.error('caught:', error); |
| 78 | + const options: InteractionReplyOptions = { |
| 79 | + content: 'There was an error while executing this command!', |
| 80 | + ephemeral: true, |
| 81 | + }; |
| 82 | + await (interaction.replied || interaction.deferred |
| 83 | + ? interaction.followUp(options) |
| 84 | + : interaction.reply(options)); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (interaction.isMessageComponent()) { |
| 90 | + // Handle message components |
| 91 | + } |
| 92 | + }, |
| 93 | +}; |
| 94 | + |
| 95 | +export default event; |
0 commit comments