index.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. console.log('Iniciando 🚀🚀🚀')
  2. import { join, dirname } from 'path'
  3. import { createRequire } from 'module'
  4. import { fileURLToPath } from 'url'
  5. import { setupMaster, fork } from 'cluster'
  6. import { watchFile, unwatchFile } from 'fs'
  7. import cfonts from 'cfonts'
  8. import { createInterface } from 'readline'
  9. import yargs from 'yargs'
  10. const __dirname = dirname(fileURLToPath(import.meta.url))
  11. const require = createRequire(__dirname)
  12. const { name, author } = require(join(__dirname, './package.json'))
  13. const { say } = cfonts
  14. const rl = createInterface(process.stdin, process.stdout)
  15. say('KantuBot', {
  16. font: 'chrome',
  17. align: 'center',
  18. gradient: ['red', 'magenta']})
  19. say(`by: CrxsMods`, {
  20. font: 'console',
  21. align: 'center',
  22. gradient: ['red', 'magenta']});
  23. let isRunning = false
  24. /**
  25. * Start a js file
  26. * @param {String} file `path/to/file`
  27. */
  28. function start(file) {
  29. if (isRunning) return
  30. isRunning = true
  31. const args = [join(__dirname, file), ...process.argv.slice(2)]
  32. setupMaster({
  33. exec: args[0],
  34. args: args.slice(1)})
  35. const p = fork()
  36. p.on('message', (data) => {
  37. //console.log('╭--------- - - - ✓\n┆ ✅ TIEMPO DE ACTIVIDAD ACTUALIZADA\n╰-------------------- - - -', data)
  38. switch (data) {
  39. case 'reset':
  40. p.process.kill()
  41. isRunning = false
  42. start.apply(this, arguments)
  43. break
  44. case 'uptime':
  45. p.send(process.uptime())
  46. break
  47. }
  48. })
  49. p.on('exit', (_, code) => {
  50. isRunning = false;
  51. console.error('⚠️ Error Inesperado ⚠️', code)
  52. p.process.kill()
  53. isRunning = false
  54. start.apply(this, arguments)
  55. if (process.env.pm_id) {
  56. process.exit(1)
  57. } else {
  58. process.exit()
  59. }})
  60. const opts = new Object(yargs(process.argv.slice(2)).exitProcess(false).parse())
  61. if (!opts['test']) {
  62. if (!rl.listenerCount()) {
  63. rl.on('line', (line) => {
  64. p.emit('message', line.trim())
  65. })
  66. }}}
  67. setInterval(() => {
  68. console.log('♻️ Reiniciando bot automáticamente...');
  69. process.exit(0);
  70. }, 14400000) //4hs
  71. start('main.js');