Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const bot = require('./login.js'),
- logger = require('../logger/usage.js'),
- { create_message } = require('./message.js'),
- fs = require('fs'),
- restartJson = require('./restart.json'),
- path = require('path'),
- axios = require('axios');
- function msDifference(startTime, endTime) {
- return ((endTime - startTime) / 1000).toFixed(1);
- }
- if (config.DATABASE.mongodb.CONNECT_MONGODB) {
- global.usersData = global.mongo.usersData;
- global.threadsData = global.mongo.threadsData;
- global.globalsData = global.mongo.globalsData;
- } else {
- if (config.DATABASE.sqlite.CONNECT_SQLITE) {
- global.usersData = global.sqlite.usersData;
- global.threadsData = global.sqlite.usersData;
- global.globalsData = global.sqlite.globalsData;
- } else {
- global.log('NO DATABASE SELECTED', 'red', true);
- process.exit(2);
- }
- }
- const log = global.log;
- async function restartProject() {
- await bot.clearTextListeners();
- await global.utils.sleep(3000);
- global.log('Restarting Project', 'yellow', true);
- process.exit(4);
- }
- function clearCache() {
- const tmpPath = path.resolve('script', 'commands', 'tmp');
- try {
- const files = fs.readdirSync(tmpPath);
- files.forEach((file) => {
- const filePath = path.join(tmpPath, file),
- stats = fs.statSync(filePath);
- if (stats.isFile()) {
- fs.unlinkSync(filePath);
- } else if (stats.isDirectory()) {
- const options = {
- recursive: true,
- force: true,
- };
- fs.rmSync(filePath, options);
- }
- });
- if (global.config_handler.auto_clean.log) {
- global.log(
- 'Cleared Cache: All files and directories in tmp have been removed.',
- 'magenta'
- );
- }
- } catch (error) {
- global.log('Failed to clear cache: ' + error.message, 'red');
- }
- }
- if (global.config_handler.auto_clean.toggle) {
- const interval = !isNaN(global.config_handler.auto_clean.time)
- ? global.config_handler.auto_clean.time
- : 1800000;
- setInterval(clearCache, interval);
- global.log(
- 'Cache cleaner in effect, Interval: ' +
- (interval / 1000 / 60).toFixed(0) +
- ' minutes',
- 'yellow'
- );
- }
- if (global.config.RESTART.toggle) {
- const interval = !isNaN(global.config.RESTART.time)
- ? global.config.RESTART.time
- : 3600000;
- setInterval(restartProject, interval);
- global.log(
- 'Project will restart in ' + (interval / 1000 / 60).toFixed(0) + ' mins',
- 'yellow'
- );
- }
- if (restartJson?.legit) {
- const { time_ms, chat_id, author_message } = restartJson.event,
- secondsTook = msDifference(time_ms, Date.now()),
- options = {
- reply_to_message_id: author_message,
- disable_notification: false,
- };
- bot.sendMessage(
- chat_id,
- 'Restarted. Time Taken: ' + secondsTook + 's',
- options
- );
- const newRestartJson = {
- legit: false,
- event: {},
- };
- fs.writeFileSync(
- path.join(__dirname, 'restart.json'),
- JSON.stringify(newRestartJson, null, 2)
- );
- }
- const admins = global.config_handler?.admins;
- if (admins?.length === 0) global.log('Admin not set', 'red', true);
- bot.onText(/.+/, async (message) => {
- try {
- if (!message.text || message.from.bot_id) return;
- if (!global.config?.chat?.level.includes(message.chat.type)) {
- if (global.config.chat.message) {
- await bot.sendMessage(
- message.chat.id,
- global.config.chat.message,
- { reply_to_message_id: message.message_id }
- );
- }
- return;
- }
- if (
- global.config_handler.adminOnly.toggle &&
- !admins.includes(String(message.from.id))
- ) {
- if (global.config_handler.adminOnly.toggle_message.length > 2) {
- await bot.sendMessage(
- message.chat.id,
- global.config_handler.adminOnly.toggle_message,
- { reply_to_message_id: message.message_id }
- );
- }
- return;
- }
- const msg = create_message(message),
- firstChar = message.text[0],
- threadPrefix =
- (await threadsData.retrieve(message.chat.id))?.prefix || null;
- let globalPrefix = await globalsData.retrieve('prefix');
- globalPrefix =
- globalPrefix !== 404 ? globalPrefix : global.config.BOT.prefix || '/';
- const prefix = threadPrefix || globalPrefix;
- if (firstChar !== prefix) return;
- await userCheck(message.from.id);
- const args = message.text.replace(/^\s*/, '').slice(1).split(' ');
- let [command, ...params] = args;
- if (command.includes('@')) {
- command = command.split('@')[0];
- }
- command = command.trim();
- const userExists = await usersData.exists(message.from.id);
- if (!userExists) {
- usersData.refresh(message.from.id, message);
- global.log(
- 'New User: @' +
- (message.from.username ||
- message.from.first_name ||
- message.from.last_name ||
- 'Unknown User'),
- 'yellow',
- true
- );
- const userData = { authorized: false };
- usersData.update(message.from.id, userData);
- }
- const userData = await usersData.retrieve(message.from.id);
- if (!userData.authorized && global.config_handler.authorization_prompt) {
- const replyOptions = { reply_to_message_id: message.message_id };
- await bot.sendMessage(
- message.chat.id,
- global.config_handler.authorization_prompt,
- replyOptions
- );
- return;
- }
- if (!commands[command]) return;
- const commandData = commands[command];
- if (commandData.admin && !admins.includes(String(message.from.id))) {
- if (global.config_handler.adminOnly.toggle_message.length > 2) {
- const replyOptions = { reply_to_message_id: message.message_id };
- await bot.sendMessage(
- message.chat.id,
- global.config_handler.adminOnly.toggle_message,
- replyOptions
- );
- }
- return;
- }
- await commandData.execute({
- bot,
- message,
- params,
- msg,
- global,
- });
- } catch (error) {
- logger.error(error);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement