Guest User

Untitled

a guest
Aug 22nd, 2024
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. const { SlashCommandBuilder } = require("discord.js");
  2. const { MongoClient, ServerApiVersion } = require("mongodb");
  3. const axios = require("axios");
  4. const { mongoURI, pterodactylAPIUrl, pteroAppAPIKey } = require("../../config.json");
  5.  
  6. module.exports = {
  7. data: new SlashCommandBuilder()
  8. .setName("create")
  9. .setDescription(`Create your account!`)
  10. .addStringOption((option) => // username
  11. option
  12. .setName("username")
  13. .setDescription("Please enter your username")
  14. .setRequired(true))
  15. .addStringOption((option) => // email-address
  16. option
  17. .setName("email-address")
  18. .setDescription("Please enter your email address")
  19. .setRequired(true))
  20. .addStringOption((option) => // password
  21. option
  22. .setName("password")
  23. .setDescription("Please enter your password")
  24. .setRequired(true))
  25. .addStringOption((option) => // first-name
  26. option
  27. .setName("first-name")
  28. .setDescription("Please enter your first name")
  29. .setRequired(true))
  30. .addStringOption((option) => // last-name
  31. option
  32. .setName("last-name")
  33. .setDescription("Please enter your last name")
  34. .setRequired(true)),
  35.  
  36. async execute(interaction) {
  37. const username = interaction.options.getString('username');
  38. const emailAdress = interaction.options.getString('email-address');
  39. const password = interaction.options.getString('password');
  40. const firstName = interaction.options.getString('first-name');
  41. const lastName = interaction.options.getString('last-name');
  42. const discordUserId = interaction.user.id;
  43.  
  44. try {
  45. // Create a new user on Pterodactyl panel
  46. const pterodactylAPI = axios.create({
  47. baseURL: pterodactylAPIUrl,
  48. headers: {
  49. 'Authorization': `Bearer ${pteroAppAPIKey}`,
  50. 'Content-Type': 'application/json'
  51. }
  52. });
  53.  
  54. const createUserResponse = await pterodactylAPI.post('/api/application/users', {
  55. username,
  56. email: emailAdress,
  57. password,
  58. first_name: firstName,
  59. last_name: lastName
  60. });
  61.  
  62. if (createUserResponse.status === 201) {
  63. // Store user data in MongoDB
  64. const client = new MongoClient(mongoURI, {
  65. useNewUrlParser: true,
  66. useUnifiedTopology: true,
  67. serverApi: ServerApiVersion.v1,
  68. });
  69.  
  70. try {
  71. await client.connect();
  72. const db = client.db('FeminiProject');
  73. const usersCollection = db.collection('users');
  74. await usersCollection.insertOne({
  75. discordUserId,
  76. username,
  77. email: emailAdress,
  78. password,
  79. firstName,
  80. lastName,
  81. servers: 0,
  82. max_servers: 2
  83. });
  84.  
  85. await interaction.reply({ content: "Account created successfully!", ephemeral: true });
  86. } catch (mongoError) {
  87. console.error(`MongoDB Error: ${mongoError.message}`);
  88. await interaction.reply({ content: `Error connecting to the database! (${mongoError.message})`, ephemeral: true });
  89. } finally {
  90. await client.close();
  91. }
  92. } else {
  93. await interaction.reply({ content: "Error creating user!", ephemeral: true });
  94. }
  95. } catch (pterodactylAPIError) {
  96. console.error(`Pterodactyl API Error: ${pterodactylAPIError.message}`);
  97. if (pterodactylAPIError.response) {
  98. console.error(`Pterodactyl API Error Status Code: ${pterodactylAPIError.response.status}`);
  99. console.error(`Pterodactyl API Error Status Text: ${pterodactylAPIError.response.statusText}`);
  100. }
  101. if (!pteroAppAPIKey) {
  102. await interaction.reply({ content: "Error: Pterodactyl API Key not set!", ephemeral: true });
  103. } else if (!pterodactylAPIUrl) {
  104. await interaction.reply({ content: "Error: Pterodactyl API URL not set!", ephemeral: true });
  105. } else {
  106. await interaction.reply({ content: `Error connecting to the Pterodactyl API! (${pterodactylAPIError.message})`, ephemeral: true });
  107. }
  108. }
  109. },
  110. };
Add Comment
Please, Sign In to add comment