-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (135 loc) · 4.74 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
const { Client, Collection, ActivityType, IntentsBitField, Partials, GatewayIntentBits } = require('discord.js')
const path = require('path')
const fs = require('fs')
const fse = require('fs-extra')
const childproc = require('child_process')
const client = new Client({
intents: [
Object.values(IntentsBitField.Flags),
Object.values(GatewayIntentBits)
],
partials: [Object.values(Partials)]
})
client.login(process.env.TOKEN)
client.commands = new Collection()
let cache = path.join(__dirname, 'cache')
require('./corehandler.js')()
let cores = require('./cores.json')
client.on('ready', () => {
require('./commandhandler.js')(client)
console.log(client.user.tag + ' online!')
client.user.setPresence({
status: 'idle',
activities: [{
name: 'Hosting your code!',
type: ActivityType.Custom
}]
})
reloadProcesses()
client.users.cache.forEach(async (user) => {
if (!(await isExists(path.join(cache, user.id)))) return await fse.mkdir(path.join(cache, user.id))
})
})
function rm(file, args) {
return childproc.exec('rm ' + args + ' ' + file)
}
function isExists(file) {
return new Promise(resolve => {
fs.access(file, fs.constants.F_OK | fs.constants.R_OK | fs.constants.W_OK, (err) => {
if (err) return resolve(false)
return resolve(true)
})
})
}
function onlyAccess(filepath, gid, uid) {
return new Promise(async (resolve) => {
if (!(await isExists(filepath))) return resolve()
fs.open(filepath, 'w', 0o0640, (err, fd) => {
if (err) {
console.error(`Failed to open file: ${err}`);
return resolve();
}
fs.fchown(fd, uid, gid, (err) => {
if (err) {
console.error(`Failed to change owner/group of file: ${err}`);
}
fs.close(fd, (err) => {
if (err) {
console.error(`Failed to close file: ${err}`);
}
resolve()
});
});
});
})
}
function reloadProcesses() {
fs.readdirSync(cache).forEach(userCache => {
fs.readdirSync(path.join(cache, userCache)).forEach(async (processId) => {
let info = path.join(cache, userCache, processId, 'info.json')
let entryPoint = info.run ? path.join(cache, userCache, processId, info.run) : undefined || path.join(cache, userCache, processId, 'none')
let core = info.core || 'nodejs'
let coreinfo = cores[core]
let cwd = path.join(cache, userCache, processId)
/*let linux = info.linux*/
if (!coreinfo) {
let hoster = await client.users.fetch(userCache)
hoster.send('for you process ' + processId + ': we cannot found a core named ' + core + '! we will remove this process from our database')
rm(path.join(cache, userCache, processId), '-rf')
return
}
let runthis = coreinfo.cmd.replaceAll('{FILE}', entryPoint).replaceAll('{ARGS}', info.args)
let hosted = cmd(runthis, false, { cwd: cwd })
hosted.stdout.on('data', async (chunk) => {
let hoster = await client.users.fetch(userCache)
let format = `${info.name}@${info.id}: ${chunk.toString()}`
hoster.send(format)
})
hosted.stderr.on('data', async (chunk) => {
let hoster = await client.users.fetch(userCache)
let format = `${info.name}@${info.id}: err=> ${chunk.toString()}`
hoster.send(format)
})
hosted.on('exit', async (chunk) => {
let hoster = await client.users.fetch(userCache)
let format = `${info.name}@${info.id}: process exited!`
rm(path.join(cache, userCache, processId), '-rf')
hoster.send(format)
})
})
})
}
function cmd(_cmd, promise, opts) {
let splitted = _cmd.split(' ')
let cmd = splitted[0]
let args = _cmd.replaceAll(`${cmd} `, '').split(' ')
let proc = childproc.spawn(cmd, args, opts)
return promise ? new Promise(resolve => {
let opterr = []
let optlog = []
proc.stdout.on('data', (chunk) => {
optlog.push(chunk.toString())
})
proc.stderr.on('data', (chunk) => {
opterr.push(chunk.toString())
})
proc.on('exit', () => {
resolve({ err: opterr, log: optlog })
})
}) : proc
}
client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand()) return
if (!(await isExists(path.join(cache, interaction.user.id)))) return await fse.mkdir(path.join(cache, interaction.user.id))
const command = client.commands.get(interaction.commandName)
if (!command) {
interaction.reply(`No command matching ${interaction.commandName} was found.`, { ephemeral: true });
return
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
})