This repository has been archived by the owner on Dec 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
237 lines (206 loc) · 8.88 KB
/
main.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
const botConfig = require(`./config.js`);
require(`dotenv`).config();
const netTools = require(`./exports/netTools.js`);
const { Player } = require(`discord-player`);
const { YoutubeiExtractor } = require(`discord-player-youtubei`);
const { Client, GatewayIntentBits, Collection } = require(`discord.js`);
const fs = require(`fs`);
const util = require(`util`);
var devnull = require(`dev-null`);
const logFileLimit = botConfig.logFileLimit;
const shouldCreateErrorLogFile = botConfig.logErrors;
const enableDebugMessages = botConfig.debugLog;
if (!fs.existsSync(`./logs`)) {
fs.mkdirSync(`./logs`);
}
const getLogfileAmount = async () => {
const files = await fs.promises.readdir(`./logs/`, (err, files) => { // eslint-disable-line no-unused-vars
if (err) throw err;
});
var filesCleared = files.filter((filename) => ((filename !== `latest.log`) && (filename !== `README.txt`) && (filename !== `logins.log`) && (filename !== `errors.log`)));
return filesCleared.length;
};
fs.unlink(`./logs/latest.log`, (err) => {
if (err) {
if (err.code !== `ENOENT`) throw err;
}
});
let nowDateString = (new Date(Date.now())).toISOString()
.replace(`T`, `_`)
.replace(`:`, `-`)
.replace(`:`, `-`);
nowDateString = nowDateString.substring(0, (nowDateString.length - 5));
var logfileStream = fs.createWriteStream(`./logs/${nowDateString}.log`, { flags: `as+` });
var latestLogfileStream = fs.createWriteStream(`./logs/latest.log`, { flags: `as+` });
var errorLogfileStream = fs.createWriteStream(`./logs/errors.log`, { flags: `as+` });
var logStdout = process.stdout;
var logStderr = process.stderr;
console.log = (...args) => {
latestLogfileStream.write(util.format.apply(null, args) + `\n`);
logfileStream.write(util.format.apply(null, args) + `\n`);
logStdout.write(util.format.apply(null, args) + `\n`);
};
console.error = (...args) => {
latestLogfileStream.write(`\n\nERROR: ${((new Date(Date.now())).toUTCString())
.replace(`GMT`, `UTC+0000 (Coordinated Universal Time)`)}\n` + util.format.apply(null, args) + `\n\n`);
logfileStream.write(`\n\nERROR: ${((new Date(Date.now())).toUTCString())
.replace(`GMT`, `UTC+0000 (Coordinated Universal Time)`)}\n` + util.format.apply(null, args) + `\n\n`);
errorLogfileStream.write(`\n\nERROR: ${((new Date(Date.now())).toUTCString())
.replace(`GMT`, `UTC+0000 (Coordinated Universal Time)`)}\n` + util.format.apply(null, args) + `\n\n`);
logStderr.write(`\n\nERROR: ${((new Date(Date.now())).toUTCString())
.replace(`GMT`, `UTC`)}\n` + util.format.apply(null, args) + `\n\n`);
};
process.on(`uncaughtException`, (err) => {
console.error((err && err.stack) ? err.stack : err);
setTimeout(() => {
process.kill(process.pid);
}, 10000);
});
const maybeUpdateLogFile = async () => {
const logfilesInFolder = await getLogfileAmount();
const shouldCreateLogFile = (logFileLimit === -1) ? true : (logfilesInFolder < logFileLimit) ? true : (logfilesInFolder >= logFileLimit) ? false : true;
if (!shouldCreateLogFile) {
logfileStream = devnull();
fs.unlink(`./logs/${nowDateString}.log`, (err) => {
if (err) {
if (err.code !== `ENOENT`) throw err;
}
});
}
};
maybeUpdateLogFile();
const maybeUpdateErrorLogFile = async () => {
if (!shouldCreateErrorLogFile) {
logfileStream = devnull();
}
};
maybeUpdateErrorLogFile();
let client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildVoiceStates
],
disableMentions: `everyone`
});
client.player = new Player(client, botConfig.opt.discordPlayer, {volume: botConfig.opt.initialVolume, volumeSmoothness: botConfig.opt.volumeSmoothness});
const player = client.player;
player.extractors.loadDefault();
player.extractors.register(YoutubeiExtractor, {});
const synchronizeSlashCommands = require(`discord-sync-commands-v14`);
const { ApplicationCommandType } = require(`discord.js`);
console.log(`-> Loading commands...`);
client.commands = new Collection();
fs.readdir(`./commands/`, (_err, files) => {
files.forEach((file) => {
if (!file.endsWith(`.js`)) return;
let props = require(`./commands/${file}`);
let commandName = file.split(`.`)[0];
client.commands.set(commandName, {
name: commandName,
...props
});
console.log(`${commandName} Command loaded`);
});
synchronizeSlashCommands(client, client.commands.map((c) => ({
name: c.name,
description: c.description,
options: c.options,
type: ApplicationCommandType.ChatInput
})), {
debug: false
});
});
console.log(`-> Loading message components...`);
client.components = new Collection();
fs.readdir(`./components/`, (_err, files) => {
files.forEach((file) => {
if (!file.endsWith(`.js`)) return;
let props = require(`./components/${file}`);
let componentName = file.split(`.`)[0];
client.components.set(componentName, {
name: componentName,
...props
});
console.log(`${componentName} Component loaded`);
});
});
console.log(`-> Loading events...`);
fs.readdir(`./events`, (_err, files) => {
files.forEach((file) => {
if (!file.endsWith(`.js`)) return;
const event = require(`./events/${file}`);
let eventName = file.split(`.`)[0];
console.log(`${eventName} Event loaded`);
client.on(eventName, event.bind(null, client));
delete require.cache[require.resolve(`./events/${file}`)];
});
});
player.events.on(`error`, (queue, error) => {
queue.metadata.channel.send({ content: `⚠️ There was a problem with the song queue! => Error: **${error.message}**` }).catch((e) => { });
const timestamp = ((new Date(Date.now())).toUTCString())
.replace(`GMT`, `UTC+0000 (Coordinated Universal Time)`);
console.log(`ERROR: ⚠️ There was a problem with the song queue! => Error: "${error.message}" (time: ${timestamp})`);
if (error.code === `ABORT_ERR`) {
console.log(`===> Note to future self: Turning off the VPN (as well as just trying again) has helped fixing "The operation was aborted".`);
}
throw error;
});
player.events.on(`playerError`, (queue, error) => {
queue.metadata.channel.send({ content: `⚠️ There was a problem with the connection! => Error: **${error.message}**` }).catch((e) => { });
const timestamp = ((new Date(Date.now())).toUTCString())
.replace(`GMT`, `UTC+0000 (Coordinated Universal Time)`);
console.log(`ERROR: ⚠️ There was a problem with the connection! => Error: "${error.message}" (time: ${timestamp})`);
});
player.events.on(`playerStart`, (queue, track) => {
if (!botConfig.opt.loopMessage && queue.repeatMode !== 0) return;
queue.metadata.channel.send({ content: `🎵 Music started playing: **${track.title}** -> Channel: <#${queue.dispatcher.channel.id}> 🎧` }).catch((e) => { });
});
player.events.on(`audioTrackAdd`, (queue, track) => {
if (!track.dontSendAddedMessage) queue.metadata.channel.send({ content: `**${track.title}** has been added to playlist. ✅` }).catch((e) => { });
});
player.events.on(`emptyChannel`, (queue) => {
queue.metadata.channel.send({ content: `I left the audio channel because there was no one on my channel. ❌` }).catch((e) => { });
});
player.events.on(`emptyQueue`, (queue) => {
if (botConfig.opt.voiceConfig.leaveOnTimer.status === true) {
setTimeout(() => {
if (queue.connection) queue.connection.disconnect();
}, botConfig.opt.voiceConfig.leaveOnTimer.time);
}
queue.metadata.channel.send({ content: `All tracks in queue are finished. ✅` }).catch((e) => { });
});
if (enableDebugMessages) {
// generate dependencies report
console.log(player.scanDeps());
// ^------ This is similar to discord-voip's `generateDependenciesReport()` function, but with additional informations related to discord-player
// log metadata query, search execution, etc.
player.on(`debug`, console.log);
// ^------ This shows how your search query is interpreted, if the query was cached, which extractor resolved the query or which extractor failed to resolve, etc.
// log debug logs of the queue, such as voice connection logs, player execution, streaming process etc.
player.events.on(`debug`, (queue, message) => console.log(`[DEBUG ${queue.guild.id}] ${message}`));
// ^------ This shows how queue handles the track. It logs informations like the status of audio player, streaming process, configurations used, if streaming failed or not, etc.
}
const express = require(`express`);
const app = express();
const http = require(`http`);
const AppIp = (`http://127.0.0.1:` + (netTools.getPort()) + `/`);
app.get(`/`, (request, response) => {
response.sendStatus(200);
});
app.listen(netTools.getPort());
setInterval(() => {
http.get(AppIp);
}, 60000);
const logIPs = async () => {
console.log(`App running!\nPublic IP: ${await netTools.getPublicIp()}\nPrivate IP: ${await netTools.getPrivateIp()}:${netTools.getPort()}`);
};
logIPs();
const botToken = process.env[`TOKEN`];
if (botToken) {
client.login(botToken).catch((e) => {
console.log(`The Bot Token you entered into your bot's .env-file is incorrect or your bot's INTENTS are OFF!`);
});
} else {
console.log(`Please write your bot token into the TOKEN-field in the .env-file of your bot!`);
}