Guest User

Untitled

a guest
Mar 1st, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.PlayerManager = void 0;
  6. const index_1 = require("../../index");
  7. class PlayerManager {
  8. _manager;
  9. cache = {};
  10. voices = {};
  11. constructor() {}
  12. init() {
  13. this._manager = index_1.Structure.manager;
  14. this._manager.emit("debug", "@Moonlink(Players) - Structure(Players) has been initialized, and assigned the value of the main class ");
  15. }
  16. handleVoiceServerUpdate(update, guildId) {
  17. this.voices[guildId] = {
  18. ...this.voices[guildId],
  19. endpoint: update.endpoint,
  20. token: update.token
  21. };
  22. this.attemptConnection(guildId);
  23. }
  24. handlePlayerDisconnect(guildId) {
  25. this._manager.emit("playerDisconnect", this.cache[guildId]);
  26. this._manager.emit("debug", `@Moonlink(PlayerManager) - a player(${guildId}) was disconnected, issuing stop and resolving information`);
  27. Object.assign(this.cache[guildId], {
  28. connected: false,
  29. voiceChannel: null,
  30. playing: false
  31. });
  32. this.cache[guildId].stop();
  33. }
  34. handlePlayerMove(newChannelId, oldChannelId, guildId) {
  35. this._manager.emit("playerMove", this.cache[guildId], newChannelId, oldChannelId);
  36. this._manager.emit("debug", `@Moonlink(PlayerManager) - a player(${guildId}) was moved channel, resolving information`);
  37. this.cache[guildId].voiceChannel = newChannelId;
  38. if (this._manager.options.resume)
  39. this.backup(this.cache[guildId]);
  40. }
  41. updateVoiceStates(guildId, update) {
  42. this.voices[guildId] = {
  43. ...this.voices[guildId],
  44. sessionId: update.session_id
  45. };
  46. }
  47. async attemptConnection(guildId) {
  48. if (!this.cache[guildId] ||
  49. !this.voices[guildId] ||
  50. (!this.voices[guildId]?.token &&
  51. !this.voices[guildId]?.endpoint &&
  52. !this.voices[guildId]?.sessionId))
  53. return false;
  54. if (this._manager.options?.balancingPlayersByRegion) {
  55. const voiceRegion = this.voices[guildId]?.endpoint?.match(/([a-zA-Z-]+)\d+/)?.[1];
  56. if (!this.cache[guildId].voiceRegion) {
  57. const connectedNodes = [
  58. ...this._manager.nodes.map.values()
  59. ].filter(node => node.state == "READY");
  60. const matchingNode = connectedNodes.find(node => node.regions.includes(voiceRegion));
  61. this.cache[guildId].voiceRegion = voiceRegion;
  62. if (matchingNode) {
  63. this.cache[guildId].node = matchingNode;
  64. }
  65. }
  66. } else if (!this.cache[guildId].voiceRegion) {
  67. const voiceRegion = this.voices[guildId]?.endpoint?.match(/([a-zA-Z-]+)\d+/)?.[1];
  68. this.cache[guildId].voiceRegion = voiceRegion;
  69. }
  70. await this.cache[guildId].node.rest.update({
  71. guildId,
  72. data: {
  73. voice: this.voices[guildId]
  74. }
  75. });
  76. return true;
  77. }
  78. has(guildId) {
  79. return !!this.cache[guildId];
  80. }
  81. get(guildId) {
  82. if (!guildId && typeof guildId !== "string")
  83. throw new Error('@Moonlink(PlayerManager) - "guildId" option in parameter to get player is empty or type is different from string');
  84. if (!this.has(guildId))
  85. return null;
  86. return this.cache[guildId];
  87. }
  88. create(data) {
  89. if (typeof data !== "object" ||
  90. !data.guildId ||
  91. typeof data.guildId !== "string" ||
  92. !data.textChannel ||
  93. typeof data.textChannel !== "string" ||
  94. !data.voiceChannel ||
  95. typeof data.voiceChannel !== "string" ||
  96. (data.autoPlay !== undefined &&
  97. typeof data.autoPlay !== "boolean") ||
  98. (data.node && typeof data.node !== "string")) {
  99. const missingParams = [];
  100. if (!data.guildId || typeof data.guildId !== "string")
  101. missingParams.push("guildId");
  102. if (!data.textChannel || typeof data.textChannel !== "string")
  103. missingParams.push("textChannel");
  104. if (!data.voiceChannel || typeof data.voiceChannel !== "string")
  105. missingParams.push("voiceChannel");
  106. if (data.autoPlay !== undefined &&
  107. typeof data.autoPlay !== "boolean")
  108. missingParams.push("autoPlay");
  109. if (data.node && typeof data.node !== "string")
  110. missingParams.push("node");
  111. throw new Error(`@Moonlink(PlayerManager) - Missing parameters for player creation: ${missingParams.join(", ")}`);
  112. }
  113. if (this.has(data.guildId))
  114. return this.get(data.guildId);
  115. let nodeSorted = this._manager.nodes.sortByUsage(`${this._manager.options.sortNode ?? "players"}`)[0];
  116. data.node = nodeSorted.identifier ?? nodeSorted.host;
  117. this._manager.emit("debug", `@Moonlink(Players) - A server player was created (${data.guildId})`);
  118. this._manager.emit("playerCreated", data.guildId);
  119. this.cache[data.guildId] = new (index_1.Structure.get("MoonlinkPlayer"))(data);
  120. return this.cache[data.guildId];
  121. }
  122. get all() {
  123. return this.cache ?? null;
  124. }
  125. backup(player) {
  126. const playerData = {};
  127. const playerKeys = Object.keys(player);
  128. playerKeys.forEach(key => {
  129. if (["guildId", "voiceChannel", "textChannel", "volume", "loop", "autoPlay", "autoLeave", "data", "previous"].includes(key)) {
  130. const value = index_1.Structure.db.get(`players.${player.guildId}.${key}`);
  131. if (player[key] !== undefined && player[key] !== value) {
  132. playerData[key] = player[key];
  133. }
  134. }
  135. });
  136. index_1.Structure.db.set(`players.${player.guildId}`,
  137. playerData);
  138. return true;
  139. }
  140. delete(guildId) {
  141. delete this.cache[guildId];
  142. index_1.Structure.db.delete(`players.${guildId}`);
  143. }
  144. }
  145. exports.PlayerManager = PlayerManager;
Advertisement
Add Comment
Please, Sign In to add comment