-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
183 lines (160 loc) · 5.64 KB
/
index.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
const Discord = require('discord.js');
const chalk = require('chalk');
require('dotenv').config();
const { version } = require('./package.json');
const axios = require('axios');
// Check if is up to date
axios.get('https://api.github.com/repos/JTDMedia/JTDBot/releases/latest').then(res => {
if (res.data.tag_name !== version) {
console.log(chalk.red.bgYellow(`Your bot is not up to date! Please update to the latest version!`, version + ' -> ' + res.data.tag_name));
}
}).catch(err => {
console.log(chalk.red.bgYellow(`Failed to check if bot is up to date!`));
});
const webhookConfig = {
id: process.env.WEBHOOK_ID,
token: process.env.WEBHOOK_TOKEN,
};
console.clear();
console.log(chalk.blue(chalk.bold(`System`)), (chalk.white(`>>`)), (chalk.green(`Starting up...`)));
console.log(`\u001b[0m`);
console.log(chalk.red(`© JTDMedia | 2024 - ${new Date().getFullYear()}`));
console.log(chalk.red(`All rights reserved`));
console.log(`\u001b[0m`);
console.log(`\u001b[0m`);
console.log(chalk.blue(chalk.bold(`System`)), (chalk.white(`>>`)), chalk.red(`Running JTDBot version ${version}`));
console.log(`\u001b[0m`);
const manager = new Discord.ShardingManager('./bot.js', {
totalShards: 'auto',
token: process.env.DISCORD_TOKEN,
respawn: true
});
manager.on('shardCreate', shard => {
let embed = new Discord.EmbedBuilder()
.setTitle(`🆙・Launching shard`)
.setDescription(`A shard has just been launched`)
.setFields([
{
name: "🆔┆ID",
value: `${shard.id + 1}/${manager.totalShards}`,
inline: true
},
{
name: `📃┆State`,
value: `Starting up...`,
inline: true
}
])
.setColor('BLUE');
const webhook = new Discord.WebhookClient({
id: webhookConfig.id,
token: webhookConfig.token
});
webhook.send({
username: 'JTDBot - Logs',
embeds: [embed],
});
console.log(chalk.blue(chalk.bold(`System`)), (chalk.white(`>>`)), (chalk.green(`Starting`)), chalk.red(`Shard #${shard.id + 1}`), (chalk.white(`...`)));
console.log(`\u001b[0m`);
shard.on("death", (process) => {
const embed = new Discord.EmbedBuilder()
.setTitle(`🚨・Closing shard ${shard.id + 1}/${manager.totalShards} unexpectedly`)
.setFields([
{
name: "🆔┆ID",
value: `${shard.id + 1}/${manager.totalShards}`,
},
])
.setColor('RED');
webhook.send({
username: 'JTDBot - Logs',
embeds: [embed]
});
if (process.exitCode === null) {
const embed = new Discord.EmbedBuilder()
.setTitle(`🚨・Shard ${shard.id + 1}/${manager.totalShards} exited with NULL error code!`)
.setFields([
{
name: "PID",
value: `\`${process.pid}\``,
},
{
name: "Exit code",
value: `\`${process.exitCode}\``,
}
])
.setColor('RED');
webhook.send({
username: 'JTDBot - Logs',
embeds: [embed]
});
}
});
shard.on("shardDisconnect", (event) => {
const embed = new Discord.EmbedBuilder()
.setTitle(`🚨・Shard ${shard.id + 1}/${manager.totalShards} disconnected`)
.setDescription("Dumping socket close event...")
.setColor('ORANGE');
webhook.send({
username: 'JTDBot - Logs',
embeds: [embed],
});
});
shard.on("shardReconnecting", () => {
const embed = new Discord.EmbedBuilder()
.setTitle(`🚨・Reconnecting shard ${shard.id + 1}/${manager.totalShards}`)
.setColor('YELLOW');
webhook.send({
username: 'JTDBot - Logs',
embeds: [embed],
});
});
});
manager.spawn();
const webhook = new Discord.WebhookClient({
id: webhookConfig.id,
token: webhookConfig.token
});
process.on('unhandledRejection', error => {
console.error('Unhandled promise rejection:', error);
if (error) if (error.length > 950) error = error.slice(0, 950) + '... view console for details';
if (error.stack) if (error.stack.length > 950) error.stack = error.stack.slice(0, 950) + '... view console for details';
if (!error.stack) return;
const embed = new Discord.EmbedBuilder()
.setTitle(`🚨・Unhandled promise rejection`)
.addFields([
{
name: "Error",
value: error ? Discord.codeBlock(error) : "No error",
},
{
name: "Stack error",
value: error.stack ? Discord.codeBlock(error.stack) : "No stack error",
}
]);
webhook.send({
username: 'JTDBot - Logs',
embeds: [embed],
}).catch(() => {
console.log('Error sending unhandled promise rejection to webhook');
console.log(error);
});
});
process.on('warning', warn => {
console.warn("Warning:", warn);
const embed = new Discord.EmbedBuilder()
.setTitle(`🚨・New warning found`)
.addFields([
{
name: `Warn`,
value: `\`\`\`${warn}\`\`\``,
},
]);
webhook.send({
username: 'JTDBot - Logs',
embeds: [embed],
}).catch(() => {
console.log('Error sending warning to webhook');
console.log(warn);
});
});