-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
200 lines (183 loc) · 6.54 KB
/
index.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
const token = 'YOUR_DISCORD_BOT_TOKEN';
const { Client, Intents, MessageAttachment } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES], partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });
const { exec, spawn } = require("child_process");
const fs = require('fs');
const { setRelays, fetch } = require('fetch-relay');
const fsExtra = require('fs-extra')
const path = require('path');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const axios = require('axios');
const tempPath = path.resolve(__dirname, "./temp");
const util = require('util');
setRelays([]);
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
fsExtra.emptyDirSync(tempPath);
});
function sendEval(message, result){ // A function that sends the result in different ways depending on the length of the result
try{
if(clean(result).length > 1020){
message.reply({ files: [new MessageAttachment(Buffer.from(clean(result), 'utf-8'), 'eval.txt')]})
}else {
message.reply('```\n'+clean(result)+'```')
}
}catch(e){
message.reply('```'+clean(e)+'```')
}
}
function clean(text) { // prevent token leak
if (typeof(text) !== 'string') {
text = util.inspect(text, { depth: 0 });
}
text = text.split(token).join('TOKEN');
return text;
}
client.on('messageCreate', async (message) => {
if(message.author.bot) return;
if(message.channel.id != 'CHANNEL_ID_FOR_NODEJS')return;
try {
let filename = `${message.id}`;
const messageContent = message.content;
var fileContent = message.attachments.first()?.url;
if(fileContent) {
const { data } = await axios.get(fileContent, { responseType: 'arraybuffer' });
fileContent = data;
}else { fileContent = ""; }
try {
fs.writeFileSync(`${tempPath}/${filename}.js`, `${messageContent} ${fileContent}`); // create a temporary file
} catch (e) { }
exec(`node temp/${filename}.js`, (error, stdout, stderr) => { // run the file
if (stderr) {
sendEval(message, stderr);
}else if (error) {
sendEval(message, error);
}else {
sendEval(message, stdout);
}
try {
fs.unlinkSync(`${tempPath}/${filename}.js`);
} catch (e) { }
});
}catch(e) {}
})
client.on('messageCreate', async (message) => {
if(message.author.bot) return;
if(message.channel.id != 'CHANNEL_ID_FOR_PYTHON')return;
try {
let filename = `${message.id}`;
const messageContent = message.content;
var fileContent = message.attachments.first()?.url;
if(fileContent) {
const { data } = await axios.get(fileContent, { responseType: 'arraybuffer' });
fileContent = data;
}else { fileContent = ""; }
try {
fs.writeFileSync(`${tempPath}/${filename}.py`, `${messageContent} ${fileContent}`);
} catch (e) { }
exec(`python3 temp/${filename}.py`, (error, stdout, stderr) => {
if (stderr) {
sendEval(message, stderr);
}else if (error) {
sendEval(message, error);
}else {
sendEval(message, stdout);
}
try {
fs.unlinkSync(`${tempPath}/${filename}.py`);
} catch (e) { }
});
}catch(e) {}
})
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
try {
await interaction.deferReply();
if (interaction.commandName === 'run') { // run a command directly on the terminal
const content = interaction.options.getString('command');
if(!content) return;
var command = content.split(' ')[0];
var args = content.split(' ').slice(1);
if(command.startsWith('node') || command.includes('kill')) return interaction.editReply('Access Denied');
const result = await runCommand(command, args);
sendShell(interaction, result);
await sendShell(interaction, await runCommand(command, args))
}else if(interaction.commandName === 'get'){ // send a get request to a url
const url = interaction.options.getString('url');
if(!url) return;
const { data } = await axios.get(url);
await sendShell(interaction, data)
}
}catch(e) {
console.log(e);
}
});
function runCommand(command, args) {
const spawned = spawn(command, args);
const log = [];
return new Promise((resolve) => {
if(command == 'node') return resolve('Access Denied');
spawned.stdout.on('data', (data) => {
log.push(data.toString());
});
spawned.stderr.on('data', (data) => {
log.push(data.toString());
});
spawned.on('error', (err) => {
log.push(err.toString());
})
spawned.on('close', () => {
resolve(log.join('\n'));
});
});
}
async function sendShell(interaction, result){
try{
if(clean(result).length > 1020){
await interaction.editReply({ files: [new MessageAttachment(Buffer.from(clean(result), 'utf-8'), 'shell.txt')]})
}else {
await interaction.editReply('```\n'+clean(result)+'```')
}
}catch(e){
await interaction.editReply('```'+clean(e)+'```')
}
}
process.on('uncaughtException', (err) => {
console.log(err)
});
process.on('unhandledRejection', err => {
console.log(err)
});
function deploy(client) {
const { SlashCommandBuilder } = require('@discordjs/builders')
const runCommand =
new SlashCommandBuilder()
.setName('run')
.setDescription('Run a command')
.addStringOption(option =>
option.setName('command')
.setDescription('The command to run')
.setRequired(true))
.toJSON();
const getCommand =
new SlashCommandBuilder()
.setName('get')
.setDescription('Send a get request')
.addStringOption(option =>
option.setName('url')
.setDescription('The url to send a get request to')
.setRequired(true))
.toJSON();
const rest = new REST({ version: '10' }).setToken(token);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(Routes.applicationCommands(client.user.id), { body: [runCommand, getCommand] }); //register commands
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
}
client.login(token);