defer to led server to provide functions

This commit is contained in:
jane 2021-05-27 14:56:26 -04:00
parent 1e30598bc4
commit 03bbf03279
6 changed files with 183 additions and 157 deletions

5
bot.js
View file

@ -1,4 +1,6 @@
import { createRequire } from 'module';
import { discover } from './lights/light_parser.js';
import { contactServer } from './lights/request.js';
const require = createRequire(import.meta.url);
import Eris from 'eris';
@ -103,3 +105,6 @@ let cmd_dir = path.join(dir, 'cmd');
log.debug(dir);
let files = fs.readdirSync(cmd_dir);
load_commands();
contactServer(discover, (err) => {
log.error(err);
})

View file

@ -1,5 +1,5 @@
import { CommandInitializer, Command } from '../parser.js';
import parse, { initialize, instructions } from '../lights/light_parser.js';
import parse, { discover, initialize, instructions } from '../lights/light_parser.js';
import req from '../lights/request.js';
const initializer = new CommandInitializer();
@ -34,6 +34,7 @@ class LightsParser extends Command {
(response) => {
msg.channel.createMessage(`${response}`);
},
discover,
(error) => {
msg.channel.createMessage(`error: ${error.message}`);
}

View file

@ -1,7 +1,7 @@
import Logger, { levels } from '../logger.js';
import { filename } from '../utils.js';
const log = new Logger(filename(import.meta.url), global.ctx.log_level);
const log = new Logger(filename(import.meta.url), "DEBUG");
class Target {
constructor(channel) {
@ -58,17 +58,6 @@ class Instruction {
}
export const instructions = [
{ n: 'CONSTANT', a: true },
{ n: 'ADD', a: true },
{ n: 'SUBTRACT', a: true },
{ n: 'MULTIPLY', a: true },
{ n: 'DIVIDE', a: true },
{ n: 'MODULO', a: true },
{ n: 'FADE', a: true },
{ n: 'RANDOM', a: false },
{ n: 'JMP', a: false },
{ n: 'JNZ', a: true },
{ n: 'JEZ', a: true },
];
export function initialize(ctx) {
@ -156,6 +145,18 @@ function parseSingleInstruction(str = '') {
return item;
}
export function discover(data) {
for (let value of data) {
if (value.n != undefined) {
let found = instructions.filter((val) => { return val.n == value.n });
if (!found.length) {
log.debug(`discovered new keyword ${value.n}`);
instructions.push(value);
}
}
}
}
export default function parse(str) {
let parsed = [];
let split = str.split('\n').filter((s) => s.length > 0);

View file

@ -7,7 +7,22 @@ function getBinarySize(string) {
return Buffer.byteLength(string, 'utf8');
}
export default function req(data, callback, errorCallback) {
export function contactServer(callback, errorCallback) {
let client = new net.Socket();
let c_port = global.ctx.lights.port || port;
let c_addr = global.ctx.lights.addr || hostname;
client.connect(c_port, c_addr, () => {
});
client.on('data', (dat) => {
let arr = JSON.parse(dat);
callback(arr);
client.destroy();
})
}
export default function req(data, callback, newFuncCallback, errorCallback) {
let string_data = JSON.stringify(data);
let size = getBinarySize(string_data);
if (size > 9999) {
@ -19,11 +34,15 @@ export default function req(data, callback, errorCallback) {
let c_port = global.ctx.lights.port || port;
let c_addr = global.ctx.lights.addr || hostname;
client.connect(c_port, c_addr, () => {
client.connect(c_port, c_addr, () => { });
client.on('data', (dat) => {
let arr = JSON.parse(dat);
newFuncCallback(arr);
client.write(string_data);
client.destroy();
callback('success.');
});
})
client.on('error', (e) => {
client.destroy();

View file

@ -91,10 +91,6 @@ export default class CommandParser {
if (msg.author.bot) {
return;
}
if (msg.channel.guild && !ctx.whitelist.guild(msg.channel.guild)) {
msg.channel.createMessage('guild not whitelisted');
return;
}
this.log.debug(msg.content);
this.log.debug(msg.content.startsWith(this.prefix));
this.log.debug(msg.content[0]);
@ -109,6 +105,10 @@ export default class CommandParser {
this.log.debug(args);
if (res != undefined) {
if (msg.channel.guild && !ctx.whitelist.guild(msg.channel.guild)) {
msg.channel.createMessage('guild not whitelisted');
return;
}
this.log.debug(`execute function ${res.name}`);
if (res.whitelist && !ctx.whitelist.user(msg.author)) {
msg.channel.createMessage('not whitelisted');

View file

@ -1 +1 @@
{"guild":["656579275008245830","754155880173404260"],"user":["123601647258697730","240240073386229760","285424490916216832","529534860243632134"]}
{"guild":["656579275008245830","754155880173404260","805978396974514206"],"user":["123601647258697730","240240073386229760","285424490916216832","529534860243632134"]}