Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. const { SlashCommandBuilder } = require("discord.js");
  2. const axios = require("axios");
  3. const { MongoClient, ServerApiVersion } = require("mongodb");
  4. const { mongoURI, pterodactylAPIUrl, pteroAppAPIKey } = require("../../config.json");
  5. const serverConfig = require("../../serverConfig.json");
  6. const getUserIDFromEmail = require('../../handlers/getIDFromEmail');
  7.  
  8. module.exports = {
  9. data: new SlashCommandBuilder()
  10. .setName("create-server")
  11. .setDescription("Create a new server!")
  12. .addStringOption((option) =>
  13. option
  14. .setName("server-name")
  15. .setDescription("Enter the name of your server")
  16. .setRequired(true)
  17. ),
  18.  
  19. async execute(interaction) {
  20. const serverName = interaction.options.getString("server-name");
  21. const discordUserId = interaction.user.id;
  22.  
  23. try {
  24. // Get user email from database
  25. const client = new MongoClient(mongoURI, {
  26. useNewUrlParser: true,
  27. useUnifiedTopology: true,
  28. serverApi: ServerApiVersion.v1,
  29. });
  30.  
  31. try {
  32. await client.connect();
  33. console.log("Connected to MongoDB");
  34.  
  35. const db = client.db("FeminiProject");
  36. const usersCollection = db.collection("users");
  37. const user = await usersCollection.findOne({ discordUserId });
  38. const userEmail = user.email;
  39. const maxServers = user.max_servers;
  40. const currentServers = user.servers;
  41.  
  42. console.log(`User email: ${userEmail}`);
  43. console.log(`Max servers: ${maxServers}`);
  44. console.log(`Current servers: ${currentServers}`);
  45.  
  46. if (currentServers < maxServers) {
  47. // Get user ID from Pterodactyl API
  48. const userId = await getUserIDFromEmail(userEmail);
  49. if (userId) {
  50. console.log(`User ID: ${userId}`);
  51.  
  52. // Create a new server on Pterodactyl panel
  53. const pterodactylAPI = axios.create({
  54. baseURL: pterodactylAPIUrl,
  55. headers: {
  56. Authorization: `Bearer ${pteroAppAPIKey}`,
  57. "Content-Type": "application/json",
  58. },
  59. });
  60.  
  61. const createServerResponse = await pterodactylAPI.post("api/application/servers", {
  62. name: serverName,
  63. user: userId,
  64. egg: serverConfig.egg,
  65. docker_image: serverConfig.docker_image,
  66. startup: serverConfig.startup,
  67. environment: {
  68. SERVER_MEMORY: serverConfig.ram,
  69. SERVER_CPU: serverConfig.cpu,
  70. SERVER_JARFILE: "server.jar",
  71. BUILD_NUMBER: "latest",
  72. },
  73. limits: {
  74. memory: serverConfig.ram,
  75. cpu: serverConfig.cpu,
  76. io: 500,
  77. swap: 0,
  78. disk: serverConfig.disk,
  79. },
  80. allocation: {
  81. default: serverConfig.node,
  82. },
  83. feature_limits: {
  84. databases: 1,
  85. backups: 1,
  86. split_limit: 1,
  87. },
  88. deploy: serverConfig.deploy,
  89. });
  90.  
  91. console.log(`Create server response: ${createServerResponse.status} ${createServerResponse.statusText}`);
  92.  
  93. if (createServerResponse.status === 201) {
  94. // Update user's server count in database
  95. await usersCollection.updateOne({ discordUserId }, { $inc: { servers: 1 } });
  96.  
  97. await interaction.reply({
  98. content: `Server created successfully! Your server name is ${serverName}.`,
  99. ephemeral: true,
  100. });
  101. } else {
  102. await interaction.reply({ content: "Error creating server!", ephemeral: true });
  103. }
  104. } else {
  105. console.error(`User not found with email ${userEmail}`);
  106. await interaction.reply({ content: `Error: User not found with email ${userEmail}`, ephemeral: true });
  107. }
  108. } else {
  109. await interaction.reply({ content: `You have reached the maximum number of servers (${maxServers})!`, ephemeral: true });
  110. }
  111. } catch (mongoError) {
  112. console.error(`MongoDB Error: ${mongoError.message}`);
  113. console.error(`MongoDB Error Stack: ${mongoError.stack}`);
  114. await interaction.reply({ content: `Error connecting to the database! (${mongoError.message})`, ephemeral: true });
  115. } finally {
  116. await client.close();
  117. console.log("Disconnected from MongoDB");
  118. }
  119. } catch (pterodactylAPIError) {
  120. console.error(`Pterodactyl API Error: ${pterodactylAPIError.message}`);
  121. console.error(`Pterodactyl API Error Stack: ${pterodactylAPIError.stack}`);
  122. if (pterodactylAPIError.response) {
  123. console.error(`Pterodactyl API Error Status Code: ${pterodactylAPIError.response.status}`);
  124. console.error(`Pterodactyl API Error Status Text: ${pterodactylAPIError.response.statusText}`);
  125. console.error(`Pterodactyl API Error Response Data: ${pterodactylAPIError.response.data}`);
  126. }
  127. await interaction.reply({ content: `Error connecting to the Pterodactyl API! (${pterodactylAPIError.message})`, ephemeral: true });
  128. }
  129. },
  130. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement