Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { SlashCommandBuilder } = require("discord.js");
- const axios = require("axios");
- const { MongoClient, ServerApiVersion } = require("mongodb");
- const { mongoURI, pterodactylAPIUrl, pteroAppAPIKey } = require("../../config.json");
- const serverConfig = require("../../serverConfig.json");
- const getUserIDFromEmail = require('../../handlers/getIDFromEmail');
- module.exports = {
- data: new SlashCommandBuilder()
- .setName("create-server")
- .setDescription("Create a new server!")
- .addStringOption((option) =>
- option
- .setName("server-name")
- .setDescription("Enter the name of your server")
- .setRequired(true)
- ),
- async execute(interaction) {
- const serverName = interaction.options.getString("server-name");
- const discordUserId = interaction.user.id;
- try {
- // Get user email from database
- const client = new MongoClient(mongoURI, {
- useNewUrlParser: true,
- useUnifiedTopology: true,
- serverApi: ServerApiVersion.v1,
- });
- try {
- await client.connect();
- console.log("Connected to MongoDB");
- const db = client.db("FeminiProject");
- const usersCollection = db.collection("users");
- const user = await usersCollection.findOne({ discordUserId });
- const userEmail = user.email;
- const maxServers = user.max_servers;
- const currentServers = user.servers;
- console.log(`User email: ${userEmail}`);
- console.log(`Max servers: ${maxServers}`);
- console.log(`Current servers: ${currentServers}`);
- if (currentServers < maxServers) {
- // Get user ID from Pterodactyl API
- const userId = await getUserIDFromEmail(userEmail);
- if (userId) {
- console.log(`User ID: ${userId}`);
- // Create a new server on Pterodactyl panel
- const pterodactylAPI = axios.create({
- baseURL: pterodactylAPIUrl,
- headers: {
- Authorization: `Bearer ${pteroAppAPIKey}`,
- "Content-Type": "application/json",
- },
- });
- const createServerResponse = await pterodactylAPI.post("api/application/servers", {
- name: serverName,
- user: userId,
- egg: serverConfig.egg,
- docker_image: serverConfig.docker_image,
- startup: serverConfig.startup,
- environment: {
- SERVER_MEMORY: serverConfig.ram,
- SERVER_CPU: serverConfig.cpu,
- SERVER_JARFILE: "server.jar",
- BUILD_NUMBER: "latest",
- },
- limits: {
- memory: serverConfig.ram,
- cpu: serverConfig.cpu,
- io: 500,
- swap: 0,
- disk: serverConfig.disk,
- },
- allocation: {
- default: serverConfig.node,
- },
- feature_limits: {
- databases: 1,
- backups: 1,
- split_limit: 1,
- },
- deploy: serverConfig.deploy,
- });
- console.log(`Create server response: ${createServerResponse.status} ${createServerResponse.statusText}`);
- if (createServerResponse.status === 201) {
- // Update user's server count in database
- await usersCollection.updateOne({ discordUserId }, { $inc: { servers: 1 } });
- await interaction.reply({
- content: `Server created successfully! Your server name is ${serverName}.`,
- ephemeral: true,
- });
- } else {
- await interaction.reply({ content: "Error creating server!", ephemeral: true });
- }
- } else {
- console.error(`User not found with email ${userEmail}`);
- await interaction.reply({ content: `Error: User not found with email ${userEmail}`, ephemeral: true });
- }
- } else {
- await interaction.reply({ content: `You have reached the maximum number of servers (${maxServers})!`, ephemeral: true });
- }
- } catch (mongoError) {
- console.error(`MongoDB Error: ${mongoError.message}`);
- console.error(`MongoDB Error Stack: ${mongoError.stack}`);
- await interaction.reply({ content: `Error connecting to the database! (${mongoError.message})`, ephemeral: true });
- } finally {
- await client.close();
- console.log("Disconnected from MongoDB");
- }
- } catch (pterodactylAPIError) {
- console.error(`Pterodactyl API Error: ${pterodactylAPIError.message}`);
- console.error(`Pterodactyl API Error Stack: ${pterodactylAPIError.stack}`);
- if (pterodactylAPIError.response) {
- console.error(`Pterodactyl API Error Status Code: ${pterodactylAPIError.response.status}`);
- console.error(`Pterodactyl API Error Status Text: ${pterodactylAPIError.response.statusText}`);
- console.error(`Pterodactyl API Error Response Data: ${pterodactylAPIError.response.data}`);
- }
- await interaction.reply({ content: `Error connecting to the Pterodactyl API! (${pterodactylAPIError.message})`, ephemeral: true });
- }
- },
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement