-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
164 lines (161 loc) · 8.03 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
const spawn = require('child_process').spawn
const Discord = require('discord.js')
const bot = new Discord.Client({ intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES] })
bot.login(require('./cred.json').token)
let options = require('./options.json')
let stdInReplies = {}
let preventFromRunning = {}
bot.once('ready', () => {
console.log(`Ready! Username: ${bot.user.username}, Discriminator: ${bot.user.discriminator}`)
options.commands.forEach(command => {
if (command.type == 'message') {
if (command.filter.type == 'none') {
bot.on('messageCreate', message => {
if (message.type == 'REPLY') {
let std = stdInReplies[message.reference.messageId]
if (std) {
std.stdIn.setEncoding('utf8')
std.stdIn.pipe(std.stdOut)
std.stdIn.write(message.content)
}
} else if (message.content == command.trigger) {
runBatchFile(message, command)
}
})
} else if (command.filter.type == 'exclusive') {
bot.on('messageCreate', message => {
if (message.type == 'REPLY') {
let std = stdInReplies[message.reference.messageId]
if (std) {
std.stdIn.setEncoding('utf8')
std.stdIn.pipe(std.stdOut)
std.stdIn.write(message.content)
}
} else if (message.content == command.trigger) {
if (command.filter.users.includes(message.author.id)) {
runBatchFile(message, command)
} else {
message.reply('Sorry! You do not have permission to run that command!')
}
}
})
} else if (command.filter.type == 'exception') {
bot.on('messageCreate', message => {
if (message.type == 'REPLY') {
let std = stdInReplies[message.reference.messageId]
if (std) {
std.stdIn.setEncoding('utf8')
std.stdIn.pipe(std.stdOut)
std.stdIn.write(message.content)
}
} else if (message.content == command.trigger) {
if (!command.filter.users.includes(message.author.id)) {
runBatchFile(message, command)
} else {
message.reply('Sorry! You do not have permission to run that command!')
}
}
})
}
}
})
})
function runBatchFile(message, command) {
let batchFile = command.batchFile
batchFile = batchFile.replace('{{$username}}', message.author.username).replace('{{$discriminator}}', message.author.discriminator).replace('{{$userId}}', message.author.id)
if (!preventFromRunning[batchFile]) {
preventFromRunning[batchFile] = true
const process = spawn('cmd.exe', ['/c', batchFile], { detatched: true })
if (command.replies.replyOnSpawn) {
message.reply(command.replies.replyOnSpawn).then(message => {
let replyToMessage = message
if (command.replies.replyToStdin.includes('SPAWN')) {
stdInReplies[replyToMessage.id] = { stdIn: process.stdin, stdOut: process.stdout }
}
})
}
if (command.replies.replyOnFinish) {
process.on('exit', code => {
preventFromRunning[batchFile] = false
message.reply(command.replies.replyOnFinish.replace('{{$code}}', code)).then(message => {
let replyToMessage = message
if (command.replies.replyToStdin.includes('FINISH')) {
stdInReplies[replyToMessage.id] = { stdIn: process.stdin, stdOut: process.stdout }
}
})
})
} else {
process.on('exit', code => {
preventFromRunning[batchFile] = false
})
}
if (command.replies.replyOnError) {
process.on('error', err => {
message.reply(command.replies.replyOnError.replace('{{$error}}', err)).then(message => {
let replyToMessage = message
if (command.replies.replyToStdin.includes('ERROR')) {
stdInReplies[replyToMessage.id] = { stdIn: process.stdin, stdOut: process.stdout }
}
})
})
}
if (command.replies.replyOnStderr) {
process.stderr.on('data', data => {
message.reply(command.replies.replyOnStderr.replace('{{$stderr}}', data.toString('utf8'))).then(message => {
let replyToMessage = message
if (command.replies.replyToStdin.includes('STDERR')) {
stdInReplies[replyToMessage.id] = { stdIn: process.stdin, stdOut: process.stdout }
}
})
})
}
if (command.replies.replyOnStdout) {
process.stdout.on('data', data => {
message.reply(command.replies.replyOnStdout.replace('{{$stdout}}', data.toString('utf8'))).then(message => {
let replyToMessage = message
if (command.replies.replyToStdin.includes('STDOUT')) {
stdInReplies[replyToMessage.id] = { stdIn: process.stdin, stdOut: process.stdout }
}
})
})
}
} else {
message.reply('The command is already running. Wait for it to finish first!')
}
}
options.commands.forEach(command => {
if (typeof command.trigger !== 'string') {
console.log(`Command trigger needs to be a string for the command ${command.trigger}!`)
process.exit(1)
} else if (command.type !== 'message') {
console.log(`Command type needs to be "message" for the command ${command.trigger}!`)
process.exit(1)
} else if (command.filter.type !== 'exclusive' && command.filter.type !== 'exception' && command.filter.type !== 'none') {
console.log(`Filter type needs to be either "none", "exclusive" or "exception" for the command ${command.trigger}!`)
process.exit(1)
} else if (typeof command.filter.users !== 'object') {
console.log(`Filter.users needs to be an Array for the command ${command.trigger}!`)
process.exit(1)
} else if (typeof command.replies.replyOnSpawn !== 'string') {
console.log(`ReplyOnSpawn needs to be a string for the command ${command.trigger}!`)
process.exit(1)
} else if (typeof command.replies.replyOnFinish !== 'string') {
console.log(`ReplyOnFinish needs to be a string for the command ${command.trigger}!`)
process.exit(1)
} else if (typeof command.replies.replyOnError !== 'string') {
console.log(`ReplyOnError needs to be a string for the command ${command.trigger}!`)
process.exit(1)
} else if (typeof command.replies.replyOnStderr !== 'string') {
console.log(`ReplyOnStderr needs to be a string for the command ${command.trigger}!`)
process.exit(1)
} else if (typeof command.replies.replyOnStdout !== 'string') {
console.log(`ReplyOnStdout needs to be a string for the command ${command.trigger}!`)
process.exit(1)
} else if (typeof command.replies.replyToStdin !== 'object') {
console.log(`ReplyToStdin needs to be an Array for the command ${command.trigger}!`)
process.exit(1)
} else if (typeof command.preventFromSimultaneousExecution !== 'boolean') {
console.log(`PreventFromSimultaneousExecution needs to be a Boolean for the command ${command.trigger}!`)
process.exit(1)
}
})