-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
168 lines (140 loc) · 5.98 KB
/
main.js
File metadata and controls
168 lines (140 loc) · 5.98 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
/**
* запуск бота
*/
const Discord = require('discord.js')
// const { Intents, Client } = require('discord.js')
const Intents = Discord.Intents
const client = new Discord.Client({ intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_INTEGRATIONS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_TYPING,
Intents.FLAGS.DIRECT_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGE_TYPING
], partials: ["CHANNEL"] })
const path = require('path')
const config = require(path.join(__dirname, 'config', 'main.js'))
// обьект бота, в начале launched = false (все обработчики на паузе)
const _local = {
Discord,
client,
config,
launched: false,
timeStart: new Date(),
path: __dirname,
models: {}
}
// champions, usersSettings, guildsSettings, commands
process._local = _local // для передачи данных между скриптами
_local.config.defaultPathToImg = path.join(__dirname, 'img', 'maps', 'test maps.jpg') // ????????
_local.utils = require(path.join(__dirname, 'utils', 'main.js'))
_local.classes = require(path.join(__dirname, 'classes', 'main.js'))
const {CommandsManager} = _local.classes
_local.commands = new CommandsManager() // добавляем дефолтные команды в быстрый доступ
// console.log(_local.commands)
;(async () => {
try {
const timeStart = new Date() // время старта загрузки
console.log('> Подключение к БД...')
const database = await require(path.join(__dirname, 'connections', 'mongoose.js'))
if (!database.status) throw database
// забираем сессию с БД
const session = await _local.models.session.find()
if (session.length > 1) console.log('ИМЕЕТ БОЛЕЕ 1 СОХРАНЕННОЙ СЕССИИ!!!')
_local.session = session[0] || {}
// console.log('session', _local.session)
// добавляем API хайрезов
_local.hirez = new _local.classes.API(config.devId, config.authKey)
// const test = await _local.hirez.getchampions(11)
// console.log(test)
console.log('> Загрузка данных...')
const loadData = await require(path.join(__dirname, 'loading', 'main.js'))
if (!loadData.status) throw loadData
console.log(`> Данные успешно загружены (${new Date - timeStart}ms)`)
// загружаем обработчики
const listeners = require(path.join(__dirname, 'listeners', 'main.js'))
if (!listeners) return; // если ошибка загрузки
console.log(' # Начинаю запуск клиента бота...')
client.login(config.discordToken)
} catch(err) {
console.log('try/catch')
console.log(err)
}
})();
setInterval(async () => { // очистка кэша
try {
const child_process = require('child_process')
await child_process.execSync('sudo -i')
await child_process.execSync('sync; echo 1 > /proc/sys/vm/drop_caches')
await child_process.execSync('sync; echo 2 > /proc/sys/vm/drop_caches')
await child_process.execSync('sync; echo 3 > /proc/sys/vm/drop_caches')
} catch(err) {
console.log(err)
}
}, 1000 * 60 * 60) // каждый час
function nextTimerUser(randomMinutes=1) {
setTimeout(async () => { // данные на сервере (запись в имя голосовых каналов)
const randomMinutes = 3 + Math.random() * 2
try {
const find = await _local.models.commandsStats.find()
const data = find[find.length - 1]
const chUsers = await client.channels.fetch(config.chShowUsers)
await chUsers.setName(`Users: ${data.users}`)
nextTimerUser(randomMinutes)
} catch(err) {
console.log(err)
nextTimerUser(randomMinutes)
}
}, 1000 * 60 * randomMinutes)
}
;nextTimerUser(1);
function nextTimerServers(randomMinutes=1) {
setTimeout(async () => { // данные на сервере (запись в имя голосовых каналов)
const randomMinutes = 3 + Math.random() * 2
try {
const find = await _local.models.commandsStats.find()
const data = find[find.length - 1]
const chServers = await client.channels.fetch(config.chShowServers)
await chServers.setName(`Servers: ${data.servers}`)
nextTimerServers(randomMinutes)
} catch(err) {
console.log(err)
nextTimerServers(randomMinutes)
}
}, 1000 * 60 * randomMinutes)
}
;nextTimerServers(2);
function nextTimerUsed(randomMinutes=1) {
setTimeout(async () => { // данные на сервере (запись в имя голосовых каналов)
const randomMinutes = 3 + Math.random() * 2
try {
const find = await _local.models.commandsStats.find()
const data = find[find.length - 1]
const chCommands = await client.channels.fetch(config.chShowCommands)
await chCommands.setName(`Used: ${data.usedCommands}`)
nextTimerUsed(randomMinutes)
} catch(err) {
console.log(err)
nextTimerUsed(randomMinutes)
}
}, 1000 * 60 * randomMinutes)
}
;nextTimerUsed(3);
// const memory = []
// const bytesToMb = bytes => Math.round(bytes / 1000, 2) / 1000
// const { memoryUsage } = require('process')
// setInterval(() => {
// // if (memory.length >= 10) memory.shift()
// console.clear()
// const usage = process.memoryUsage()
// const row = {
// rss: bytesToMb(usage.rss),
// heapTotal: bytesToMb(usage.heapTotal),
// heapUsed: bytesToMb(usage.heapUsed),
// external: bytesToMb(usage.external), // c++
// stack: bytesToMb(usage.rss - usage.heapTotal)
// }
// memory.push(row)
// console.table(memory)
// }, 1000)