Skip to content

Commit

Permalink
refactor cscl commands to support fkz api, add na servers to command
Browse files Browse the repository at this point in the history
  • Loading branch information
jvnipers committed Oct 20, 2024
1 parent 306735e commit c783c3e
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 59 deletions.
89 changes: 69 additions & 20 deletions src/commands/CSCL/cscl-restart.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ const {
EmbedBuilder,
} = require("discord.js");
const { exec } = require("child_process");
const axios = require("axios");
const wait = require("timers/promises").setTimeout;
require("dotenv").config();

const username = process.env.DATHOST_USERNAME;
const password = process.env.DATHOST_PASSWORD;
const key = process.env.API_KEY;
const port = process.env.PORT || 8080;
const apiUrl = new URL(process.env.API_URL);
const url = `${apiUrl.origin}:${port}${apiUrl.pathname}`;

module.exports = {
data: new SlashCommandBuilder()
Expand All @@ -22,9 +25,11 @@ module.exports = {
.setDescription("Which server do you want to restart")
.setRequired(true)
.addChoices(
{ name: "CS:CL FKZ 1 - VNL KZ 128t", value: "cscl-fkz-1" },
{ name: "CS:CL FKZ 2 - VNL KZ 64t", value: "cscl-fkz-2" },
{ name: "CS:CL FKZ 3 - KZTimer 128t", value: "cscl-fkz-3" }
{ name: "CS:CL EU FKZ 1 - VNL KZ 128t", value: "cscl-fkz-1" },
{ name: "CS:CL EU FKZ 2 - VNL KZ 64t", value: "cscl-fkz-2" },
{ name: "CS:CL EU FKZ 3 - KZTimer 128t", value: "cscl-fkz-3" },
{ name: "CS:CL NA FKZ 1 - VNL KZ 128t", value: "cscl-fkz-4" },
{ name: "CS:CL NA FKZ 2 - VNL KZ 64t", value: "cscl-fkz-5" }
)
),

Expand All @@ -40,20 +45,30 @@ module.exports = {

const server = {
"cscl-fkz-1": {
name: "CS:CL FKZ 1 - VNL KZ 128t",
name: "CS:CL EU - FKZ 1 - VNL KZ 128t",
user: "cscl-fkz-1",
id: null,
},
"cscl-fkz-2": {
name: "CS:CL FKZ 2 - VNL KZ 64t",
name: "CS:CL EU - FKZ 2 - VNL KZ 64t",
user: "cscl-fkz-2",
id: null,
},
"cscl-fkz-3": {
name: "CS:CL FKZ 3 - KZTimer 128t",
name: "CS:CL EU - FKZ 3 - KZTimer 128t",
user: "cscl-fkz-3",
id: null,
},
"cscl-fkz-4": {
name: "CS:CL NA - FKZ 1 - VNL KZ 128t",
user: "cscl-fkz-1",
id: 1,
},
"cscl-fkz-5": {
name: "CS:CL NA - FKZ 2 - VNL KZ 64t",
user: "cscl-fkz-2",
id: 2,
},
}[servers];

if (!server) {
Expand All @@ -65,6 +80,7 @@ module.exports = {
}

const { name, user, id } = server;
const command = `sudo -iu ${user} /home/${user}/csgoserver restart`;

if (
!interaction.member.permissions.has(PermissionFlagsBits.Administrator) &&
Expand All @@ -83,20 +99,53 @@ module.exports = {
embeds: [embed],
ephemeral: true,
});
exec(
`sudo -iu ${user} /home/${user}/csgoserver restart`,
async (error, stdout, stderr) => {
if (id === null) {
exec(command, async (error, stdout, stderr) => {
if (error) console.log(error);
//if (stderr) console.log(stderr);
//if (stdout) console.log(stdout);
});
await wait(3000);
embed.setDescription(`Restarted: ${name}`);
return await interaction.editReply({
embeds: [embed],
ephemeral: true,
});
} else {
if (!url || !key) {
embed.setDescription("API url and/or key missing.");
return await interaction.editReply({
embeds: [embed],
ephemeral: true,
});
}
);
await wait(3000);
embed.setDescription(`Restarted: ${name}`);
return await interaction.editReply({
embeds: [embed],
ephemeral: true,
});
const response = await axios.post(
url,
{
command: command,
},
{
headers: {
authorization: `Bearer ${key}`,
},
}
);
await wait(3000);
if (response.data.status === 200) {
embed.setDescription(`Restarted: ${name}`);
return await interaction.editReply({
embeds: [embed],
ephemeral: true,
});
} else {
console.log(response.status, response.data);
embed.setDescription(
`Something went wrong while restarting: ${name}`
);
return await interaction.editReply({
embeds: [embed],
ephemeral: true,
});
}
}
} catch (error) {
await interaction.reply({
content: `Error: ${error}`,
Expand Down
81 changes: 64 additions & 17 deletions src/commands/CSCL/cscl-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ const {
EmbedBuilder,
} = require("discord.js");
const { exec } = require("child_process");
const axios = require("axios");
const wait = require("timers/promises").setTimeout;
require("dotenv").config();

const username = process.env.DATHOST_USERNAME;
const password = process.env.DATHOST_PASSWORD;
const key = process.env.API_KEY;
const port = process.env.PORT || 8080;
const apiUrl = new URL(process.env.API_URL);
const url = `${apiUrl.origin}:${port}${apiUrl.pathname}`;

module.exports = {
data: new SlashCommandBuilder()
Expand All @@ -22,9 +25,11 @@ module.exports = {
.setDescription("Which server do you want to start")
.setRequired(true)
.addChoices(
{ name: "CS:CL FKZ 1 - VNL KZ 128t", value: "cscl-fkz-1" },
{ name: "CS:CL FKZ 2 - VNL KZ 64t", value: "cscl-fkz-2" },
{ name: "CS:CL FKZ 3 - KZTimer 128t", value: "cscl-fkz-3" }
{ name: "CS:CL EU FKZ 1 - VNL KZ 128t", value: "cscl-fkz-1" },
{ name: "CS:CL EU FKZ 2 - VNL KZ 64t", value: "cscl-fkz-2" },
{ name: "CS:CL EU FKZ 3 - KZTimer 128t", value: "cscl-fkz-3" },
{ name: "CS:CL NA FKZ 1 - VNL KZ 128t", value: "cscl-fkz-4" },
{ name: "CS:CL NA FKZ 2 - VNL KZ 64t", value: "cscl-fkz-5" }
)
),

Expand Down Expand Up @@ -54,6 +59,16 @@ module.exports = {
user: "cscl-fkz-3",
id: null,
},
"cscl-fkz-4": {
name: "CS:CL NA - FKZ 1 - VNL KZ 128t",
user: "cscl-fkz-1",
id: 1,
},
"cscl-fkz-5": {
name: "CS:CL NA - FKZ 2 - VNL KZ 64t",
user: "cscl-fkz-2",
id: 2,
},
}[servers];

if (!server) {
Expand All @@ -65,6 +80,7 @@ module.exports = {
}

const { name, user, id } = server;
const command = `sudo -iu ${user} /home/${user}/csgoserver start`;

if (
!interaction.member.permissions.has(PermissionFlagsBits.Administrator) &&
Expand All @@ -83,20 +99,51 @@ module.exports = {
embeds: [embed],
ephemeral: true,
});
exec(
`sudo -iu ${user} /home/${user}/csgoserver start`,
async (error, stdout, stderr) => {
if (id === null) {
exec(command, async (error, stdout, stderr) => {
if (error) console.log(error);
//if (stderr) console.log(stderr);
//if (stdout) console.log(stdout);
});
await wait(3000);
embed.setDescription(`Started: ${name}`);
return await interaction.editReply({
embeds: [embed],
ephemeral: true,
});
} else {
if (!url || !key) {
embed.setDescription("API url and/or key missing.");
return await interaction.editReply({
embeds: [embed],
ephemeral: true,
});
}
);
await wait(3000);
embed.setDescription(`Started: ${name}`);
return await interaction.editReply({
embeds: [embed],
ephemeral: true,
});
const response = await axios.post(
url,
{
command: command,
},
{
headers: {
authorization: `Bearer ${key}`,
},
}
);
await wait(3000);
if (response.data.status === 200) {
embed.setDescription(`Started: ${name}`);
return await interaction.editReply({
embeds: [embed],
ephemeral: true,
});
} else {
console.log(response.status, response.data);
embed.setDescription(`Something went wrong while starting: ${name}`);
return await interaction.editReply({
embeds: [embed],
ephemeral: true,
});
}
}
} catch (error) {
await interaction.reply({
content: `Error: ${error}`,
Expand Down
91 changes: 69 additions & 22 deletions src/commands/CSCL/cscl-stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ const {
EmbedBuilder,
} = require("discord.js");
const { exec } = require("child_process");
const axios = require("axios");
const wait = require("timers/promises").setTimeout;
require("dotenv").config();

const username = process.env.DATHOST_USERNAME;
const password = process.env.DATHOST_PASSWORD;
const key = process.env.API_KEY;
const port = process.env.PORT || 8080;
const apiUrl = new URL(process.env.API_URL);
const url = `${apiUrl.origin}:${port}${apiUrl.pathname}`;

module.exports = {
data: new SlashCommandBuilder()
Expand All @@ -22,9 +25,11 @@ module.exports = {
.setDescription("Which server do you want to stop")
.setRequired(true)
.addChoices(
{ name: "CS:CL FKZ 1 - VNL KZ 128t", value: "cscl-fkz-1" },
{ name: "CS:CL FKZ 2 - VNL KZ 64t", value: "cscl-fkz-2" },
{ name: "CS:CL FKZ 3 - KZTimer 128t", value: "cscl-fkz-3" }
{ name: "CS:CL EU FKZ 1 - VNL KZ 128t", value: "cscl-fkz-1" },
{ name: "CS:CL EU FKZ 2 - VNL KZ 64t", value: "cscl-fkz-2" },
{ name: "CS:CL EU FKZ 3 - KZTimer 128t", value: "cscl-fkz-3" },
{ name: "CS:CL NA FKZ 1 - VNL KZ 128t", value: "cscl-fkz-4" },
{ name: "CS:CL NA FKZ 2 - VNL KZ 64t", value: "cscl-fkz-5" }
)
),

Expand Down Expand Up @@ -54,6 +59,16 @@ module.exports = {
user: "cscl-fkz-3",
id: null,
},
"cscl-fkz-4": {
name: "CS:CL NA - FKZ 1 - VNL KZ 128t",
user: "cscl-fkz-1",
id: 1,
},
"cscl-fkz-5": {
name: "CS:CL NA - FKZ 2 - VNL KZ 64t",
user: "cscl-fkz-2",
id: 2,
},
}[servers];

if (!server) {
Expand All @@ -65,6 +80,7 @@ module.exports = {
}

const { name, user, id } = server;
const command = `sudo -iu ${user} /home/${user}/csgoserver stop`;

if (
!interaction.member.permissions.has(PermissionFlagsBits.Administrator) &&
Expand All @@ -78,25 +94,56 @@ module.exports = {
}

try {
embed.setDescription(`Stopping: ${name}`);
await interaction.reply({
embeds: [embed],
ephemeral: true,
});
exec(
`sudo -iu ${user} /home/${user}/csgoserver stop`,
async (error, stdout, stderr) => {
if (id === null) {
embed.setDescription(`Stopping: ${name}`);
await interaction.reply({
embeds: [embed],
ephemeral: true,
});
exec(command, async (error, stdout, stderr) => {
if (error) console.log(error);
//if (stderr) console.log(stderr);
//if (stdout) console.log(stdout);
});
await wait(3000);
embed.setDescription(`Stopped: ${name}`);
return await interaction.editReply({
embeds: [embed],
ephemeral: true,
});
} else {
if (!url || !key) {
embed.setDescription("API url and/or key missing.");
return await interaction.editReply({
embeds: [embed],
ephemeral: true,
});
}
);
await wait(3000);
embed.setDescription(`Stopped: ${name}`);
return await interaction.editReply({
embeds: [embed],
ephemeral: true,
});
const response = await axios.post(
url,
{
command: command,
},
{
headers: {
authorization: `Bearer ${key}`,
},
}
);
await wait(3000);
if (response.data.status === 200) {
embed.setDescription(`Stopped: ${name}`);
return await interaction.editReply({
embeds: [embed],
ephemeral: true,
});
} else {
console.log(response.status, response.data);
embed.setDescription(`Something went wrong while stopping: ${name}`);
return await interaction.editReply({
embeds: [embed],
ephemeral: true,
});
}
}
} catch (error) {
await interaction.reply({
content: `Error: ${error}`,
Expand Down

0 comments on commit c783c3e

Please sign in to comment.