Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { ShardingManager } = require('discord.js');
- const axios = require('axios');
- const config = require('./config.json');
- // Webhook URL
- const WEBHOOK_URL = 'https://discord.com/api/webhooks/1310921735402557461/AASLJndRyW2_SyEe7de99CPMnphMluh9mf53HBcOnsQ99AHGzaZyQuaX5Z93gxFtHhvd';
- // Hàm gửi thông báo đến webhook
- async function sendToWebhook(embed) {
- try {
- await axios.post(WEBHOOK_URL, {
- embeds: [embed],
- });
- } catch (error) {
- console.error(`[WEBHOOK ERROR] Không thể gửi log đến webhook: ${error}`);
- }
- }
- // Tự động tính toán số lượng shard
- const manager = new ShardingManager('./botmimi.js', {
- totalShards: 'auto',
- mode: 'process',
- respawn: true,
- shardArgs: [],
- execArgv: [],
- token: config.token,
- });
- // Logging khi shard được tạo
- manager.on('shardCreate', shard => {
- const embed = {
- title: `Shard ${shard.id} Đã Được Khởi Động`,
- description: `Shard ${shard.id} đã được spawn bởi worker PID: ${process.pid}`,
- color: 3066993, // Xanh lá
- timestamp: new Date().toISOString(),
- };
- sendToWebhook(embed);
- shard.on('spawn', () => {
- sendToWebhook({
- title: `Shard ${shard.id} Spawn`,
- description: `Shard ${shard.id} đã spawn thành công.`,
- color: 3447003, // Xanh dương
- timestamp: new Date().toISOString(),
- });
- });
- shard.on('disconnect', () => {
- sendToWebhook({
- title: `Shard ${shard.id} Mất Kết Nối`,
- description: `Shard ${shard.id} đã mất kết nối.`,
- color: 15158332, // Đỏ
- timestamp: new Date().toISOString(),
- });
- });
- shard.on('death', () => {
- sendToWebhook({
- title: `Shard ${shard.id} Đã Ngắt`,
- description: `Shard ${shard.id} đã bị ngắt bởi worker.`,
- color: 15158332, // Đỏ
- timestamp: new Date().toISOString(),
- });
- });
- shard.on('reconnecting', () => {
- sendToWebhook({
- title: `Shard ${shard.id} Đang Kết Nối Lại`,
- description: `Shard ${shard.id} đang thử kết nối lại.`,
- color: 15844367, // Vàng
- timestamp: new Date().toISOString(),
- });
- });
- });
- // Xử lý khi có lỗi khi spawn shard
- manager.spawn({ delay: 5000 }).catch(error => {
- sendToWebhook({
- title: `Lỗi Spawn Shard`,
- description: `Không thể spawn shard: ${error}`,
- color: 15158332, // Đỏ
- timestamp: new Date().toISOString(),
- });
- process.exit(1); // Thoát nếu không thể spawn shard
- });
- // Xử lý tín hiệu hệ thống để dừng shard an toàn
- process.on('SIGINT', () => {
- sendToWebhook({
- title: `Bot Đang Tắt`,
- description: `Nhận tín hiệu SIGINT, dừng bot...`,
- color: 15105570, // Cam
- timestamp: new Date().toISOString(),
- });
- manager.shards.forEach(shard => shard.kill());
- process.exit(0);
- });
- process.on('SIGTERM', () => {
- sendToWebhook({
- title: `Bot Đang Tắt`,
- description: `Nhận tín hiệu SIGTERM, dừng bot...`,
- color: 15105570, // Cam
- timestamp: new Date().toISOString(),
- });
- manager.shards.forEach(shard => shard.kill());
- process.exit(0);
- });
Add Comment
Please, Sign In to add comment