AshuGuptaGamer

Untitled

Jul 17th, 2023
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 4.29 KB | Source Code | 0 0
  1. const { InteractionType } = require('discord.js');
  2. const Event = require('../../classes/ClientEvent');
  3. const logger = require('../../utils/logger');
  4. const fs = require('fs');
  5. const {
  6.   GLOBAL_INTERACTION_RESPOND_AGE_LIMIT,
  7. } = require('../../utils/constant');
  8. const ErrorHandler = require('../../utils/ErrorHandler');
  9.  
  10. module.exports = class extends Event {
  11.   /**
  12.    *  @type {{
  13.    * name: string;
  14.    * isResolvable: ((arg: import('../../../typings/Client').ClickInteraction) => boolean);
  15.    * exec: ((arg: import('../../../typings/Client').ClickInteraction) => void | Promise<void>);
  16.    * }[]}
  17.    */
  18.   registeredHandlers = [];
  19.  
  20.   /**
  21.    * @param {import('../../../typings/Client').HybridClient} client
  22.    */
  23.   constructor(client) {
  24.     super('interactionCreate');
  25.     this.client = client;
  26.     this._registerHandlers();
  27.   }
  28.  
  29.   _registerHandlers() {
  30.     const eventFilesOrFolders = fs
  31.       .readdirSync(
  32.         `./src/cluster/clientEvents/interactionCreateManager/handlers`
  33.       )
  34.       .map((fileName) => {
  35.         const stat = fs.statSync(
  36.           `./src/cluster/clientEvents/interactionCreateManager/handlers/${fileName}`
  37.         );
  38.         return {
  39.           name: stat.isFile()
  40.             ? fileName.endsWith('.js')
  41.               ? fileName.substring(0, fileName.length - 3)
  42.               : null
  43.             : stat.isDirectory()
  44.             ? fileName
  45.             : null,
  46.           stat: stat,
  47.         };
  48.       })
  49.       .filter((fileData) => fileData.name !== null);
  50.     eventFilesOrFolders.forEach((fileData) => {
  51.       const handler = require(`./handlers/${fileData.name}`);
  52.       handler.name = fileData.name;
  53.       this.registeredHandlers.push(handler);
  54.     });
  55.     logger.info(
  56.       `All ${
  57.         this.registeredHandlers.length
  58.       } interactionCreate handlers registered. Handlers: ${this.registeredHandlers
  59.         .map((e) => e.name)
  60.         .join(', ')}`
  61.     );
  62.   }
  63.  
  64.   /**
  65.    * @param {import('discord.js').Interaction<import('discord.js').CacheType>} interaction
  66.    */
  67.   async exec(interaction) {
  68.     this._logInteraction(interaction);
  69.     const { guild, user } = interaction;
  70.     if (interaction.isCommand() || interaction.isAutocomplete()) return;
  71.     const interactionProcessStartAt = Date.now();
  72.     if (
  73.       interaction.createdTimestamp + GLOBAL_INTERACTION_RESPOND_AGE_LIMIT <
  74.       interactionProcessStartAt
  75.     ) {
  76.       logger.debug(
  77.         `[Interaction Create Handler] Non-Command & Non-AutoComplete Interaction is too old to be handled! Created At: ${
  78.           interaction.createdTimestamp
  79.         } | Now: ${interactionProcessStartAt} | Alive: ${
  80.           interactionProcessStartAt - interaction.createdTimestamp
  81.         } | Ignoring...`,
  82.         interaction.guild
  83.       );
  84.       return;
  85.     }
  86.     if (this.registeredHandlers.length === 0) return;
  87.     const handler = this.registeredHandlers.find((h) =>
  88.       h.isResolvable(interaction)
  89.     );
  90.     if (!handler) return;
  91.     try {
  92.       await handler.exec(interaction);
  93.     } catch (error) {
  94.       const errorData = JSON.stringify(error, null, 2);
  95.       logger.error(
  96.         `[FAILED] The interactionCreate handler "${
  97.          handler?.name
  98.        }" failed. User: ${user?.tag} (${user?.id}). Interaction Type: ${
  99.           InteractionType[interaction?.type]
  100.         }. Error: ${error}. Below is the full error stack trace.`,
  101.         guild || null,
  102.         `[FAILED] The interactionCreate handler "${
  103.          handler?.name
  104.        }" failed. User: ${user?.tag} (${user?.id}). Interaction Type: ${
  105.           InteractionType[interaction?.type]
  106.         }. Error: ${error}. Below is the full error stack trace.\nError Data: \n${errorData}\nStack: \n${
  107.           error?.stack
  108.         }`
  109.       );
  110.       console.error(error);
  111.       ErrorHandler.onError(error, __filename, this.client);
  112.     }
  113.   }
  114.  
  115.   /**
  116.    * @param {import("discord.js").Interaction<import("discord.js").CacheType>} interaction
  117.    */
  118.   _logInteraction(interaction) {
  119.     logger.debug(
  120.       null,
  121.       null,
  122.       // @ts-ignore
  123.       `${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}`
  124.     );
  125.   }
  126. };
  127.  
Advertisement
Add Comment
Please, Sign In to add comment