MCPcopy
hub / github.com/eritislami/evobot / onInteractionCreate

Method onInteractionCreate

structs/Bot.ts:59–113  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

57 }
58
59 private async onInteractionCreate() {
60 this.client.on(Events.InteractionCreate, async (interaction: Interaction): Promise<any> => {
61 if (!interaction.isChatInputCommand()) return;
62
63 const command = this.slashCommandsMap.get(interaction.commandName);
64
65 if (!command) return;
66
67 if (!this.cooldowns.has(interaction.commandName)) {
68 this.cooldowns.set(interaction.commandName, new Collection());
69 }
70
71 const now = Date.now();
72 const timestamps = this.cooldowns.get(interaction.commandName)!;
73 const cooldownAmount = (command.cooldown || 1) * 1000;
74
75 const timestamp = timestamps.get(interaction.user.id);
76
77 if (timestamp) {
78 const expirationTime = timestamp + cooldownAmount;
79
80 if (now < expirationTime) {
81 const timeLeft = (expirationTime - now) / 1000;
82 return interaction.reply({
83 content: i18n.__mf("common.cooldownMessage", {
84 time: timeLeft.toFixed(1),
85 name: interaction.commandName
86 }),
87 ephemeral: true
88 });
89 }
90 }
91
92 timestamps.set(interaction.user.id, now);
93 setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount);
94
95 try {
96 const permissionsCheck: PermissionResult = await checkPermissions(command, interaction);
97
98 if (permissionsCheck.result) {
99 command.execute(interaction as ChatInputCommandInteraction);
100 } else {
101 throw new MissingPermissionsException(permissionsCheck.missing);
102 }
103 } catch (error: any) {
104 console.error(error);
105
106 if (error.message.includes("permissions")) {
107 interaction.reply({ content: error.toString(), ephemeral: true }).catch(console.error);
108 } else {
109 interaction.reply({ content: i18n.__("common.errorCommand"), ephemeral: true }).catch(console.error);
110 }
111 }
112 });
113 }
114}

Callers 1

constructorMethod · 0.95

Calls 3

checkPermissionsFunction · 0.90
executeMethod · 0.80
toStringMethod · 0.80

Tested by

no test coverage detected