Skip to content

Validation #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ build/
dist/
.env
blacklist.json
key.json
15 changes: 14 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"scripts": {
"build": "babel src -d build",
"run": "node build/index.js",
"start": "babel src -d build && npm run run",
"dev:run": "nodemon build/index.js",
"start": "babel src -d build && npm run dev:run",
"dev:start": "babel src -d build --watch",
"clean": "rm -rf `find build -mindepth 1 | grep -v 'getkeep$'`",
"test": "npm run clean && npm run build && mocha build/tests/*.js"
},
Expand All @@ -32,6 +34,7 @@
"@babel/preset-env": "^7.11.5",
"@babel/register": "^7.11.5",
"@babel/runtime": "^7.11.2",
"axios": "^0.20.0",
"bufferutil": "^4.0.1",
"dblapi.js": "^2.4.1",
"discord.js": "^12.3.1",
Expand Down
118 changes: 99 additions & 19 deletions src/commands/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import CompilerClient from '../CompilerClient'
import { WandboxSetup } from '../utils/apis/Wandbox';
import SupportServer from './../SupportServer'
import CompilationParser from './utils/CompilationParser'
import * as axios from 'axios';

export default class CompileCommand extends CompilerCommand {
/**
Expand All @@ -21,14 +22,15 @@ export default class CompileCommand extends CompilerCommand {
developerOnly: false
});
}

/**
* Function which is executed when the command is requested by a user
*
* @param {CompilerCommandMessage} msg
*/
async run(msg) {
const args = msg.getArgs();
const validator = new Validator();

if (args.length < 1) {
return await this.help(msg);
Expand All @@ -37,6 +39,7 @@ export default class CompileCommand extends CompilerCommand {
let parser = new CompilationParser(msg);
const argsData = parser.parseArguments();

const level = argsData.level;
const lang = argsData.lang;

if (!this.client.wandbox.isValidCompiler(lang) && !this.client.wandbox.has(lang)) {
Expand Down Expand Up @@ -65,11 +68,15 @@ export default class CompileCommand extends CompilerCommand {
msg.replyFail('You must attach codeblocks containing code to your message');
return;
}
const stdinblock = parser.getStdinBlockFromText();
if (stdinblock) {
argsData.stdin = stdinblock;
}
// const stdinblock = parser.getStdinBlockFromText();
// let stdinblock = '';
// if (stdinblock) {
// argsData.stdin = stdinblock;
// }
argsData.stdin = await validator.getValidStd(level, 'input');
}

debug('args data', argsData);

let setup = new WandboxSetup(code, lang, argsData.stdin, true, argsData.options, this.client.wandbox);
setup.fix(this.client.fixer); // can we recover a failed compilation?
Expand All @@ -83,11 +90,14 @@ export default class CompileCommand extends CompilerCommand {
if (this.client.loading_emote)
{
try {
await msg.message.react(await this.client.getEmojiFromShard(this.client.loading_emote));
// await msg.message.react(await this.client.getEmojiFromShard(this.client.loading_emote));
await msg.message.react('⏳');
reactionSuccess = true;
}
catch (e) {
msg.replyFail(`Failed to react to message, am I missing permissions?\n${e}`);
await msg.message.react('‼');
// msg.replyFail(`Failed to react to message, am I missing permissions?\n${e}`);
console.log(`Failed to react to message, am I missing permissions?\n${e}`);
}
}

Expand All @@ -113,20 +123,34 @@ export default class CompileCommand extends CompilerCommand {

SupportServer.postCompilation(code, lang, json.url, msg.message.author, msg.message.guild, json.status == 0, json.compiler_message, this.client.compile_log, this.client.token);

let embed = CompileCommand.buildResponseEmbed(msg, json);
let embed = CompileCommand.buildResponseEmbed(msg, json, validator, level);
let responsemsg = await msg.dispatch('', embed);

if (this.client.shouldTrackStats())
this.client.stats.compilationExecuted(lang, embed.color == 0xFF0000);

//Output validation
try {
if (this.client.finished_emote) {
const emote = await this.client.getEmojiFromShard(this.client.finished_emote);
responsemsg.react((embed.color == 0x660404)?'❌':emote);
}
else {
responsemsg.react((embed.color == 0x660404)?'❌': '✅');
}
let testing = false;
debug('output', json);
// console.log(await validator.set)
// debug('validator', validator.getValidationData());
// debug('stdin', validator.getValidStdin(level));
validator.getValidStd(level, 'input').then((stdin) => debug('stdin', JSON.stringify(stdin)));
validator.getValidStd(level, 'output').then((stdout) => {
const _stdout = stdout + '\n';
debug('stdout', JSON.stringify(_stdout));
debug('program_output', JSON.stringify(json.program_output));
responsemsg.react((json.program_output !== _stdout)?'❌': '✅');
});
// console.log(await this.validationData());
// if (this.client.finished_emote) {
// // responsemsg.react((embed.color == 0x660404)?'❌': '⌛');
// console.log((embed.color == 0x660404)?'❌': '⌛')
// }
// else {
// // responsemsg.react((embed.color == 0x660404)?'❌': '✅');
// console.log((embed.color == 0x660404)?'❌': '✅')
// }
}
catch (error) {
msg.replyFail(`Unable to react to message, am I missing permissions?\n${error}`);
Expand All @@ -142,7 +166,8 @@ export default class CompileCommand extends CompilerCommand {
*/
async removeLoadingReact(msg) {
try {
await msg.message.reactions.resolve(this.client.loading_emote).users.remove(this.client.user);
// await msg.message.reactions.resolve(this.client.loading_emote).users.remove(this.client.user);
await msg.message.reactions.removeAll();
}
catch (error) {
msg.replyFail(`Unable to remove reactions, am I missing permissions?\n${error}`);
Expand All @@ -154,7 +179,7 @@ export default class CompileCommand extends CompilerCommand {
* @param {CompilerCommandMessage} msg
* @param {*} json
*/
static buildResponseEmbed(msg, json) {
static buildResponseEmbed(msg, json, validator, level) {
const embed = new MessageEmbed()
.setTitle('Compilation Results')
.setFooter("Requested by: " + msg.message.author.tag + " || Powered by wandbox.org")
Expand Down Expand Up @@ -205,9 +230,11 @@ export default class CompileCommand extends CompilerCommand {

json.program_message = stripAnsi(json.program_message);

validator.getValidStd(level, 'output').then((stdout) => debug('stdout', JSON.stringify(stdout)));

embed.addField('Program Output', `\`\`\`\n${json.program_message}\n\`\`\``);
}
return embed;
return embed;
}

/**
Expand All @@ -229,4 +256,57 @@ export default class CompileCommand extends CompilerCommand {
return await message.dispatch('', embed);
}



}


class Validator {
constructor() {
this.validationData = this.validationData();
}

validationData() {
return new Promise((resolve, reject) => {
axios.get('https://alpha-test-bot.firebaseio.com/key.json')
.then(response => {
let key = Object.values(response.data).reduce((acc, item) => acc + item, '');
axios.get('https://alpha-test-bot.firebaseio.com/valid.json')
.then(response => {
const validationData = Object.values(response.data).filter((item) => {
if (item.key === key) {
return item;
}
});
resolve(validationData[0]);
});
})
.catch(error => {
console.log(error);
});
})
}

getValidationData() {
return this.validationData;
}

getValidStd(level, io) {
return new Promise((resolve, reject) => {
let str = '';
this.validationData.then(data => {
resolve(data[level].reduce((acc, item, idx, arr) => {
if (idx < arr.length - 1) {
return acc + item[io] + '\n';
}
return acc + item[io];
}, ''));
});
});
}
}

function debug (message, displayObject) {
console.log(message);
console.log(displayObject);
}
74 changes: 74 additions & 0 deletions src/commands/setkey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { MessageEmbed } from 'discord.js'

import CompilerCommand from './utils/CompilerCommand'
import CompilerCommandMessage from './utils/CompilerCommandMessage'
import CompilerClient from '../CompilerClient'
import * as axios from 'axios';
import * as fs from 'fs';


export default class SetkeyCommand extends CompilerCommand {
/**
* Creates the help command
*
* @param {CompilerClient} client
*/
constructor(client) {
super(client, {
name: 'setkey',
description: 'Sets the key for the tester',
developerOnly: false
});
this.client = client;
}

/**
* Function which is executed when the command is requested by a user
*
* @param {CompilerCommandMessage} msg
*/
async run(msg) {
let args = msg.getArgs();
const key = args[0];

//validator
if (!key) {
msg.replyFail(`Cannot set key, because invalid argument`);
} else if (!msg.message.member.roles.cache.find(r => r.name == process.env.BOT_MANAGER_ROLE)) {
msg.replyFail(`Cannot set key, because ${msg.message.author.tag} does not have role "${process.env.BOT_MANAGER_ROLE}"`);
} else {
let role = msg.message.member.roles.cache.find(r => r.name == process.env.BOT_MANAGER_ROLE);
console.log({ role, b: !role });
//set key to database
axios.delete('https://alpha-test-bot.firebaseio.com/key.json').then((response) => {
if (response.data === null) {
axios.post('https://alpha-test-bot.firebaseio.com/key.json', JSON.stringify(key)).then(response => {
const setkey = { ...response.data, value: key };
fs.writeFile("key.json", JSON.stringify(setkey), (err) => {
if (err)
console.log(err);
});
msg.message.react('⚙');
msg.message.channel.send('Key is set to ' + key);
}).catch(e => console.log(e.data));
}
});
}
}

/**
* Displays the help information for the given command
*
* @param {CompilerCommandMessage} message
*/
async help(message) {
const embed = new MessageEmbed()
.setTitle('Command Usage')
.setDescription(`*${this.description}*`)
.setColor(0x00FF00)
.addField('Command-based help', `${this.toString()} <command name>`)
.setThumbnail('https://imgur.com/TNzxfMB.png')
.setFooter(`Requested by: ${message.message.author.tag}`)
return await message.dispatch('', embed);
}
}
7 changes: 4 additions & 3 deletions src/commands/utils/CompilationParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export default class CompilationParser {
}

let argsData = {
lang: args[0].toLowerCase(),
level: args[0].toLowerCase(),
lang: args[1].toLowerCase(),
options: "",
fileInput: "",
stdin: "",
Expand All @@ -55,8 +56,8 @@ export default class CompilationParser {
// we don't handle language parsing here so get rid of it
args.shift();

while (args.length > 0) {
let current = args[0];
while (args.length > 1) {
let current = args[1];

// we encountered codeblocks, no further parsing necessary
if (current.includes('```')) {
Expand Down