forked from GataNina-Li/GataBot-MD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
156 lines (136 loc) Β· 4.67 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import express from 'express'
import { createServer } from 'http'
import path from 'path'
import { Socket } from 'socket.io'
import { toBuffer } from 'qrcode'
import fetch from 'node-fetch'
function connect(conn, PORT) {
let app = global.app = express()
console.log(app)
let server = global.server = createServer(app)
let _qr = 'invalid'
conn.ev.on('connection.update', function appQR({ qr }) {
if (qr) _qr = qr
})
app.use(async (req, res) => {
res.setHeader('content-type', 'image/png')
res.end(await toBuffer(_qr))
})
server.listen(PORT, () => {
console.log('App listened on port', PORT)
if (opts['keepalive']) keepAlive()
})}
function pipeEmit(event, event2, prefix = '') {
let old = event.emit
event.emit = function (event, ...args) {
old.emit(event, ...args)
event2.emit(prefix + event, ...args)
}
return {
unpipeEmit() {
event.emit = old
}}}
function keepAlive() {
const url = `https://${process.env.REPL_SLUG}.${process.env.REPL_OWNER}.repl.co`
if (/(\/\/|\.)undefined\./.test(url)) return
setInterval(() => {
fetch(url).catch(console.error)
}, 5 * 1000 * 60)}
export default connect
/*import express from 'express'
import {createServer} from 'http'
import path from 'path'
import { join, dirname } from 'path'
import {Socket} from 'socket.io'
import {toBuffer} from 'qrcode'
import fetch from 'node-fetch'
//import fs from 'fs'
import fs from 'fs/promises'
import { watchFile, unwatchFile } from "fs"
import { fileURLToPath } from "url"
function connect(conn, PORT) {
const app = global.app = express()
//console.log(app)
const server = global.server = createServer(app)
let _qr = 'QR invalido, probablemente ya hayas escaneado el QR.'
conn.ev.on('connection.update', function appQR({qr}) {
if (qr) {
_qr = qr
if (global.keepAliveRender === 1 && process.env.RENDER_EXTERNAL_URL) {
console.log(`Para obtener el cΓ³digo QR ingresa a ${process.env.RENDER_EXTERNAL_URL}/get-qr-code`);
}
}
})
app.get('/get-qr-code', async (req, res) => {
res.setHeader('content-type', 'image/png')
res.end(await toBuffer(_qr))
});
app.get('*', async (req, res) => {
res.json("GATABOT-MD en ejecuciΓ³n");
});
server.listen(PORT, async () => {
console.log('App listened on port', PORT)
if (global.keepAliveRender === 1) await keepAliveHostRender();
if (opts['keepalive']) keepAlive()
})
}
function pipeEmit(event, event2, prefix = '') {
const old = event.emit;
event.emit = function(event, ...args) {
old.emit(event, ...args)
event2.emit(prefix + event, ...args)
}
return {
unpipeEmit() {
event.emit = old
}}
}
function keepAlive() {
const url = `https://${process.env.REPL_SLUG}.${process.env.REPL_OWNER}.repl.co`
if (/(\/\/|\.)undefined\./.test(url)) return
setInterval(() => {
fetch(url).catch(console.error)
}, 5 * 1000 * 60)
}
//Kurt18: Esta funciΓ³n va impedir que Render vaya a modo suspensiΓ³n por inactividad
const keepAliveHostRender = async () => {
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
//const configPath = path.join(__dirname, 'config.js')
const configPath = join(__dirname, 'config.js')
let configContent = await fs.readFile(configPath, 'utf8')
try {
setInterval(async() => {
if (process.env.RENDER_EXTERNAL_URL) {
const urlRender = process.env.RENDER_EXTERNAL_URL;
const res = await fetch(urlRender);
if (res.status === 200) {
const result = await res.text();
console.log(`Resultado desde keepAliveHostRender() ->`, result);
}
} else {
const isInitialConfig = configContent.includes('global.obtenerQrWeb = 1;') && configContent.includes('global.keepAliveRender = 1;')
if (isInitialConfig) {
console.log(`No esta usando un Host de Render.com\nCambiando valores de "obtenerQrWeb" y "keepAliveRender" a 0 en 'config.js'`)
try {
configContent = configContent.replace('global.obtenerQrWeb = 1;', 'global.obtenerQrWeb = 0;')
configContent = configContent.replace('global.keepAliveRender = 1;', 'global.keepAliveRender = 0;')
await fs.writeFile(configPath, configContent, 'utf8')
console.log('Archivo de configuraciΓ³n actualizado con Γ©xito.')
} catch (writeError) {
console.error(`Error al escribir el archivo de 'config.js': `, writeError)
}}
}
}, 5 * 1000 * 60)
} catch (error) {
console.log(`Error manejado en server.js keepAliveHostRender() detalles: ${error}`);
}
}
export default connect
let file = fileURLToPath(import.meta.url);
watchFile(file, () => {
unwatchFile(file);
console.log(chalk.redBright("Update 'server.js'"));
import(`${file}?update=${Date.now()}`);
})
*/