-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
147 lines (125 loc) · 6.33 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const { SlashCommandBuilder } = require('@discordjs/builders');
const Rcon = require('rcon');
const { exec, spawn } = require('child_process');
client.once('ready', () => {
console.log('Bot is Starting');
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const { commandName, options } = interaction;
// Define an async function to handle the interaction
async function handleInteraction() {
if (commandName === 'startserver') {
// Check if interaction has already been replied to or deferred
if (interaction.replied || interaction.deferred) return;
// await interaction.reply('Checking server status...');
console.log('Start Server command received');
await interaction.deferReply({content: "Start Server", ephemeral: false });
// Execute the batch file to check server status
exec('Check_Server.bat', async (error, stdout, stderr) => {
if (error) {
console.error(`Error executing the batch file: ${error}`);
return;
}
// Check the output of the batch file to determine if the server is already running
if (stdout.includes('PalServer is already running')) {
await interaction.editReply({ content: "Server is already running", ephemeral: true });
console.log('Server is already running');
return;
} else {
// Start PalServer.exe process in the background
const palServerProcess = spawn('Location of your PalServer.exe', ['-publicport=8211', 'EpicApp=PalServer', '-useperfthreads', '-NoAsyncLoadingThread', '-UseMultithreadForDS'], { detached: true });
palServerProcess.unref(); // Detach the child process from the parent
await interaction.editReply({ content: "Server started successfully", ephemeral: true });
console.log('Server started successfully');
return;
}
});
return;
} else if (commandName === 'statusserver') {
// Logic for checking server status
await interaction.reply('Server Status not implemented yet');
} else if (commandName === 'restartserver') {
// Logic for restarting the server
await interaction.reply('Restarting Server in 1 Minute');
var conn = new Rcon('ServerIP', 25575, 'adminpassword', true);
conn.on('auth', function() {
// You must wait until this event is fired before sending any commands,
// otherwise those commands will fail.
console.log("Authenticated");
console.log("Sending command: Restart Server")
conn.send("save");
conn.send("shutdown 60 ServerRestartIn1minLOGOUTNOW.");
}).on('response', function(str) {
console.log("Response: " + str);
interaction.editReply({ content: `Message sent to server: ${message}`, ephemeral: true });
}).on('error', function(err) {
console.log("Error: " + err);
}).on('end', function() {
console.log("Connection closed");
});
conn.connect();
} else if (commandName === 'sendmessage') {
// Check if interaction has already been replied to or deferred
if (interaction.replied || interaction.deferred) return;
const message = options.getString('message');
if (!message) {
await interaction.reply({ content: "Please provide a message to send.", ephemeral: true });
return;
}
await interaction.deferReply({ content: "Sending message to server...", ephemeral: true });
// Logic for messaging the server
var conn = new Rcon('ServerIP', 25575, 'adminpassword', true);
conn.on('auth', function() {
// You must wait until this event is fired before sending any commands,
// otherwise those commands will fail.
console.log("Authenticated");
console.log("Sending command: Broadcast")
conn.send(`broadcast ${message}`);
}).on('response', function(str) {
console.log("Response: " + str);
interaction.editReply({ content: `Message sent to server: ${message}`, ephemeral: true });
}).on('error', function(err) {
console.log("Error: " + err);
}).on('end', function() {
console.log("Connection closed");
});
conn.connect();
await interaction.editReply('Message Sent to the server');
} else {
await interaction.reply('Slash Command Not Registered');
console.log('Unknown command received');
}
}
// Call the async function to handle the interaction
handleInteraction();
});
client.login('Discord Token Bot');
// Register slash commands
client.once('ready', async () => {
try {
console.log('Registering Commands');
const commands = [
new SlashCommandBuilder()
.setName('startserver')
.setDescription('Start the server'),
new SlashCommandBuilder()
.setName('statusserver')
.setDescription('Check server status'),
new SlashCommandBuilder()
.setName('restartserver')
.setDescription('Restart the server'),
new SlashCommandBuilder()
.setName('sendmessage')
.setDescription('Send a message to the server')
.addStringOption(option => option.setName('message').setDescription('Message that you want to send to the server, NO SPACE ONLY UNDERSCORE').setRequired(true))
];
const rest = await client.guilds.cache.get('ServerID')?.commands;
console.log('Registered');
await rest?.set(commands);
} catch (error) {
console.error('Error registering slash commands:', error);
}
});