A Discord Bot connected to your Pterodactyl API.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: run lint fix

authored by

cosmeak and committed by
Cosmeak
23e49970 cadc6444

+200 -200
+22 -21
src/commands/ClientServerList.js
··· 3 3 require("dotenv").config(); 4 4 5 5 module.exports = { 6 - data: new SlashCommandBuilder() 7 - .setName("client-server-list") 8 - .setDescription("List all servers"), 9 - async execute(interaction) { 10 - const response = await axios.get(`${process.env.PTERO_HOST}/api/client/`, { 11 - "headers": { 12 - "Accept": "application/json", 13 - "Authorization": `Bearer ${process.env.PTERO_TOKEN}`, 14 - } 15 - }); 6 + data: new SlashCommandBuilder() 7 + .setName("client-server-list") 8 + .setDescription("List all servers"), 9 + async execute(interaction) { 10 + const response = await axios.get(`${process.env.PTERO_HOST}/api/client/`, { 11 + "headers": { 12 + "Accept": "application/json", 13 + "Authorization": `Bearer ${process.env.PTERO_TOKEN}`, 14 + }, 15 + }); 16 16 17 - let message = ""; 18 - response.data.data.forEach((server) => { 19 - message += server.attributes.name + ' [' + server.attributes.identifier + '] \n' 20 - }) 17 + let message = ""; 18 + response.data.data.forEach((server) => { 19 + message += server.attributes.name + " [" + server.attributes.identifier + "] \n"; 20 + }); 21 21 22 - const embed = new EmbedBuilder() 23 - .setTitle("All available servers") 24 - .setDescription(message) 25 - .setColor("DarkNavy"); 22 + const embed = new EmbedBuilder() 23 + .setTitle("All available servers") 24 + .setDescription(message) 25 + .setColor("Blurple") 26 + .setTitle(); 26 27 27 - return interaction.reply({ embeds: [embed] }) 28 - } 29 - } 28 + return interaction.reply({ embeds: [embed] }); 29 + }, 30 + };
+41 -40
src/commands/ClientServerPower.js
··· 4 4 require("dotenv").config(); 5 5 6 6 module.exports = { 7 - data: new SlashCommandBuilder() 8 - .setName("client-server-power") 9 - .setDescription("Provide a way to up or down a server") 10 - .addStringOption((option) => 11 - option.setName("server-id") 12 - .setDescription("Your server external_ID") 13 - .setRequired(true) 14 - ) 15 - .addStringOption((option) => 16 - option.setName("state") 17 - .setDescription("Choose a state for your server") 18 - .setChoices( 19 - { name: "start", value: "start" }, 20 - { name: "stop", value: "stop" }, 21 - { name: "restart", value: "restart" }, 22 - { name: "kill", value: "kill" }, 23 - ) 24 - .setRequired(true) 25 - ), 26 - async execute(interaction) { 27 - const state = interaction.options.get("state").value; 28 - const id = interaction.options.get("server-id").value; 29 - const server = await fecthClientServerInfo(id); 7 + data: new SlashCommandBuilder() 8 + .setName("client-server-power") 9 + .setDescription("Provide a way to up or down a server") 10 + .addStringOption((option) => 11 + option.setName("server-id") 12 + .setDescription("Your server external_ID") 13 + .setRequired(true), 14 + ) 15 + .addStringOption((option) => 16 + option.setName("state") 17 + .setDescription("Choose a state for your server") 18 + .setChoices( 19 + { name: "start", value: "start" }, 20 + { name: "stop", value: "stop" }, 21 + { name: "restart", value: "restart" }, 22 + { name: "kill", value: "kill" }, 23 + ) 24 + .setRequired(true), 25 + ), 26 + async execute(interaction) { 27 + const state = interaction.options.get("state").value; 28 + const id = interaction.options.get("server-id").value; 29 + const server = await fecthClientServerInfo(id); 30 30 31 - try { 32 - await axios.post(`${process.env.PTERO_HOST}/api/client/servers/${id}/power`, { 33 - "headers": { 34 - "Accept": "application/json", 35 - "Content-Type": "application/json", 36 - "Authorization": `Bearer ${process.env.PTERO_TOKEN}`, 37 - }, 38 - "body": { 39 - "signal": state, 40 - } 41 - }); 42 - } catch (error) { 43 - console.error(error.response.data.errors); 44 - return interaction.reply('An error occurred with your request...'); 45 - } 31 + try { 32 + await axios.post(`${process.env.PTERO_HOST}/api/client/servers/${id}/power`, { 33 + "headers": { 34 + "Accept": "application/json", 35 + "Content-Type": "application/json", 36 + "Authorization": `Bearer ${process.env.PTERO_TOKEN}`, 37 + }, 38 + "body": { 39 + "signal": state, 40 + }, 41 + }); 42 + } 43 + catch (error) { 44 + console.error(error.response.data.errors); 45 + return interaction.reply("An error occurred with your request..."); 46 + } 46 47 47 - return interaction.reply(`The **${state}** state has been sent to the server **${server.name}**`); 48 - } 48 + return interaction.reply(`The **${state}** state has been sent to the server **${server.name}**`); 49 + }, 49 50 };
+7 -7
src/commands/Ping.js
··· 1 1 const { SlashCommandBuilder } = require("discord.js"); 2 2 3 3 module.exports = { 4 - data: new SlashCommandBuilder() 5 - .setName("ping") 6 - .setDescription("Ping function to know if bot is operationnal"), 7 - async execute(interaction) { 8 - interaction.reply("Pong!") 9 - } 10 - } 4 + data: new SlashCommandBuilder() 5 + .setName("ping") 6 + .setDescription("Ping function to know if bot is operationnal"), 7 + async execute(interaction) { 8 + interaction.reply("Pong!"); 9 + }, 10 + };
+5 -5
src/events/ClientReady.js
··· 3 3 require("dotenv").config(); 4 4 5 5 module.exports = { 6 - name: Events.ClientReady, 7 - once: true, 8 - async execute(client) { 9 - Logger.success(`Ready! Logged in as ${client.user.tag}`); 10 - }, 6 + name: Events.ClientReady, 7 + once: true, 8 + async execute(client) { 9 + Logger.success(`Ready! Logged in as ${client.user.tag}`); 10 + }, 11 11 };
+7 -7
src/events/Debug.js
··· 1 1 const { Events } = require("discord.js"); 2 2 const Logger = require("../services/Logger"); 3 - require("dotenv").config() 3 + require("dotenv").config(); 4 4 5 5 // When the client is ready, run this code (only once) 6 6 module.exports = { 7 - name: Events.Debug, 8 - execute(debug) { 9 - if (process.env.BOT_MODE === "debug") { 10 - Logger.info(debug); 11 - } 12 - }, 7 + name: Events.Debug, 8 + execute(debug) { 9 + if (process.env.BOT_MODE === "debug") { 10 + Logger.info(debug); 11 + } 12 + }, 13 13 };
+4 -4
src/events/Error.js
··· 3 3 4 4 // When the client is ready, run this code (only once) 5 5 module.exports = { 6 - name: Events.Error, 7 - execute(error) { 8 - Logger.error(error); 9 - }, 6 + name: Events.Error, 7 + execute(error) { 8 + Logger.error(error); 9 + }, 10 10 };
+21 -21
src/events/InteractionCreate.js
··· 2 2 const Logger = require("../services/Logger"); 3 3 4 4 module.exports = { 5 - name: Events.InteractionCreate, 6 - async execute(interaction) { 7 - if (!interaction.isChatInputCommand()) return; 8 - const command = interaction.client.commands.get(interaction.commandName); 9 - if (!command) { 10 - Logger.error(`No command matching ${interaction.commandName} was found.`); 11 - return; 12 - } 13 - try { 14 - await command.execute(interaction); 15 - } 16 - catch (error) { 17 - Logger.error(error); 18 - if (interaction.replied || interaction.deferred) { 19 - await interaction.followUp({ content: "There was an error while executing this command!", ephemeral: true }); 20 - } 21 - else { 22 - await interaction.reply({ content: "There was an error while executing this command!", ephemeral: true }); 23 - } 24 - } 25 - }, 5 + name: Events.InteractionCreate, 6 + async execute(interaction) { 7 + if (!interaction.isChatInputCommand()) return; 8 + const command = interaction.client.commands.get(interaction.commandName); 9 + if (!command) { 10 + Logger.error(`No command matching ${interaction.commandName} was found.`); 11 + return; 12 + } 13 + try { 14 + await command.execute(interaction); 15 + } 16 + catch (error) { 17 + Logger.error(error); 18 + if (interaction.replied || interaction.deferred) { 19 + await interaction.followUp({ content: "There was an error while executing this command!", ephemeral: true }); 20 + } 21 + else { 22 + await interaction.reply({ content: "There was an error while executing this command!", ephemeral: true }); 23 + } 24 + } 25 + }, 26 26 };
+4 -4
src/events/Warn.js
··· 3 3 4 4 // When the client is ready, run this code (only once) 5 5 module.exports = { 6 - name: Events.Warn, 7 - execute(warn) { 8 - Logger.warn(warn); 9 - }, 6 + name: Events.Warn, 7 + execute(warn) { 8 + Logger.warn(warn); 9 + }, 10 10 };
+15 -17
src/functions/fetchClientServerInfo.js
··· 1 1 const axios = require("axios"); 2 2 require("dotenv").config(); 3 3 4 - async function fetchClientServerInfo(id) { 5 - const headers = { 6 - "Accept": "application/json", 7 - "Content-Type": "application/json", 8 - "Authorization": `Bearer ${process.env.PTERO_TOKEN}`, 9 - }; 4 + module.exports = async (id) => { 5 + const headers = { 6 + "Accept": "application/json", 7 + "Content-Type": "application/json", 8 + "Authorization": `Bearer ${process.env.PTERO_TOKEN}`, 9 + }; 10 10 11 - let response = await axios.get(`${process.env.PTERO_HOST}/api/client/servers/${id}`, { 12 - "headers": headers, 13 - }); 11 + let response = await axios.get(`${process.env.PTERO_HOST}/api/client/servers/${id}`, { 12 + "headers": headers, 13 + }); 14 14 15 - let server = response.data.attributes; 15 + const server = response.data.attributes; 16 16 17 - response = await axios.get(`${process.env.PTERO_HOST}/api/client/servers/${id}/resources`, { 18 - "headers": headers, 19 - }); 17 + response = await axios.get(`${process.env.PTERO_HOST}/api/client/servers/${id}/resources`, { 18 + "headers": headers, 19 + }); 20 20 21 - server.stats = response.data.attributes; 21 + server.stats = response.data.attributes; 22 22 23 - return server; 23 + return server; 24 24 }; 25 - 26 - module.exports = fetchClientServerInfo;
+59 -59
src/services/Bot.js
··· 5 5 require("dotenv").config(); 6 6 7 7 module.exports = class Bot { 8 - constructor() { 9 - // Set bot token 10 - this.token = process.env.BOT_TOKEN; 11 - // Create a new client instance 12 - this.client = new Client({ 13 - intents: [ 14 - GatewayIntentBits.Guilds, 15 - GatewayIntentBits.GuildMembers, 16 - GatewayIntentBits.GuildModeration, 17 - // GatewayIntentBits.GuildEmojisAndStickers, 18 - // GatewayIntentBits.GuildIntegrations, 19 - // GatewayIntentBits.GuildWebhooks, 20 - // GatewayIntentBits.GuildInvites, 21 - // GatewayIntentBits.GuildVoiceStates, 22 - // GatewayIntentBits.GuildPresences, 23 - GatewayIntentBits.GuildMessages, 24 - GatewayIntentBits.GuildMessageReactions, 25 - // GatewayIntentBits.GuildMessageTyping, 26 - // GatewayIntentBits.DirectMessages, 27 - // GatewayIntentBits.DirectMessageReactions, 28 - // GatewayIntentBits.DirectMessageTyping, 29 - // GatewayIntentBits.MessageContent, 30 - // GatewayIntentBits.GuildScheduledEvents, 31 - // GatewayIntentBits.AutoModerationConfiguration, 32 - // GatewayIntentBits.AutoModerationExecution, 33 - ] 34 - }); 35 - } 8 + constructor() { 9 + // Set bot token 10 + this.token = process.env.BOT_TOKEN; 11 + // Create a new client instance 12 + this.client = new Client({ 13 + intents: [ 14 + GatewayIntentBits.Guilds, 15 + GatewayIntentBits.GuildMembers, 16 + GatewayIntentBits.GuildModeration, 17 + // GatewayIntentBits.GuildEmojisAndStickers, 18 + // GatewayIntentBits.GuildIntegrations, 19 + // GatewayIntentBits.GuildWebhooks, 20 + // GatewayIntentBits.GuildInvites, 21 + // GatewayIntentBits.GuildVoiceStates, 22 + // GatewayIntentBits.GuildPresences, 23 + GatewayIntentBits.GuildMessages, 24 + GatewayIntentBits.GuildMessageReactions, 25 + // GatewayIntentBits.GuildMessageTyping, 26 + // GatewayIntentBits.DirectMessages, 27 + // GatewayIntentBits.DirectMessageReactions, 28 + // GatewayIntentBits.DirectMessageTyping, 29 + // GatewayIntentBits.MessageContent, 30 + // GatewayIntentBits.GuildScheduledEvents, 31 + // GatewayIntentBits.AutoModerationConfiguration, 32 + // GatewayIntentBits.AutoModerationExecution, 33 + ], 34 + }); 35 + } 36 36 37 - run() { 38 - // Log in to Discord with your token 39 - this.client.login(this.token); 40 - // Launch registration of all slash commands 41 - this.registerCommands(); 42 - // Launch registration of events 43 - this.registerEvents(); 44 - } 37 + run() { 38 + // Log in to Discord with your token 39 + this.client.login(this.token); 40 + // Launch registration of all slash commands 41 + this.registerCommands(); 42 + // Launch registration of events 43 + this.registerEvents(); 44 + } 45 45 46 - registerCommands() { 47 - // Create a new collection instance for commands 48 - this.client.commands = new Collection(); 49 - const commandsPath = path.join(__dirname, "..", "commands"); 50 - const commandFiles = fs.readdirSync(commandsPath).filter((file) => file.endsWith(".js")); 51 - for (const file of commandFiles) { 52 - const filePath = path.join(commandsPath, file); 53 - const command = require(filePath); 54 - // Set a new item in the Collection with the key as the command name and the value as the exported module 55 - if (command?.data && command?.execute) this.client.commands.set(command.data.name, command); 56 - else Logger.warn(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); 57 - } 58 - } 46 + registerCommands() { 47 + // Create a new collection instance for commands 48 + this.client.commands = new Collection(); 49 + const commandsPath = path.join(__dirname, "..", "commands"); 50 + const commandFiles = fs.readdirSync(commandsPath).filter((file) => file.endsWith(".js")); 51 + for (const file of commandFiles) { 52 + const filePath = path.join(commandsPath, file); 53 + const command = require(filePath); 54 + // Set a new item in the Collection with the key as the command name and the value as the exported module 55 + if (command?.data && command?.execute) this.client.commands.set(command.data.name, command); 56 + else Logger.warn(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); 57 + } 58 + } 59 59 60 - registerEvents() { 61 - const eventsPath = path.join(__dirname, "..", "events"); 62 - const eventsFiles = fs.readdirSync(eventsPath).filter((file) => file.endsWith(".js")); 63 - for (const file of eventsFiles) { 64 - const filePath = path.join(eventsPath, file); 65 - const event = require(filePath); 66 - if (event.once) this.client.once(event.name, (...args) => event.execute(...args)); 67 - else this.client.on(event.name, (...args) => event.execute(...args)); 68 - } 69 - } 60 + registerEvents() { 61 + const eventsPath = path.join(__dirname, "..", "events"); 62 + const eventsFiles = fs.readdirSync(eventsPath).filter((file) => file.endsWith(".js")); 63 + for (const file of eventsFiles) { 64 + const filePath = path.join(eventsPath, file); 65 + const event = require(filePath); 66 + if (event.once) this.client.once(event.name, (...args) => event.execute(...args)); 67 + else this.client.on(event.name, (...args) => event.execute(...args)); 68 + } 69 + } 70 70 };
+15 -15
src/services/Logger.js
··· 1 1 const { yellow, gray, red, cyan, green, bold } = require("colorette"); 2 2 3 3 module.exports = class Logger { 4 - static log(string) { 5 - return console.log(gray(string)); 6 - } 7 - static warn(string) { 8 - return console.warn(yellow(`${bold("[WARN]")} ${string}`)); 9 - } 10 - static error(string) { 11 - return console.error(red(`${bold("[ERROR]")} ${string}`)); 12 - } 13 - static info(string) { 14 - return console.info(cyan(`${bold("[INFO]")} ${string}`)); 15 - } 16 - static success(string) { 17 - return console.log(green(string)); 18 - } 4 + static log(string) { 5 + return console.log(gray(string)); 6 + } 7 + static warn(string) { 8 + return console.warn(yellow(`${bold("[WARN]")} ${string}`)); 9 + } 10 + static error(string) { 11 + return console.error(red(`${bold("[ERROR]")} ${string}`)); 12 + } 13 + static info(string) { 14 + return console.info(cyan(`${bold("[INFO]")} ${string}`)); 15 + } 16 + static success(string) { 17 + return console.log(green(string)); 18 + } 19 19 };