forked from funnbot/Nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunnfuck.js
More file actions
300 lines (233 loc) · 7.72 KB
/
Copy pathfunnfuck.js
File metadata and controls
300 lines (233 loc) · 7.72 KB
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
let Discord = require('discord.js')
exports.run = (message, bot) => {
let NLI = new Interpreter(message.suffix, message)
NLI.interpret()
}
exports.conf = {
userPerm: [],
botPerm: ["SEND_MESSAGES"],
coolDown: 0,
dm: true,
category: "Fun",
help: "A Wierd brainfuck derivative",
args: "",
}
class Interpreter {
constructor(rawInput, message) {
this.rawInput = rawInput
this.code = this.rawInput.replace(/[^\+\<\>\.\-\,\[\]\#\;\:\$\&]/g, "")
this.codeSplit
this.codeCursor = 0
this.memCursor = 0
this.message = message
this.input = ""
this.memory = Array.from({
length: 255
}, () => 0)
this.output = ""
this.infiniteCheck
this.collector
this.quitC
this.loop = true
this.commands = {
"4": (cell) => {
if (cell != 0) this.codeCursor++
else this.codeCursor = this.checkRight("4", "5")
},
"5": (cell) => {
this.codeCursor = this.checkLeft("4", "5")
},
"6": (cell) => {
if (cell != 0) this.codeCursor++
else this.checkRight("6", "7")
},
"2": (cell) => {
this.memCursor++
if (this.memCursor > 255) return this.end(), this.message.channel.send("`MemoryError: Out Of Memory`")
this.codeCursor++
},
"3": (cell) => {
this.memCursor--
if (this.memCursor < 0) return this.end(), this.message.channel.send("`MemoryError: Negative Memory Does Not Exist`")
this.codeCursor++
},
"0": (cell) => {
this.memory[this.memCursor] = cell + 1
if (this.memory[this.memCursor] > 65535) this.memory[this.memCursor] = 0
this.codeCursor++
},
"1": (cell) => {
this.memory[this.memCursor] = cell - 1
if (this.memory[this.memCursor] < 0) this.memory[this.memCursor] = 65535
this.codeCursor++
},
"8": (cell) => {
this.output += String.fromCharCode(cell)
this.codeCursor++
},
"9": (cell) => {
this.output += cell
this.codeCursor++
},
"A": async (cell) => {
let char = await this._getInput("A")
this.memory[this.memCursor] = char
this.codeCursor++
},
"B": async (cell) => {
let char = await this._getInput("B")
if (char > 65535) char = 65535
this.memory[this.memCursor] = char
this.codeCursor++
},
"C": (cell) => {
this.end()
},
"D": (cell) => {
this.memory[this.memCursor] = cell * cell
this.codeCursor++
},
"E": (cell) => {
cell = Math.sqrt(cell)
this.memory[this.memCursor] = Math.round(cell)
this.codeCursor++
}
}
}
async interpret() {
let Comp = new Compiler(this.code)
this.codeSplit = Comp.compile()
try {
await this.linter()
} catch (e) {
return this.message.channel.send(e)
}
this.forceQuit()
this.infinity()
await this.delay()
while (this.loop) {
await this.delay()
if (this.codeCursor >= this.codeSplit.length) {
return this.end()
}
let input = this.codeSplit[this.codeCursor]
let cell = this.memory[this.memCursor]
await this.commands[input](cell)
}
}
end() {
this.loop = false
clearTimeout(this.infiniteCheck)
if (this.collector && !this.collector.ended) this.collector.stop('end of the line')
if (this.quitC && !this.quitC.ended) this.quitC.stop('end of the line')
this.message.channel.send('**Output:** ' + this.output)
}
forceQuit() {
this.message.channel.send("**Evaluating Code**\nType `quit` to quit.")
this.quitC = new Discord.MessageCollector(this.message.channel, (m) => m.author.id === this.message.author.id)
this.quitC.on('message', (m) => {
if (m.content.includes('quit')) {
this.message.channel.send('**Force Quitting**').then(() => {
console.log("?")
this.end()
})
}
})
}
linter() {
return new Promise((resolve, reject) => {
let code = this.code
let leftB = (code.match(/4/g) || []).length
let rightB = (code.match(/5/g) || []).length
let leftP = (code.match(/6/g) || []).length
let rightP = (code.match(/7/g) || []).length
if (leftB !== rightB) return reject('**Mismatched Brackets**')
else if (leftP !== rightP) return reject('**Mismatched Parentheses')
else return resolve()
})
}
infinity() {
this.infiniteCheck = setTimeout(() => {
this.message.channel.send('**Infinite Loop Detected**').then(() => {
this.codeCursor = this.codeSplit.length + 1
})
}, 180000)
}
delay() {
return new Promise((resolve) => {
setTimeout(() => resolve(), 1)
})
}
_getInput(type) {
return new Promise((resolve, reject) => {
this.message.channel.send("**Awaiting Input...**")
this.collector = new Discord.MessageCollector(this.message.channel, (m) => m.author.id === this.message.author.id, {
time: 60000,
max: 1,
maxMatches: 1
})
this.collector.on('message', (m) => {
if (type === "A") {
let char = m.content.split('')[0]
let toUni = char.charCodeAt(0)
return resolve(toUni)
} else if (type === "B") {
let char = parseInt(m.content) || 0
return resolve(char)
}
})
this.collector.on('end', (c, r) => {
if (r === "time") {
return resolve(0)
}
})
})
}
checkRight(char1, char2) {
let counter = 0
for (let i = this.codeCursor + 1; i < this.codeSplit.length; i++) {
let char = this.codeSplit[i]
if (char === char1) counter++
else if (char === char2 && counter > 0) counter--
else if (char === char2) return i + 1
}
}
checkLeft(char1, char2) {
let counter = 0
for (let i = this.codeCursor - 1; i >= 1; i--) {
let char = this.codeSplit[i]
if (char === char2) counter++
else if (char === char1 && counter > 0) counter--
else if (char === char1) return i
}
}
}
class Compiler {
constructor(rawInput) {
this.rawInput = rawInput
this.output = []
this.table = {
"+":0,
"-":1,
">":2,
"<":3,
"[":4,
"]":5,
"(":6,
")":7,
".":8,
":":9,
",":"A",
";":"B",
"#":"C",
"$":"D",
"&":"E",
}
}
compile() {
this.rawInput.split('').forEach(i => {
this.output.push(this.table[i])
})
return this.output.join('')
}
}