tangled
alpha
login
or
join now
cosmeak.dev
/
pterobot
0
fork
atom
A Discord Bot connected to your Pterodactyl API.
0
fork
atom
overview
issues
pulls
pipelines
fix: run lint fix
authored by
cosmeak.dev
and committed by
Cosmeak
2 years ago
e55d9305
cc189c9d
+287
-287
21 changed files
expand all
collapse all
unified
split
bin
command.deploy.js
src
commands
Backups.js
CreateBackup.js
Help.js
Power.js
Safemode.js
SendCommand.js
Server.js
Servers.js
events
ClientReady.js
Debug.js
Error.js
InteractionCreate.js
Warn.js
functions
bytesToSize.js
capitalizeFirstLetter.js
convertMilliseconds.js
fetchClientServerInfo.js
postServerPower.js
services
Bot.js
Logger.js
+22
-22
bin/command.deploy.js
reviewed
···
9
9
const __dirname = path.dirname(__filename);
10
10
11
11
(async () => {
12
12
-
const commands = [];
13
13
-
const commandsPath = path.join(__dirname, "..", "src", "commands");
14
14
-
const commandFiles = fs.readdirSync(commandsPath).filter((file) => file.endsWith(".js"));
15
15
-
for (const file of commandFiles) {
16
16
-
const filePath = path.join(commandsPath, file);
17
17
-
const command = await import(filePath);
18
18
-
if (command.default?.data && command.default?.execute) {
19
19
-
commands.push(command.default.data.toJSON());
20
20
-
}
21
21
-
}
22
22
-
const rest = new REST({ version: "10" }).setToken(process.env.BOT_TOKEN);
23
23
-
try {
24
24
-
console.log(`Started refreshing ${commands.length} application (/) commands`);
25
25
-
const data = await rest.put(
26
26
-
Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID),
27
27
-
{ body: commands },
28
28
-
);
29
29
-
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
30
30
-
}
31
31
-
catch (error) {
32
32
-
console.error(error);
33
33
-
}
12
12
+
const commands = [];
13
13
+
const commandsPath = path.join(__dirname, "..", "src", "commands");
14
14
+
const commandFiles = fs.readdirSync(commandsPath).filter((file) => file.endsWith(".js"));
15
15
+
for (const file of commandFiles) {
16
16
+
const filePath = path.join(commandsPath, file);
17
17
+
const command = await import(filePath);
18
18
+
if (command.default?.data && command.default?.execute) {
19
19
+
commands.push(command.default.data.toJSON());
20
20
+
}
21
21
+
}
22
22
+
const rest = new REST({ version: "10" }).setToken(process.env.BOT_TOKEN);
23
23
+
try {
24
24
+
console.log(`Started refreshing ${commands.length} application (/) commands`);
25
25
+
const data = await rest.put(
26
26
+
Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID),
27
27
+
{ body: commands },
28
28
+
);
29
29
+
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
30
30
+
}
31
31
+
catch (error) {
32
32
+
console.error(error);
33
33
+
}
34
34
})();
+5
-5
src/commands/Backups.js
reviewed
···
1
1
import { SlashCommandBuilder } from "discord.js";
2
2
3
3
export default {
4
4
-
data: new SlashCommandBuilder()
5
5
-
.setName("backups")
6
6
-
.setDescription("Show all your backups"),
7
7
-
async execute(interaction) {
4
4
+
data: new SlashCommandBuilder()
5
5
+
.setName("backups")
6
6
+
.setDescription("Show all your backups"),
7
7
+
async execute(interaction) {
8
8
9
9
-
},
9
9
+
},
10
10
};
+18
-18
src/commands/CreateBackup.js
reviewed
···
5
5
// config();
6
6
7
7
export default {
8
8
-
data: new SlashCommandBuilder()
9
9
-
.setName("create-backup")
10
10
-
.setDescription("Create a new backup for a server")
11
11
-
.addStringOption((option) =>
12
12
-
option.setName("server-id")
13
13
-
.setDescription("Your server identifier")
14
14
-
.setRequired(true),
15
15
-
),
16
16
-
async execute(interaction) {
17
17
-
const serverId = interaction.options.get("server-id").value;
18
18
-
const server = await fecthClientServerInfo(serverId);
19
19
-
const embed = new EmbedBuilder()
20
20
-
.setTitle(`Server: ${server.name}`)
21
21
-
.setDescription("New embed for a serve")
22
22
-
.setColor("Blurple")
23
23
-
.setTimestamp();
8
8
+
data: new SlashCommandBuilder()
9
9
+
.setName("create-backup")
10
10
+
.setDescription("Create a new backup for a server")
11
11
+
.addStringOption((option) =>
12
12
+
option.setName("server-id")
13
13
+
.setDescription("Your server identifier")
14
14
+
.setRequired(true),
15
15
+
),
16
16
+
async execute(interaction) {
17
17
+
const serverId = interaction.options.get("server-id").value;
18
18
+
const server = await fecthClientServerInfo(serverId);
19
19
+
const embed = new EmbedBuilder()
20
20
+
.setTitle(`Server: ${server.name}`)
21
21
+
.setDescription("New embed for a serve")
22
22
+
.setColor("Blurple")
23
23
+
.setTimestamp();
24
24
25
25
-
return interaction.reply({ embeds: [embed] });
26
26
-
},
25
25
+
return interaction.reply({ embeds: [embed] });
26
26
+
},
27
27
};
+5
-5
src/commands/Help.js
reviewed
···
1
1
import { SlashCommandBuilder } from "discord.js";
2
2
3
3
export default {
4
4
-
data: new SlashCommandBuilder()
5
5
-
.setName("help")
6
6
-
.setDescription("Show all available commands"),
7
7
-
async execute(interaction) {
4
4
+
data: new SlashCommandBuilder()
5
5
+
.setName("help")
6
6
+
.setDescription("Show all available commands"),
7
7
+
async execute(interaction) {
8
8
9
9
-
},
9
9
+
},
10
10
};
+30
-30
src/commands/Power.js
reviewed
···
2
2
import postServerPower from "../functions/postServerPower.js";
3
3
4
4
export default {
5
5
-
data: new SlashCommandBuilder()
6
6
-
.setName("power")
7
7
-
.setDescription("Provide a way to up or down a server")
8
8
-
.addStringOption((option) =>
9
9
-
option.setName("server-id")
10
10
-
.setDescription("Your server identifier")
11
11
-
.setRequired(true),
12
12
-
)
13
13
-
.addStringOption((option) =>
14
14
-
option.setName("state")
15
15
-
.setDescription("Choose a state for your server")
16
16
-
.setChoices(
17
17
-
{ name: "start", value: "start" },
18
18
-
{ name: "stop", value: "stop" },
19
19
-
{ name: "restart", value: "restart" },
20
20
-
{ name: "kill", value: "kill" },
21
21
-
)
22
22
-
.setRequired(true),
23
23
-
),
24
24
-
async execute(interaction) {
25
25
-
const state = interaction.options.get("state").value;
26
26
-
const id = interaction.options.get("server-id").value;
5
5
+
data: new SlashCommandBuilder()
6
6
+
.setName("power")
7
7
+
.setDescription("Provide a way to up or down a server")
8
8
+
.addStringOption((option) =>
9
9
+
option.setName("server-id")
10
10
+
.setDescription("Your server identifier")
11
11
+
.setRequired(true),
12
12
+
)
13
13
+
.addStringOption((option) =>
14
14
+
option.setName("state")
15
15
+
.setDescription("Choose a state for your server")
16
16
+
.setChoices(
17
17
+
{ name: "start", value: "start" },
18
18
+
{ name: "stop", value: "stop" },
19
19
+
{ name: "restart", value: "restart" },
20
20
+
{ name: "kill", value: "kill" },
21
21
+
)
22
22
+
.setRequired(true),
23
23
+
),
24
24
+
async execute(interaction) {
25
25
+
const state = interaction.options.get("state").value;
26
26
+
const id = interaction.options.get("server-id").value;
27
27
28
28
-
try {
29
29
-
await postServerPower(id, state);
30
30
-
}
31
31
-
catch (error) {
32
32
-
return interaction.reply({ content: error, ephemeral: true });
33
33
-
}
28
28
+
try {
29
29
+
await postServerPower(id, state);
30
30
+
}
31
31
+
catch (error) {
32
32
+
return interaction.reply({ content: error, ephemeral: true });
33
33
+
}
34
34
35
35
-
return interaction.reply(`**${state}** request as been sent to the server with success!`);
36
36
-
},
35
35
+
return interaction.reply(`**${state}** request as been sent to the server with success!`);
36
36
+
},
37
37
};
+5
-5
src/commands/Safemode.js
reviewed
···
1
1
import { SlashCommandBuilder } from "discord.js";
2
2
3
3
export default {
4
4
-
data: new SlashCommandBuilder()
5
5
-
.setName("safemode")
6
6
-
.setDescription("Turn servers into safemode - Kill/Stop and backup servers"),
7
7
-
async execute(interaction) {
4
4
+
data: new SlashCommandBuilder()
5
5
+
.setName("safemode")
6
6
+
.setDescription("Turn servers into safemode - Kill/Stop and backup servers"),
7
7
+
async execute(interaction) {
8
8
9
9
-
},
9
9
+
},
10
10
};
+10
-10
src/commands/SendCommand.js
reviewed
···
4
4
// config();
5
5
6
6
export default {
7
7
-
data: new SlashCommandBuilder()
8
8
-
.setName("send")
9
9
-
.setDescription("Send a command to your server")
10
10
-
.addStringOption((option) =>
11
11
-
option.setName("server-id")
12
12
-
.setDescription("Your server identifier")
13
13
-
.setRequired(true),
14
14
-
),
15
15
-
async execute(interaction) {
7
7
+
data: new SlashCommandBuilder()
8
8
+
.setName("send")
9
9
+
.setDescription("Send a command to your server")
10
10
+
.addStringOption((option) =>
11
11
+
option.setName("server-id")
12
12
+
.setDescription("Your server identifier")
13
13
+
.setRequired(true),
14
14
+
),
15
15
+
async execute(interaction) {
16
16
17
17
-
},
17
17
+
},
18
18
};
+24
-24
src/commands/Server.js
reviewed
···
5
5
import convertMilliseconds from "../functions/convertMilliseconds.js";
6
6
7
7
export default {
8
8
-
data: new SlashCommandBuilder()
9
9
-
.setName("server")
10
10
-
.setDescription("Show informations about a server and control it")
11
11
-
.addStringOption((option) =>
12
12
-
option.setName("server-id")
13
13
-
.setDescription("Your server identifier")
14
14
-
.setRequired(true),
15
15
-
),
16
16
-
async execute(interaction) {
17
17
-
const server = await fecthClientServerInfo(interaction.options.get("server-id").value);
18
18
-
const embed = new EmbedBuilder()
19
19
-
.setTitle(`${server.name} • ${server.identifier}`)
20
20
-
.setDescription(server.description ? server.description : "No description...")
21
21
-
.addFields(
22
22
-
{ name: "Status", value: (server.stats.current_state === "offline" ? "🔴 " : (server.stats.current_state === "running" ? "🟢 " : "🟠 ")) + capitalizeFirstLetter(server.stats.current_state), inline: true },
23
23
-
{ name: "CPU", value: `${server.stats.resources.cpu_absolute}%`, inline: true },
24
24
-
{ name: "Memory", value: bytesToSize(server.stats.resources.memory_bytes), inline: true },
25
25
-
{ name: "Disk", value: bytesToSize(server.stats.resources.disk_bytes), inline: true },
26
26
-
{ name: "Uptime", value: convertMilliseconds(server.stats.resources.uptime), inline: true },
27
27
-
)
28
28
-
.setColor(server.stats.current_state === "offline" ? "Red" : (server.stats.current_state === "running" ? "Green" : "Orange"))
29
29
-
.setTimestamp();
8
8
+
data: new SlashCommandBuilder()
9
9
+
.setName("server")
10
10
+
.setDescription("Show informations about a server and control it")
11
11
+
.addStringOption((option) =>
12
12
+
option.setName("server-id")
13
13
+
.setDescription("Your server identifier")
14
14
+
.setRequired(true),
15
15
+
),
16
16
+
async execute(interaction) {
17
17
+
const server = await fecthClientServerInfo(interaction.options.get("server-id").value);
18
18
+
const embed = new EmbedBuilder()
19
19
+
.setTitle(`${server.name} • ${server.identifier}`)
20
20
+
.setDescription(server.description ? server.description : "No description...")
21
21
+
.addFields(
22
22
+
{ name: "Status", value: (server.stats.current_state === "offline" ? "🔴 " : (server.stats.current_state === "running" ? "🟢 " : "🟠 ")) + capitalizeFirstLetter(server.stats.current_state) },
23
23
+
{ name: "Uptime", value: convertMilliseconds(server.stats.resources.uptime) },
24
24
+
{ name: "CPU", value: `${server.stats.resources.cpu_absolute}%`, inline: true },
25
25
+
{ name: "Memory", value: bytesToSize(server.stats.resources.memory_bytes), inline: true },
26
26
+
{ name: "Disk", value: bytesToSize(server.stats.resources.disk_bytes), inline: true },
27
27
+
)
28
28
+
.setColor(server.stats.current_state === "offline" ? "Red" : (server.stats.current_state === "running" ? "Green" : "Orange"))
29
29
+
.setTimestamp();
30
30
31
31
-
return interaction.reply({ embeds: [embed] });
32
32
-
},
31
31
+
return interaction.reply({ embeds: [embed] });
32
32
+
},
33
33
};
+22
-22
src/commands/Servers.js
reviewed
···
4
4
config();
5
5
6
6
export default {
7
7
-
data: new SlashCommandBuilder()
8
8
-
.setName("servers")
9
9
-
.setDescription("Show information about your servers"),
10
10
-
async execute(interaction) {
11
11
-
const response = await axios.get(`${process.env.PTERO_HOST}/api/client/`, {
12
12
-
"headers": {
13
13
-
"Accept": "application/json",
14
14
-
"Authorization": `Bearer ${process.env.PTERO_TOKEN}`,
15
15
-
},
16
16
-
});
7
7
+
data: new SlashCommandBuilder()
8
8
+
.setName("servers")
9
9
+
.setDescription("Show information about your servers"),
10
10
+
async execute(interaction) {
11
11
+
const response = await axios.get(`${process.env.PTERO_HOST}/api/client/`, {
12
12
+
"headers": {
13
13
+
"Accept": "application/json",
14
14
+
"Authorization": `Bearer ${process.env.PTERO_TOKEN}`,
15
15
+
},
16
16
+
});
17
17
18
18
-
console.log(response);
18
18
+
console.log(response);
19
19
20
20
-
let message = "";
21
21
-
response.data.data.forEach((server) => {
22
22
-
message += `**${server.attributes.name}** [ ${server.attributes.identifier} ]\n\n`;
23
23
-
});
20
20
+
let message = "";
21
21
+
response.data.data.forEach((server) => {
22
22
+
message += `**${server.attributes.name}** [ ${server.attributes.identifier} ]\n\n`;
23
23
+
});
24
24
25
25
-
const embed = new EmbedBuilder()
26
26
-
.setTitle("Your servers")
27
27
-
.setDescription(message)
28
28
-
.setColor("Blurple")
29
29
-
.setTimestamp();
25
25
+
const embed = new EmbedBuilder()
26
26
+
.setTitle("Your servers")
27
27
+
.setDescription(message)
28
28
+
.setColor("Blurple")
29
29
+
.setTimestamp();
30
30
31
31
-
return interaction.reply({ embeds: [embed] });
32
32
-
},
31
31
+
return interaction.reply({ embeds: [embed] });
32
32
+
},
33
33
};
+5
-5
src/events/ClientReady.js
reviewed
···
4
4
config();
5
5
6
6
export default {
7
7
-
name: Events.ClientReady,
8
8
-
once: true,
9
9
-
async execute(client) {
10
10
-
Logger.success(`Ready! Logged in as ${client.user.tag}`);
11
11
-
},
7
7
+
name: Events.ClientReady,
8
8
+
once: true,
9
9
+
async execute(client) {
10
10
+
Logger.success(`Ready! Logged in as ${client.user.tag}`);
11
11
+
},
12
12
};
+6
-6
src/events/Debug.js
reviewed
···
4
4
config();
5
5
6
6
export default {
7
7
-
name: Events.Debug,
8
8
-
execute(debug) {
9
9
-
if (process.env.BOT_MODE === "debug") {
10
10
-
Logger.info(debug);
11
11
-
}
12
12
-
},
7
7
+
name: Events.Debug,
8
8
+
execute(debug) {
9
9
+
if (process.env.BOT_MODE === "debug") {
10
10
+
Logger.info(debug);
11
11
+
}
12
12
+
},
13
13
};
+4
-4
src/events/Error.js
reviewed
···
2
2
import { Logger } from "../services/Logger.js";
3
3
4
4
export default {
5
5
-
name: Events.Error,
6
6
-
execute(error) {
7
7
-
Logger.error(error);
8
8
-
},
5
5
+
name: Events.Error,
6
6
+
execute(error) {
7
7
+
Logger.error(error);
8
8
+
},
9
9
};
+21
-21
src/events/InteractionCreate.js
reviewed
···
2
2
import { Logger } from "../services/Logger.js";
3
3
4
4
export default {
5
5
-
name: Events.InteractionCreate,
6
6
-
async execute(interaction) {
7
7
-
if (!interaction.isChatInputCommand()) return;
8
8
-
const command = interaction.client.commands.get(interaction.commandName);
9
9
-
if (!command) {
10
10
-
Logger.error(`No command matching ${interaction.commandName} was found.`);
11
11
-
return;
12
12
-
}
13
13
-
try {
14
14
-
await command.execute(interaction);
15
15
-
}
16
16
-
catch (error) {
17
17
-
Logger.error(error);
18
18
-
if (interaction.replied || interaction.deferred) {
19
19
-
await interaction.followUp({ content: "There was an error while executing this command!", ephemeral: true });
20
20
-
}
21
21
-
else {
22
22
-
await interaction.reply({ content: "There was an error while executing this command!", ephemeral: true });
23
23
-
}
24
24
-
}
25
25
-
},
5
5
+
name: Events.InteractionCreate,
6
6
+
async execute(interaction) {
7
7
+
if (!interaction.isChatInputCommand()) return;
8
8
+
const command = interaction.client.commands.get(interaction.commandName);
9
9
+
if (!command) {
10
10
+
Logger.error(`No command matching ${interaction.commandName} was found.`);
11
11
+
return;
12
12
+
}
13
13
+
try {
14
14
+
await command.execute(interaction);
15
15
+
}
16
16
+
catch (error) {
17
17
+
Logger.error(error);
18
18
+
if (interaction.replied || interaction.deferred) {
19
19
+
await interaction.followUp({ content: "There was an error while executing this command!", ephemeral: true });
20
20
+
}
21
21
+
else {
22
22
+
await interaction.reply({ content: "There was an error while executing this command!", ephemeral: true });
23
23
+
}
24
24
+
}
25
25
+
},
26
26
};
+4
-4
src/events/Warn.js
reviewed
···
3
3
4
4
// When the client is ready, run this code (only once)
5
5
export default {
6
6
-
name: Events.Warn,
7
7
-
execute(warn) {
8
8
-
Logger.warn(warn);
9
9
-
},
6
6
+
name: Events.Warn,
7
7
+
execute(warn) {
8
8
+
Logger.warn(warn);
9
9
+
},
10
10
};
+5
-5
src/functions/bytesToSize.js
reviewed
···
1
1
export default (bytes) => {
2
2
-
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
3
3
-
if (bytes === 0) return "n/a";
4
4
-
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10);
5
5
-
if (i === 0) return `${bytes} ${sizes[i]})`;
6
6
-
return `${(bytes / (1024 ** i)).toFixed(1)} ${sizes[i]}`;
2
2
+
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
3
3
+
if (bytes === 0) return "n/a";
4
4
+
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10);
5
5
+
if (i === 0) return `${bytes} ${sizes[i]})`;
6
6
+
return `${(bytes / (1024 ** i)).toFixed(1)} ${sizes[i]}`;
7
7
};
+1
-1
src/functions/capitalizeFirstLetter.js
reviewed
···
1
1
export default (string) => {
2
2
-
return string.charAt(0).toUpperCase() + string.slice(1);
2
2
+
return string.charAt(0).toUpperCase() + string.slice(1);
3
3
};
+8
-8
src/functions/convertMilliseconds.js
reviewed
···
1
1
export default (ms) => {
2
2
-
const days = Math.floor(ms / (24 * 60 * 60 * 1000));
3
3
-
const daysms = ms % (24 * 60 * 60 * 1000);
4
4
-
const hours = Math.floor(daysms / (60 * 60 * 1000));
5
5
-
const hoursms = ms % (60 * 60 * 1000);
6
6
-
const minutes = Math.floor(hoursms / (60 * 1000));
7
7
-
const minutesms = ms % (60 * 1000);
8
8
-
const secondes = Math.floor(minutesms / 1000);
9
9
-
return `${days}days ${hours}h ${minutes}min ${secondes}s`;
2
2
+
const days = Math.floor(ms / (24 * 60 * 60 * 1000));
3
3
+
const daysms = ms % (24 * 60 * 60 * 1000);
4
4
+
const hours = Math.floor(daysms / (60 * 60 * 1000));
5
5
+
const hoursms = ms % (60 * 60 * 1000);
6
6
+
const minutes = Math.floor(hoursms / (60 * 1000));
7
7
+
const minutesms = ms % (60 * 1000);
8
8
+
const secondes = Math.floor(minutesms / 1000);
9
9
+
return `${days} days ${hours}h ${minutes}min ${secondes}s`;
10
10
};
+14
-14
src/functions/fetchClientServerInfo.js
reviewed
···
3
3
config();
4
4
5
5
export default async (id) => {
6
6
-
const headers = {
7
7
-
"Accept": "application/json",
8
8
-
"Content-Type": "application/json",
9
9
-
"Authorization": `Bearer ${process.env.PTERO_TOKEN}`,
10
10
-
};
6
6
+
const headers = {
7
7
+
"Accept": "application/json",
8
8
+
"Content-Type": "application/json",
9
9
+
"Authorization": `Bearer ${process.env.PTERO_TOKEN}`,
10
10
+
};
11
11
12
12
-
let response = await axios.get(`${process.env.PTERO_HOST}/api/client/servers/${id}`, {
13
13
-
"headers": headers,
14
14
-
});
12
12
+
let response = await axios.get(`${process.env.PTERO_HOST}/api/client/servers/${id}`, {
13
13
+
"headers": headers,
14
14
+
});
15
15
16
16
-
const server = response.data.attributes;
16
16
+
const server = response.data.attributes;
17
17
18
18
-
response = await axios.get(`${process.env.PTERO_HOST}/api/client/servers/${id}/resources`, {
19
19
-
"headers": headers,
20
20
-
});
18
18
+
response = await axios.get(`${process.env.PTERO_HOST}/api/client/servers/${id}/resources`, {
19
19
+
"headers": headers,
20
20
+
});
21
21
22
22
-
server.stats = response.data.attributes;
22
22
+
server.stats = response.data.attributes;
23
23
24
24
-
return server;
24
24
+
return server;
25
25
};
+16
-16
src/functions/postServerPower.js
reviewed
···
3
3
config();
4
4
5
5
export default async (id, signal) => {
6
6
-
try {
7
7
-
await axios.post(`${process.env.PTERO_HOST}/api/client/servers/${id}/power`, {
8
8
-
"headers": {
9
9
-
"Accept": "application/json",
10
10
-
"Content-Type": "application/json",
11
11
-
"Authorization": `Bearer ${process.env.PTERO_TOKEN}`,
12
12
-
},
13
13
-
"body": {
14
14
-
"signal": signal,
15
15
-
},
16
16
-
});
17
17
-
}
18
18
-
catch (error) {
19
19
-
console.log(error.request.data.errors);
20
20
-
throw "An error occured with the request to your API...";
21
21
-
}
6
6
+
try {
7
7
+
await axios.post(`${process.env.PTERO_HOST}/api/client/servers/${id}/power`, {
8
8
+
"headers": {
9
9
+
"Accept": "application/json",
10
10
+
"Content-Type": "application/json",
11
11
+
"Authorization": `Bearer ${process.env.PTERO_TOKEN}`,
12
12
+
},
13
13
+
"body": {
14
14
+
"signal": signal,
15
15
+
},
16
16
+
});
17
17
+
}
18
18
+
catch (error) {
19
19
+
console.log(error.request.data.errors);
20
20
+
throw "An error occured with the request to your API...";
21
21
+
}
22
22
};
+46
-46
src/services/Bot.js
reviewed
···
10
10
const __dirname = path.dirname(__filename);
11
11
12
12
class Bot {
13
13
-
constructor() {
14
14
-
// Set bot token
15
15
-
this.token = process.env.BOT_TOKEN;
16
16
-
// Create a new client instance
17
17
-
this.client = new Client({
18
18
-
intents: [
19
19
-
GatewayIntentBits.Guilds,
20
20
-
GatewayIntentBits.GuildMembers,
21
21
-
GatewayIntentBits.GuildMessages,
22
22
-
],
23
23
-
});
24
24
-
}
13
13
+
constructor() {
14
14
+
// Set bot token
15
15
+
this.token = process.env.BOT_TOKEN;
16
16
+
// Create a new client instance
17
17
+
this.client = new Client({
18
18
+
intents: [
19
19
+
GatewayIntentBits.Guilds,
20
20
+
GatewayIntentBits.GuildMembers,
21
21
+
GatewayIntentBits.GuildMessages,
22
22
+
],
23
23
+
});
24
24
+
}
25
25
26
26
-
async run() {
27
27
-
// Log in to Discord with your token
28
28
-
await this.client.login(this.token);
29
29
-
// Launch registration of all slash commands
30
30
-
await this.registerCommands();
31
31
-
// Launch registration of events
32
32
-
await this.registerEvents();
33
33
-
}
26
26
+
async run() {
27
27
+
// Log in to Discord with your token
28
28
+
await this.client.login(this.token);
29
29
+
// Launch registration of all slash commands
30
30
+
await this.registerCommands();
31
31
+
// Launch registration of events
32
32
+
await this.registerEvents();
33
33
+
}
34
34
35
35
-
async registerCommands() {
36
36
-
// Create a new collection instance for commands
37
37
-
this.client.commands = new Collection();
38
38
-
const commandsPath = path.join(__dirname, "..", "commands");
39
39
-
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith(".js"));
40
40
-
for (const file of commandFiles) {
41
41
-
const filePath = path.join(commandsPath, file);
42
42
-
let command = await import(filePath);
43
43
-
command = command.default;
44
44
-
// Set a new item in the Collection with the key as the command name and the value as the exported module
45
45
-
if (command?.data && command?.execute) this.client.commands.set(command.data.name, command);
46
46
-
else Logger.warn(`The command at ${filePath} is missing a required "data" or "execute" property.`);
47
47
-
}
48
48
-
}
35
35
+
async registerCommands() {
36
36
+
// Create a new collection instance for commands
37
37
+
this.client.commands = new Collection();
38
38
+
const commandsPath = path.join(__dirname, "..", "commands");
39
39
+
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith(".js"));
40
40
+
for (const file of commandFiles) {
41
41
+
const filePath = path.join(commandsPath, file);
42
42
+
let command = await import(filePath);
43
43
+
command = command.default;
44
44
+
// Set a new item in the Collection with the key as the command name and the value as the exported module
45
45
+
if (command?.data && command?.execute) this.client.commands.set(command.data.name, command);
46
46
+
else Logger.warn(`The command at ${filePath} is missing a required "data" or "execute" property.`);
47
47
+
}
48
48
+
}
49
49
50
50
-
async registerEvents() {
51
51
-
const eventsPath = path.join(__dirname, "..", "events");
52
52
-
const eventsFiles = fs.readdirSync(eventsPath).filter((file) => file.endsWith(".js"));
50
50
+
async registerEvents() {
51
51
+
const eventsPath = path.join(__dirname, "..", "events");
52
52
+
const eventsFiles = fs.readdirSync(eventsPath).filter((file) => file.endsWith(".js"));
53
53
54
54
-
for (const file of eventsFiles) {
55
55
-
const filePath = path.join(eventsPath, file);
56
56
-
let event = await import(filePath);
57
57
-
event = event.default;
58
58
-
if (event.once) this.client.once(event.name, (...args) => event.execute(...args));
59
59
-
else this.client.on(event.name, (...args) => event.execute(...args));
60
60
-
}
61
61
-
}
62
62
-
};
54
54
+
for (const file of eventsFiles) {
55
55
+
const filePath = path.join(eventsPath, file);
56
56
+
let event = await import(filePath);
57
57
+
event = event.default;
58
58
+
if (event.once) this.client.once(event.name, (...args) => event.execute(...args));
59
59
+
else this.client.on(event.name, (...args) => event.execute(...args));
60
60
+
}
61
61
+
}
62
62
+
}
63
63
64
64
export { Bot };
+16
-16
src/services/Logger.js
reviewed
···
1
1
import { yellow, gray, red, cyan, green, bold } from "colorette";
2
2
3
3
class Logger {
4
4
-
static log(string) {
5
5
-
return console.log(gray(string));
6
6
-
}
7
7
-
static warn(string) {
8
8
-
return console.warn(yellow(`${bold("[WARN]")} ${string}`));
9
9
-
}
10
10
-
static error(string) {
11
11
-
return console.error(red(`${bold("[ERROR]")} ${string}`));
12
12
-
}
13
13
-
static info(string) {
14
14
-
return console.info(cyan(`${bold("[INFO]")} ${string}`));
15
15
-
}
16
16
-
static success(string) {
17
17
-
return console.log(green(string));
18
18
-
}
19
19
-
};
4
4
+
static log(string) {
5
5
+
return console.log(gray(string));
6
6
+
}
7
7
+
static warn(string) {
8
8
+
return console.warn(yellow(`${bold("[WARN]")} ${string}`));
9
9
+
}
10
10
+
static error(string) {
11
11
+
return console.error(red(`${bold("[ERROR]")} ${string}`));
12
12
+
}
13
13
+
static info(string) {
14
14
+
return console.info(cyan(`${bold("[INFO]")} ${string}`));
15
15
+
}
16
16
+
static success(string) {
17
17
+
return console.log(green(string));
18
18
+
}
19
19
+
}
20
20
21
21
export { Logger };