Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { SlashCommandBuilder } = require("discord.js");
- const { MongoClient, ServerApiVersion } = require("mongodb");
- const axios = require("axios");
- const { mongoURI, pterodactylAPIUrl, pteroAppAPIKey } = require("../../config.json");
- module.exports = {
- data: new SlashCommandBuilder()
- .setName("create")
- .setDescription(`Create your account!`)
- .addStringOption((option) => // username
- option
- .setName("username")
- .setDescription("Please enter your username")
- .setRequired(true))
- .addStringOption((option) => // email-address
- option
- .setName("email-address")
- .setDescription("Please enter your email address")
- .setRequired(true))
- .addStringOption((option) => // password
- option
- .setName("password")
- .setDescription("Please enter your password")
- .setRequired(true))
- .addStringOption((option) => // first-name
- option
- .setName("first-name")
- .setDescription("Please enter your first name")
- .setRequired(true))
- .addStringOption((option) => // last-name
- option
- .setName("last-name")
- .setDescription("Please enter your last name")
- .setRequired(true)),
- async execute(interaction) {
- const username = interaction.options.getString('username');
- const emailAdress = interaction.options.getString('email-address');
- const password = interaction.options.getString('password');
- const firstName = interaction.options.getString('first-name');
- const lastName = interaction.options.getString('last-name');
- const discordUserId = interaction.user.id;
- try {
- // Create a new user on Pterodactyl panel
- const pterodactylAPI = axios.create({
- baseURL: pterodactylAPIUrl,
- headers: {
- 'Authorization': `Bearer ${pteroAppAPIKey}`,
- 'Content-Type': 'application/json'
- }
- });
- const createUserResponse = await pterodactylAPI.post('/api/application/users', {
- username,
- email: emailAdress,
- password,
- first_name: firstName,
- last_name: lastName
- });
- if (createUserResponse.status === 201) {
- // Store user data in MongoDB
- const client = new MongoClient(mongoURI, {
- useNewUrlParser: true,
- useUnifiedTopology: true,
- serverApi: ServerApiVersion.v1,
- });
- try {
- await client.connect();
- const db = client.db('FeminiProject');
- const usersCollection = db.collection('users');
- await usersCollection.insertOne({
- discordUserId,
- username,
- email: emailAdress,
- password,
- firstName,
- lastName,
- servers: 0,
- max_servers: 2
- });
- await interaction.reply({ content: "Account created successfully!", ephemeral: true });
- } catch (mongoError) {
- console.error(`MongoDB Error: ${mongoError.message}`);
- await interaction.reply({ content: `Error connecting to the database! (${mongoError.message})`, ephemeral: true });
- } finally {
- await client.close();
- }
- } else {
- await interaction.reply({ content: "Error creating user!", ephemeral: true });
- }
- } catch (pterodactylAPIError) {
- console.error(`Pterodactyl API Error: ${pterodactylAPIError.message}`);
- if (pterodactylAPIError.response) {
- console.error(`Pterodactyl API Error Status Code: ${pterodactylAPIError.response.status}`);
- console.error(`Pterodactyl API Error Status Text: ${pterodactylAPIError.response.statusText}`);
- }
- if (!pteroAppAPIKey) {
- await interaction.reply({ content: "Error: Pterodactyl API Key not set!", ephemeral: true });
- } else if (!pterodactylAPIUrl) {
- await interaction.reply({ content: "Error: Pterodactyl API URL not set!", ephemeral: true });
- } else {
- await interaction.reply({ content: `Error connecting to the Pterodactyl API! (${pterodactylAPIError.message})`, ephemeral: true });
- }
- }
- },
- };
Add Comment
Please, Sign In to add comment