Guest User

Untitled

a guest
Jan 15th, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. const { ShardingManager } = require('discord.js');
  2. const axios = require('axios');
  3. const config = require('./config.json');
  4.  
  5. // Webhook URL
  6. const WEBHOOK_URL = 'https://discord.com/api/webhooks/1310921735402557461/AASLJndRyW2_SyEe7de99CPMnphMluh9mf53HBcOnsQ99AHGzaZyQuaX5Z93gxFtHhvd';
  7.  
  8. // Hàm gửi thông báo đến webhook
  9. async function sendToWebhook(embed) {
  10. try {
  11. await axios.post(WEBHOOK_URL, {
  12. embeds: [embed],
  13. });
  14. } catch (error) {
  15. console.error(`[WEBHOOK ERROR] Không thể gửi log đến webhook: ${error}`);
  16. }
  17. }
  18.  
  19. // Tự động tính toán số lượng shard
  20. const manager = new ShardingManager('./botmimi.js', {
  21. totalShards: 'auto',
  22. mode: 'process',
  23. respawn: true,
  24. shardArgs: [],
  25. execArgv: [],
  26. token: config.token,
  27. });
  28.  
  29. // Logging khi shard được tạo
  30. manager.on('shardCreate', shard => {
  31. const embed = {
  32. title: `Shard ${shard.id} Đã Được Khởi Động`,
  33. description: `Shard ${shard.id} đã được spawn bởi worker PID: ${process.pid}`,
  34. color: 3066993, // Xanh lá
  35. timestamp: new Date().toISOString(),
  36. };
  37. sendToWebhook(embed);
  38.  
  39. shard.on('spawn', () => {
  40. sendToWebhook({
  41. title: `Shard ${shard.id} Spawn`,
  42. description: `Shard ${shard.id} đã spawn thành công.`,
  43. color: 3447003, // Xanh dương
  44. timestamp: new Date().toISOString(),
  45. });
  46. });
  47.  
  48. shard.on('disconnect', () => {
  49. sendToWebhook({
  50. title: `Shard ${shard.id} Mất Kết Nối`,
  51. description: `Shard ${shard.id} đã mất kết nối.`,
  52. color: 15158332, // Đỏ
  53. timestamp: new Date().toISOString(),
  54. });
  55. });
  56.  
  57. shard.on('death', () => {
  58. sendToWebhook({
  59. title: `Shard ${shard.id} Đã Ngắt`,
  60. description: `Shard ${shard.id} đã bị ngắt bởi worker.`,
  61. color: 15158332, // Đỏ
  62. timestamp: new Date().toISOString(),
  63. });
  64. });
  65.  
  66. shard.on('reconnecting', () => {
  67. sendToWebhook({
  68. title: `Shard ${shard.id} Đang Kết Nối Lại`,
  69. description: `Shard ${shard.id} đang thử kết nối lại.`,
  70. color: 15844367, // Vàng
  71. timestamp: new Date().toISOString(),
  72. });
  73. });
  74. });
  75.  
  76. // Xử lý khi có lỗi khi spawn shard
  77. manager.spawn({ delay: 5000 }).catch(error => {
  78. sendToWebhook({
  79. title: `Lỗi Spawn Shard`,
  80. description: `Không thể spawn shard: ${error}`,
  81. color: 15158332, // Đỏ
  82. timestamp: new Date().toISOString(),
  83. });
  84. process.exit(1); // Thoát nếu không thể spawn shard
  85. });
  86.  
  87. // Xử lý tín hiệu hệ thống để dừng shard an toàn
  88. process.on('SIGINT', () => {
  89. sendToWebhook({
  90. title: `Bot Đang Tắt`,
  91. description: `Nhận tín hiệu SIGINT, dừng bot...`,
  92. color: 15105570, // Cam
  93. timestamp: new Date().toISOString(),
  94. });
  95. manager.shards.forEach(shard => shard.kill());
  96. process.exit(0);
  97. });
  98.  
  99. process.on('SIGTERM', () => {
  100. sendToWebhook({
  101. title: `Bot Đang Tắt`,
  102. description: `Nhận tín hiệu SIGTERM, dừng bot...`,
  103. color: 15105570, // Cam
  104. timestamp: new Date().toISOString(),
  105. });
  106. manager.shards.forEach(shard => shard.kill());
  107. process.exit(0);
  108. });
  109.  
Add Comment
Please, Sign In to add comment