-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
400 lines (359 loc) · 14.2 KB
/
server.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
const { Client, GatewayIntentBits, MessageActionRow, MessageButton, ApplicationCommandOptionType } = require('discord.js');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { Command } = require('commander');
const program = new Command();
require('dotenv').config();
const fs = require('fs');
const token = process.env.TOKEN;
const clientId = process.env.CLIENT_ID;
const guildId = process.env.GUILD_ID;
const PASSWD = process.env.PASSWD
const path = require('path');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
module.exports = {
getAccountAndPassword: () => ({ username, newpassword, account, password}),
};
const events = require('events');
const eventEmitter = new events.EventEmitter();
module.exports = eventEmitter;
const commands = [
{
name: 'sudo',
description: 'makes an account with sudo (only admin use)',
options: [
{
name: 'account',
description: 'The name of the account',
type: 3,
required: true,
},
{
name: 'password',
description: 'The password of the account',
type: 3,
required: true,
},
{
name: 'accountid',
description: 'The Discord ID of the user',
type: 3,
required: true,
},
],
},
{
name: 'nonsudo',
description: 'makes your account on the vm',
options: [
{
name: 'account',
type: 3,
description: 'The name of the account',
required: true,
},
{
name: 'password',
type: 3,
description: 'The password of the account',
required: true,
},
],
},
{
name: 'chngpasswd',
description: 'Changes your password',
options: [
{
name: 'newpassword',
type: 3,
description: 'New password',
required: true,
},
],
},
{
name: 'stopvm',
description: 'Stops the VM',
},
{
name: 'execute',
description: 'Executes a command',
options: [
{
name: 'command',
type: 3,
description: 'The command to execute',
required: true,
},
],
},
{
name: 'announcement',
description: 'Announces a message',
options: [
{
name: 'message',
type: 3,
description: 'The message to announce',
required: true,
},
],
},
{
name: 'gpt',
description: 'Generates text using GPT-4-turbo',
options: [
{
name: 'message',
type: 3,
description: 'The message to generate text from',
required: true,
},
],
}
];
process.on('unhandledRejection', (reason, promise) => {
console.log('Unhandled Rejection at:', promise, 'reason:', reason);
});
process.on('uncaughtException', (err, origin) => {
console.log('Caught exception: ', err, 'Exception origin: ', origin);
});
if (!token || !clientId || !guildId) {
console.error('Missing environment variables. Please ensure that TOKEN, CLIENT_ID, and GUILD_ID are set.');
process.exit(1);
}
program
.command('execute')
.description('Execute a command')
.action(() => {
console.log('Command executed!');
});
client.once('ready', () => {
console.log('Ready!');
});
const { exec, execSync } = require('child_process');
let account, password;
function readUserIds() {
if (fs.existsSync('userids.json')) {
const data = fs.readFileSync('userids.json', 'utf-8');
return JSON.parse(data);
} else {
return {};
}
}
function writeUserIds(userIds) {
const data = JSON.stringify(userIds);
fs.writeFileSync('userids.json', data, 'utf-8');
}
client.on('interactionCreate', async interaction => {
if (interaction.isCommand()) {
process.env.ACCOUNT = interaction.options.getString('account');
process.env.PASSWORD = interaction.options.getString('password');
console.log(account);
eventEmitter.emit('interactionCreated', { account, password,});
console.log('Emitting interactionCreated event');
if (interaction.commandName === 'sudo') {
const requiredRoleId = process.env.ROLE_ID;
let discID = interaction.options.getString('accountid');
discID = discID.replace(/<@!?(\d+)>/, '$1');
await interaction.deferReply({ ephemeral: true });
if (!interaction.member.roles.cache.has(requiredRoleId)) {
try {
await interaction.editReply({ content: 'You do not have the required role to use this command.' });
} catch (error) {
console.error(`Failed to send reply: ${error}`);
}
return;
}
console.log('Running sudo.js');
exec(`node sudo.js ${process.env.ACCOUNT} ${process.env.PASSWORD}`, async (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
await interaction.followUp({ content: `sudo.js script executed! Output: ${stdout}`, ephemeral: true });
let userIds;
try {
userIds = JSON.parse(fs.readFileSync('userids.json', 'utf-8'));
} catch (error) {
userIds = {};
}
userIds[discID] = process.env.ACCOUNT;
writeUserIds(userIds);
});
}
} if (interaction.commandName === 'nonsudo') {
const userIds = readUserIds();
process.env.ACCOUNT = interaction.options.getString('account');
process.env.PASSWORD = interaction.options.getString('password');
const discordId = interaction.user.id;
if (Object.keys(userIds).includes(discordId)) {
await interaction.reply({ content: 'You can only use the nonsudo command once.', ephemeral: true });
return;
}
console.log(process.env.ACCOUNT);
await interaction.deferReply({ ephemeral: true });
exec(`node nonsudo.js ${process.env.ACCOUNT} ${process.env.PASSWORD}`, async (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
await interaction.followUp({ content: `nonsudo.js script executed! Output: ${stdout}`, ephemeral: true });
userIds[discordId] = process.env.ACCOUNT;
writeUserIds(userIds);
});
}
else if (interaction.commandName === 'chngpasswd') {
process.env.NEWPASSWORD = interaction.options.getString('newpassword');
const discordId = interaction.user.id;
const userids = JSON.parse(fs.readFileSync(path.join(__dirname, 'userids.json'), 'utf-8'));
await interaction.deferReply({ ephemeral: true });
username = userids[discordId];
if (!username) {
await interaction.followUp({ content: 'No account associated with your Discord ID.', ephemeral: true });
return;
}
try {
exec(`node chngpasswd.js ${username} ${process.env.NEWPASSWORD }`);
} catch (error) {
console.error(`chngpasswd.js error: ${error}`);
await interaction.followUp({ content: 'Failed to change password.', ephemeral: true });
}
await interaction.followUp({ content: 'Password changed successfully!', ephemeral: true });
}else if (interaction.commandName === 'stopvm') {
await interaction.deferReply();
const requiredRoleId = process.env.ROLE_ID;
if (!interaction.member.roles.cache.has(requiredRoleId)) {
try {
await interaction.editReply({ content: 'You do not have the required role to use this command.' });
} catch (error) {
console.error(`Failed to send reply: ${error}`);
}
return;
}
try {
const { stdout, stderr } = execSync(`echo ${passwd} | sudo -S /usr/sbin/shutdown -h now`, { stdio: 'pipe' });
interaction.followUp({ content: `Command executed: /usr/sbin/shutdown -h +1\nOutput: ${stdout}\nError: ${stderr}`, ephemeral: false });
} catch (error) {
console.error(`exec error: ${error}`);
interaction.followUp({ content: `An error occurred while executing the command: ${error.message}`, ephemeral: false });
return;
}
await interaction.followUp({ content: 'vm will stop now', ephemeral: false });
return;
}
else if (interaction.commandName === 'execute') {
const requiredRoleId = process.env.ROLE_ID;
const command = interaction.options.getString('command');
await interaction.deferReply();
if (!interaction.member.roles.cache.has(requiredRoleId)) {
try {
await interaction.editReply({ content: 'You do not have the required role to use this command.' });
} catch (error) {
console.error(`Failed to send reply: ${error}`);
}
return;
}
try {
const stdout = execSync(`echo ${PASSWD} | sudo -S ${command}`);
const output = stdout.toString();
console.log(`stdout: ${output}`);
interaction.followUp({ content: `Command executed: ${command}\nOutput: ${output}`, ephemeral: true });
return;
} catch (error) {
console.error(`exec error: ${error}`);
interaction.followUp({ content: `An error occurred while executing the command: ${error.message}`, ephemeral: true });
return;
}
}
else if (interaction.commandName === 'announcement') {
const requiredRoleId = process.env.ROLE_ID;
const message = interaction.options.getString('message');
await interaction.deferReply();
if (interaction.member.roles.cache.has(requiredRoleId)) {
exec(`echo ${PASSWD} | sudo -S node announcement.js -n ${message}`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
interaction.followUp({ content: `Announcement made! Output: ${stdout}`, ephemeral: true });
});
}
}
else if (interaction.commandName === 'gpt') {
const message = interaction.options.getString('message').replace(/"/g, '\\"');
const blocklist = ['<@&1203130746538041344>', '@', 'test'];
const containsBlockedWord = blocklist.some(blockedWord => message.toLowerCase().includes(blockedWord));
if (containsBlockedWord) {
await interaction.reply({ content: "Your message contains a word that is not allowed.", ephemeral: true });
return;
}
await interaction.deferReply();
exec(`node gpt.js "${message}"`, async (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
await interaction.followUp({ content: "There was an error processing your request.", ephemeral: true });
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
}
console.log(`Received stdout: ${stdout}`);
const lines = stdout.split('\n');
let capturing = false;
let capturedData = '';
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
console.log(`Processing line: ${line}`);
if (line.match(/^\s*data:\s*['"]/)) {
capturing = true;
continue;
}
if (capturing) {
if (i === lines.length - 1 && line.match(/^\s*}\s*$/)) {
break;
}
capturedData += line + '\n';
}
}
if (capturedData) {
console.log(`Captured data: ${capturedData}`);
await interaction.followUp({ content: capturedData.trim(), ephemeral: true });
} else {
console.log("No valid data found in response.");
await interaction.followUp({ content: "No valid data found in response.", ephemeral: true });
}
});
}
else {
interaction.followUp({ content: "Failed womp", ephemeral: true });
}
});
client.login(token);
const rest = new REST({ version: '9' }).setToken(token);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();