Advertisement
samiroexpikachu

Handler

Sep 2nd, 2024 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2. const bot = require('./login.js'),
  3.   logger = require('../logger/usage.js'),
  4.   { create_message } = require('./message.js'),
  5.   fs = require('fs'),
  6.   restartJson = require('./restart.json'),
  7.   path = require('path'),
  8.   axios = require('axios');
  9.  
  10. function msDifference(startTime, endTime) {
  11.   return ((endTime - startTime) / 1000).toFixed(1);
  12. }
  13.  
  14. if (config.DATABASE.mongodb.CONNECT_MONGODB) {
  15.   global.usersData = global.mongo.usersData;
  16.   global.threadsData = global.mongo.threadsData;
  17.   global.globalsData = global.mongo.globalsData;
  18. } else {
  19.   if (config.DATABASE.sqlite.CONNECT_SQLITE) {
  20.     global.usersData = global.sqlite.usersData;
  21.     global.threadsData = global.sqlite.usersData;
  22.     global.globalsData = global.sqlite.globalsData;
  23.   } else {
  24.     global.log('NO DATABASE SELECTED', 'red', true);
  25.     process.exit(2);
  26.   }
  27. }
  28.  
  29. const log = global.log;
  30.  
  31. async function restartProject() {
  32.   await bot.clearTextListeners();
  33.   await global.utils.sleep(3000);
  34.   global.log('Restarting Project', 'yellow', true);
  35.   process.exit(4);
  36. }
  37.  
  38. function clearCache() {
  39.   const tmpPath = path.resolve('script', 'commands', 'tmp');
  40.   try {
  41.     const files = fs.readdirSync(tmpPath);
  42.     files.forEach((file) => {
  43.       const filePath = path.join(tmpPath, file),
  44.         stats = fs.statSync(filePath);
  45.       if (stats.isFile()) {
  46.         fs.unlinkSync(filePath);
  47.       } else if (stats.isDirectory()) {
  48.         const options = {
  49.           recursive: true,
  50.           force: true,
  51.         };
  52.         fs.rmSync(filePath, options);
  53.       }
  54.     });
  55.     if (global.config_handler.auto_clean.log) {
  56.       global.log(
  57.         'Cleared Cache: All files and directories in tmp have been removed.',
  58.         'magenta'
  59.       );
  60.     }
  61.   } catch (error) {
  62.     global.log('Failed to clear cache: ' + error.message, 'red');
  63.   }
  64. }
  65.  
  66. if (global.config_handler.auto_clean.toggle) {
  67.   const interval = !isNaN(global.config_handler.auto_clean.time)
  68.     ? global.config_handler.auto_clean.time
  69.     : 1800000;
  70.   setInterval(clearCache, interval);
  71.   global.log(
  72.     'Cache cleaner in effect, Interval: ' +
  73.       (interval / 1000 / 60).toFixed(0) +
  74.       ' minutes',
  75.     'yellow'
  76.   );
  77. }
  78.  
  79. if (global.config.RESTART.toggle) {
  80.   const interval = !isNaN(global.config.RESTART.time)
  81.     ? global.config.RESTART.time
  82.     : 3600000;
  83.   setInterval(restartProject, interval);
  84.   global.log(
  85.     'Project will restart in ' + (interval / 1000 / 60).toFixed(0) + ' mins',
  86.     'yellow'
  87.   );
  88. }
  89.  
  90. if (restartJson?.legit) {
  91.   const { time_ms, chat_id, author_message } = restartJson.event,
  92.     secondsTook = msDifference(time_ms, Date.now()),
  93.     options = {
  94.       reply_to_message_id: author_message,
  95.       disable_notification: false,
  96.     };
  97.   bot.sendMessage(
  98.     chat_id,
  99.     'Restarted. Time Taken: ' + secondsTook + 's',
  100.     options
  101.   );
  102.   const newRestartJson = {
  103.     legit: false,
  104.     event: {},
  105.   };
  106.   fs.writeFileSync(
  107.     path.join(__dirname, 'restart.json'),
  108.     JSON.stringify(newRestartJson, null, 2)
  109.   );
  110. }
  111.  
  112. const admins = global.config_handler?.admins;
  113. if (admins?.length === 0) global.log('Admin not set', 'red', true);
  114.  
  115. bot.onText(/.+/, async (message) => {
  116.   try {
  117.     if (!message.text || message.from.bot_id) return;
  118.  
  119.     if (!global.config?.chat?.level.includes(message.chat.type)) {
  120.       if (global.config.chat.message) {
  121.         await bot.sendMessage(
  122.           message.chat.id,
  123.           global.config.chat.message,
  124.           { reply_to_message_id: message.message_id }
  125.         );
  126.       }
  127.       return;
  128.     }
  129.  
  130.     if (
  131.       global.config_handler.adminOnly.toggle &&
  132.       !admins.includes(String(message.from.id))
  133.     ) {
  134.       if (global.config_handler.adminOnly.toggle_message.length > 2) {
  135.         await bot.sendMessage(
  136.           message.chat.id,
  137.           global.config_handler.adminOnly.toggle_message,
  138.           { reply_to_message_id: message.message_id }
  139.         );
  140.       }
  141.       return;
  142.     }
  143.  
  144.     const msg = create_message(message),
  145.       firstChar = message.text[0],
  146.       threadPrefix =
  147.         (await threadsData.retrieve(message.chat.id))?.prefix || null;
  148.     let globalPrefix = await globalsData.retrieve('prefix');
  149.     globalPrefix =
  150.       globalPrefix !== 404 ? globalPrefix : global.config.BOT.prefix || '/';
  151.     const prefix = threadPrefix || globalPrefix;
  152.  
  153.     if (firstChar !== prefix) return;
  154.  
  155.     await userCheck(message.from.id);
  156.  
  157.     const args = message.text.replace(/^\s*/, '').slice(1).split(' ');
  158.     let [command, ...params] = args;
  159.  
  160.     if (command.includes('@')) {
  161.       command = command.split('@')[0];
  162.     }
  163.     command = command.trim();
  164.  
  165.     const userExists = await usersData.exists(message.from.id);
  166.     if (!userExists) {
  167.       usersData.refresh(message.from.id, message);
  168.       global.log(
  169.         'New User: @' +
  170.           (message.from.username ||
  171.             message.from.first_name ||
  172.             message.from.last_name ||
  173.             'Unknown User'),
  174.         'yellow',
  175.         true
  176.       );
  177.       const userData = { authorized: false };
  178.       usersData.update(message.from.id, userData);
  179.     }
  180.  
  181.     const userData = await usersData.retrieve(message.from.id);
  182.     if (!userData.authorized && global.config_handler.authorization_prompt) {
  183.       const replyOptions = { reply_to_message_id: message.message_id };
  184.       await bot.sendMessage(
  185.         message.chat.id,
  186.         global.config_handler.authorization_prompt,
  187.         replyOptions
  188.       );
  189.       return;
  190.     }
  191.  
  192.     if (!commands[command]) return;
  193.  
  194.     const commandData = commands[command];
  195.     if (commandData.admin && !admins.includes(String(message.from.id))) {
  196.       if (global.config_handler.adminOnly.toggle_message.length > 2) {
  197.         const replyOptions = { reply_to_message_id: message.message_id };
  198.         await bot.sendMessage(
  199.           message.chat.id,
  200.           global.config_handler.adminOnly.toggle_message,
  201.           replyOptions
  202.         );
  203.       }
  204.       return;
  205.     }
  206.  
  207.     await commandData.execute({
  208.       bot,
  209.       message,
  210.       params,
  211.       msg,
  212.       global,
  213.     });
  214.   } catch (error) {
  215.     logger.error(error);
  216.   }
  217. });
  218.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement