Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { InteractionType } = require('discord.js');
- const Event = require('../../classes/ClientEvent');
- const logger = require('../../utils/logger');
- const fs = require('fs');
- const {
- GLOBAL_INTERACTION_RESPOND_AGE_LIMIT,
- } = require('../../utils/constant');
- const ErrorHandler = require('../../utils/ErrorHandler');
- module.exports = class extends Event {
- /**
- * @type {{
- * name: string;
- * isResolvable: ((arg: import('../../../typings/Client').ClickInteraction) => boolean);
- * exec: ((arg: import('../../../typings/Client').ClickInteraction) => void | Promise<void>);
- * }[]}
- */
- registeredHandlers = [];
- /**
- * @param {import('../../../typings/Client').HybridClient} client
- */
- constructor(client) {
- super('interactionCreate');
- this.client = client;
- this._registerHandlers();
- }
- _registerHandlers() {
- const eventFilesOrFolders = fs
- .readdirSync(
- `./src/cluster/clientEvents/interactionCreateManager/handlers`
- )
- .map((fileName) => {
- const stat = fs.statSync(
- `./src/cluster/clientEvents/interactionCreateManager/handlers/${fileName}`
- );
- return {
- name: stat.isFile()
- ? fileName.endsWith('.js')
- ? fileName.substring(0, fileName.length - 3)
- : null
- : stat.isDirectory()
- ? fileName
- : null,
- stat: stat,
- };
- })
- .filter((fileData) => fileData.name !== null);
- eventFilesOrFolders.forEach((fileData) => {
- const handler = require(`./handlers/${fileData.name}`);
- handler.name = fileData.name;
- this.registeredHandlers.push(handler);
- });
- logger.info(
- `All ${
- this.registeredHandlers.length
- } interactionCreate handlers registered. Handlers: ${this.registeredHandlers
- .map((e) => e.name)
- .join(', ')}`
- );
- }
- /**
- * @param {import('discord.js').Interaction<import('discord.js').CacheType>} interaction
- */
- async exec(interaction) {
- this._logInteraction(interaction);
- const { guild, user } = interaction;
- if (interaction.isCommand() || interaction.isAutocomplete()) return;
- const interactionProcessStartAt = Date.now();
- if (
- interaction.createdTimestamp + GLOBAL_INTERACTION_RESPOND_AGE_LIMIT <
- interactionProcessStartAt
- ) {
- logger.debug(
- `[Interaction Create Handler] Non-Command & Non-AutoComplete Interaction is too old to be handled! Created At: ${
- interaction.createdTimestamp
- } | Now: ${interactionProcessStartAt} | Alive: ${
- interactionProcessStartAt - interaction.createdTimestamp
- } | Ignoring...`,
- interaction.guild
- );
- return;
- }
- if (this.registeredHandlers.length === 0) return;
- const handler = this.registeredHandlers.find((h) =>
- h.isResolvable(interaction)
- );
- if (!handler) return;
- try {
- await handler.exec(interaction);
- } catch (error) {
- const errorData = JSON.stringify(error, null, 2);
- logger.error(
- `[FAILED] The interactionCreate handler "${
- handler?.name
- }" failed. User: ${user?.tag} (${user?.id}). Interaction Type: ${
- InteractionType[interaction?.type]
- }. Error: ${error}. Below is the full error stack trace.`,
- guild || null,
- `[FAILED] The interactionCreate handler "${
- handler?.name
- }" failed. User: ${user?.tag} (${user?.id}). Interaction Type: ${
- InteractionType[interaction?.type]
- }. Error: ${error}. Below is the full error stack trace.\nError Data: \n${errorData}\nStack: \n${
- error?.stack
- }`
- );
- console.error(error);
- ErrorHandler.onError(error, __filename, this.client);
- }
- }
- /**
- * @param {import("discord.js").Interaction<import("discord.js").CacheType>} interaction
- */
- _logInteraction(interaction) {
- logger.debug(
- null,
- null,
- // @ts-ignore
- `${interaction.type} Interaction Submitted: ${interaction.customId} by ${interaction.user.tag} (${interaction.user.id}) in ${interaction.guild?.name} (${interaction.guild?.id}) Interaction Id: ${interaction.id} | Interaction Message: ${interaction.message?.id}`
- );
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment