new format args
This commit is contained in:
		
							parent
							
								
									0e827d1c85
								
							
						
					
					
						commit
						1e30598bc4
					
				
					 4 changed files with 212 additions and 218 deletions
				
			
		
							
								
								
									
										104
									
								
								bot.js
									
										
									
									
									
								
							
							
						
						
									
										104
									
								
								bot.js
									
										
									
									
									
								
							|  | @ -10,7 +10,7 @@ import fs from 'fs'; | ||||||
| 
 | 
 | ||||||
| const cfg = require('./config.json'); | const cfg = require('./config.json'); | ||||||
| if (!fs.existsSync('./whitelist.json')) { | if (!fs.existsSync('./whitelist.json')) { | ||||||
| 	fs.writeFileSync('./whitelist.json', '{}'); |   fs.writeFileSync('./whitelist.json', '{}'); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| const whitelist = require('./whitelist.json'); | const whitelist = require('./whitelist.json'); | ||||||
|  | @ -19,45 +19,49 @@ const dir = dirname(import.meta.url); | ||||||
| const bot = new Eris(cfg.token); | const bot = new Eris(cfg.token); | ||||||
| 
 | 
 | ||||||
| global.ctx = { | global.ctx = { | ||||||
| 	bot: bot, |   bot: bot, | ||||||
| 	log_level: levels[cfg.log_level.toUpperCase()] || levels.WARN, |   log_level: levels[cfg.log_level.toUpperCase()] || levels.WARN, | ||||||
| 	set_ctx: (key, value) => { |   set_ctx: (key, value) => { | ||||||
| 		global.ctx[key] = value; |     global.ctx[key] = value; | ||||||
| 	}, |   }, | ||||||
|  |   lights: { | ||||||
|  |     port: cfg.lights_port, | ||||||
|  |     addr: cfg.lights_address | ||||||
|  |   } | ||||||
| }; | }; | ||||||
| const log = new Logger(filename(import.meta.url), global.ctx.log_level); | const log = new Logger(filename(import.meta.url), global.ctx.log_level); | ||||||
| const parse = new CommandParser(global.ctx, cfg.prefix || undefined); | const parse = new CommandParser(global.ctx, cfg.prefix || undefined); | ||||||
| 
 | 
 | ||||||
| const checkUser = (user) => { | const checkUser = (user) => { | ||||||
| 	if (cfg.user_whitelist.includes(user.id) || (whitelist.user && whitelist.user.includes(user.id))) { |   if (cfg.user_whitelist.includes(user.id) || (whitelist.user && whitelist.user.includes(user.id))) { | ||||||
| 		return true; |     return true; | ||||||
| 	} |   } | ||||||
| 	log.info(`user not on whitelist: ${user.username}`); |   log.info(`user not on whitelist: ${user.username}`); | ||||||
| 	return false; |   return false; | ||||||
| }; | }; | ||||||
| const checkGuild = (guild) => { | const checkGuild = (guild) => { | ||||||
| 	if (cfg.whitelist.includes(guild.id) || (whitelist.guild && whitelist.guild.includes(guild.id))) { |   if (cfg.whitelist.includes(guild.id) || (whitelist.guild && whitelist.guild.includes(guild.id))) { | ||||||
| 		return true; |     return true; | ||||||
| 	} |   } | ||||||
| 	log.info(`guild not on whitelist: ${guild.name}`); |   log.info(`guild not on whitelist: ${guild.name}`); | ||||||
| 	return false; |   return false; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| const saveWhitelist = () => { | const saveWhitelist = () => { | ||||||
| 	let data = JSON.stringify(whitelist); |   let data = JSON.stringify(whitelist); | ||||||
| 	fs.writeFileSync('./whitelist.json', data); |   fs.writeFileSync('./whitelist.json', data); | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| global.ctx.whitelist = { | global.ctx.whitelist = { | ||||||
| 	guild: checkGuild, |   guild: checkGuild, | ||||||
| 	user: checkUser, |   user: checkUser, | ||||||
| 	save: saveWhitelist, |   save: saveWhitelist, | ||||||
| 	wl: whitelist, |   wl: whitelist, | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| bot.on('ready', () => { | bot.on('ready', () => { | ||||||
| 	log.info('ready recieved.'); |   log.info('ready recieved.'); | ||||||
| 	bot.guilds.forEach((guild) => checkGuild(guild)); |   bot.guilds.forEach((guild) => checkGuild(guild)); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| bot.on('messageCreate', (msg) => parse.parseMsg(msg, global.ctx)); | bot.on('messageCreate', (msg) => parse.parseMsg(msg, global.ctx)); | ||||||
|  | @ -67,32 +71,32 @@ bot.on('error', (err) => log.error(err.toString())); | ||||||
| bot.connect(); | bot.connect(); | ||||||
| 
 | 
 | ||||||
| async function load_commands() { | async function load_commands() { | ||||||
| 	for (let file of files) { |   for (let file of files) { | ||||||
| 		let p = path.join(cmd_dir, file); |     let p = path.join(cmd_dir, file); | ||||||
| 		log.debug(p); |     log.debug(p); | ||||||
| 		let obj; |     let obj; | ||||||
| 		try { |     try { | ||||||
| 			obj = await import('file:////' + p); |       obj = await import('file:////' + p); | ||||||
| 		} catch (e) { |     } catch (e) { | ||||||
| 			log.warn(`loading file ${file}, ran into issue: ${e.stack}`); |       log.warn(`loading file ${file}, ran into issue: ${e.stack}`); | ||||||
| 			continue; |       continue; | ||||||
| 		} |     } | ||||||
| 		if (obj.default != undefined) { |     if (obj.default != undefined) { | ||||||
| 			if (obj.default.constructor.name == 'CommandInitializer') { |       if (obj.default.constructor.name == 'CommandInitializer') { | ||||||
| 				obj.default.initialize(global.ctx); |         obj.default.initialize(global.ctx); | ||||||
| 				let cmds = obj.default.getCommands(); |         let cmds = obj.default.getCommands(); | ||||||
| 				if (parse.isCmd(cmds)) { |         if (parse.isCmd(cmds)) { | ||||||
| 					parse.addCommand(cmds); |           parse.addCommand(cmds); | ||||||
| 				} else if (cmds.constructor.name == 'Array') { |         } else if (cmds.constructor.name == 'Array') { | ||||||
| 					for (let cmd of cmds) { |           for (let cmd of cmds) { | ||||||
| 						parse.addCommand(cmd); |             parse.addCommand(cmd); | ||||||
| 					} |           } | ||||||
| 				} |         } | ||||||
| 			} |       } | ||||||
| 		} else { |     } else { | ||||||
| 			log.warn('module ' + file + ' returned an undefined module.'); |       log.warn('module ' + file + ' returned an undefined module.'); | ||||||
| 		} |     } | ||||||
| 	} |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| let cmd_dir = path.join(dir, 'cmd'); | let cmd_dir = path.join(dir, 'cmd'); | ||||||
|  |  | ||||||
|  | @ -4,156 +4,165 @@ 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), global.ctx.log_level); | ||||||
| 
 | 
 | ||||||
| class Target { | class Target { | ||||||
| 	constructor(channel) { |   constructor(channel) { | ||||||
| 		this.channel = channel; |     this.channel = channel; | ||||||
| 	} |   } | ||||||
| 	channel; |   channel; | ||||||
| 	value; |   value; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| const available_targets = { | const available_targets = { | ||||||
| 	r: new Target('r'), |   r: new Target('r'), | ||||||
| 	g: new Target('g'), |   g: new Target('g'), | ||||||
| 	b: new Target('b'), |   b: new Target('b'), | ||||||
| 	stack: new Target('stack'), |   stack: new Target('stack'), | ||||||
| 	stack2: new Target('stack2'), |   stack2: new Target('stack2'), | ||||||
| 	stack3: new Target('stack3'), |   stack3: new Target('stack3'), | ||||||
| 	stack4: new Target('stack4'), |   stack4: new Target('stack4'), | ||||||
| 	stack5: new Target('stack5'), |   stack5: new Target('stack5'), | ||||||
| 	stack6: new Target('stack6'), |   stack6: new Target('stack6'), | ||||||
| 	stack6: new Target('stackr'), |   stack6: new Target('stackr'), | ||||||
| 	stack6: new Target('stackg'), |   stack6: new Target('stackg'), | ||||||
| 	stack6: new Target('stackb'), |   stack6: new Target('stackb'), | ||||||
| 	tick: new Target('tick'), |   tick: new Target('tick'), | ||||||
| 	index: new Target('index'), |   index: new Target('index'), | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| class Instruction { | class Instruction { | ||||||
| 	constructor(name, targets, args) { |   constructor(name, targets, args) { | ||||||
| 		let valid_targets = []; |     let valid_targets = []; | ||||||
| 		targets.forEach((t) => { |     let matched_args = []; | ||||||
| 			if (t instanceof Target) { |     targets.forEach((t, i) => { | ||||||
| 				let match = Object.keys(available_targets).find((key) => available_targets[key].channel === t.channel); |       if (t instanceof Target) { | ||||||
| 				if (match) { |         let match = Object.keys(available_targets).find((key) => available_targets[key].channel === t.channel); | ||||||
| 					valid_targets.push(t); |         if (match) { | ||||||
| 				} |           if (args[i]) { | ||||||
| 			} |             matched_args.push({ | ||||||
| 		}); |               ...t, | ||||||
| 		this.name = name; |               arg: args[i] | ||||||
| 		this.targets = valid_targets; |             }) | ||||||
| 		this.args = args; |           } | ||||||
| 	} |           else { | ||||||
| 	name; |             matched_args.push(t); | ||||||
| 	targets; |           } | ||||||
| 	args; |         } | ||||||
|  |       } | ||||||
|  |     }); | ||||||
|  |     this.name = name; | ||||||
|  |     this.targets = valid_targets; | ||||||
|  |     this.args = matched_args; | ||||||
|  |   } | ||||||
|  |   name; | ||||||
|  |   targets; | ||||||
|  |   args; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export const instructions = [ | export const instructions = [ | ||||||
| 	{ n: 'CONSTANT', a: true }, |   { n: 'CONSTANT', a: true }, | ||||||
| 	{ n: 'ADD', a: true }, |   { n: 'ADD', a: true }, | ||||||
| 	{ n: 'SUBTRACT', a: true }, |   { n: 'SUBTRACT', a: true }, | ||||||
| 	{ n: 'MULTIPLY', a: true }, |   { n: 'MULTIPLY', a: true }, | ||||||
| 	{ n: 'DIVIDE', a: true }, |   { n: 'DIVIDE', a: true }, | ||||||
| 	{ n: 'MODULO', a: true }, |   { n: 'MODULO', a: true }, | ||||||
| 	{ n: 'FADE', a: true }, |   { n: 'FADE', a: true }, | ||||||
| 	{ n: 'RANDOM', a: false }, |   { n: 'RANDOM', a: false }, | ||||||
| 	{ n: 'JMP', a: false }, |   { n: 'JMP', a: false }, | ||||||
| 	{ n: 'JNZ', a: true }, |   { n: 'JNZ', a: true }, | ||||||
| 	{ n: 'JEZ', a: true }, |   { n: 'JEZ', a: true }, | ||||||
| ]; | ]; | ||||||
| 
 | 
 | ||||||
| export function initialize(ctx) { | export function initialize(ctx) { | ||||||
| 	log.s(ctx.log_level); |   log.s(ctx.log_level); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function parseSingleInstruction(str = '') { | function parseSingleInstruction(str = '') { | ||||||
| 	let item = { |   let item = { | ||||||
| 		valid: false, |     valid: false, | ||||||
| 		instruction: undefined, |     instruction: undefined, | ||||||
| 		errors: undefined, |     errors: undefined, | ||||||
| 	}; |   }; | ||||||
| 	let split = str.split(' '); |   let split = str.split(' '); | ||||||
| 	let cmd = split[0]; |   let cmd = split[0]; | ||||||
| 	log.debug(cmd); |   log.debug(cmd); | ||||||
| 	let match = instructions.filter((i) => i.n == cmd.toUpperCase()); |   let match = instructions.filter((i) => i.n == cmd.toUpperCase()); | ||||||
| 	log.debug(match); |   log.debug(match); | ||||||
| 	split = split.slice(1); |   split = split.slice(1); | ||||||
| 	if (match.length) { |   if (match.length) { | ||||||
| 		let inst_targets = []; |     let inst_targets = []; | ||||||
| 		let inst_args = []; |     let inst_args = []; | ||||||
| 		let parsed_args = []; |     let parsed_args = []; | ||||||
| 		let join_index = -1; |     let join_index = -1; | ||||||
| 		let add = true; |     let add = true; | ||||||
| 		for (let index in split) { |     for (let index in split) { | ||||||
| 			if (split[index].startsWith('[')) { |       if (split[index].startsWith('[')) { | ||||||
| 				join_index = index; |         join_index = index; | ||||||
| 				add = false; |         add = false; | ||||||
| 			} |       } | ||||||
| 			if (add) { |       if (add) { | ||||||
| 				parsed_args.push(split[index]); |         parsed_args.push(split[index]); | ||||||
| 			} |       } | ||||||
| 			if (split[index].endsWith(']') && join_index != -1) { |       if (split[index].endsWith(']') && join_index != -1) { | ||||||
| 				let joined = split |         let joined = split | ||||||
| 					.slice(join_index, index + 1) |           .slice(join_index, index + 1) | ||||||
| 					.join(' ') |           .join(' ') | ||||||
| 					.replace(/[\[\]]/g, ''); |           .replace(/[\[\]]/g, ''); | ||||||
| 				parsed_args.push(joined); |         parsed_args.push(joined); | ||||||
| 				add = true; |         add = true; | ||||||
| 				join_index = -1; |         join_index = -1; | ||||||
| 			} |       } | ||||||
| 		} |     } | ||||||
| 		inst_targets = parsed_args[0] |     inst_targets = parsed_args[0] | ||||||
| 			.split(',') |       .split(',') | ||||||
| 			.map((s) => s.trim()) |       .map((s) => s.trim()) | ||||||
| 			.map((t) => new Target(t)); |       .map((t) => new Target(t)); | ||||||
| 
 | 
 | ||||||
| 		if (!inst_targets.length) { |     if (!inst_targets.length) { | ||||||
| 			item.errors = 'No valid targets.'; |       item.errors = 'No valid targets.'; | ||||||
| 		} |     } | ||||||
| 		inst_targets.forEach((t) => { |     inst_targets.forEach((t) => { | ||||||
| 			let match = Object.keys(available_targets).find((key) => available_targets[key].channel === t.channel); |       let match = Object.keys(available_targets).find((key) => available_targets[key].channel === t.channel); | ||||||
| 			if (!match) { |       if (!match) { | ||||||
| 				item.errors = `Invalid target: ${t.channel}`; |         item.errors = `Invalid target: ${t.channel}`; | ||||||
| 			} |       } | ||||||
| 		}); |     }); | ||||||
| 		if (match[0].a) { |     if (match[0].a) { | ||||||
| 			if (parsed_args.length < 2) { |       if (parsed_args.length < 2) { | ||||||
| 				item.errors = 'Not enough arguments.'; |         item.errors = 'Not enough arguments.'; | ||||||
| 				return item; |         return item; | ||||||
| 			} |       } | ||||||
| 			inst_args = parsed_args[1] |       inst_args = parsed_args[1] | ||||||
| 				.split(',') |         .split(',') | ||||||
| 				.map((s) => s.trim()) |         .map((s) => s.trim()) | ||||||
| 				.map((s) => (available_targets[s] ? s : parseInt(s))); |         .map((s) => (available_targets[s] ? s : parseInt(s))); | ||||||
| 			if (!inst_args.length) { |       if (!inst_args.length) { | ||||||
| 				item.errors = 'No valid args.'; |         item.errors = 'No valid args.'; | ||||||
| 			} |       } | ||||||
| 			inst_args.forEach((arg) => { |       inst_args.forEach((arg) => { | ||||||
| 				if (arg === NaN) { |         if (arg === NaN) { | ||||||
| 					item.errors = 'An argument is null or undefined.'; |           item.errors = 'An argument is null or undefined.'; | ||||||
| 				} |         } | ||||||
| 			}); |       }); | ||||||
| 		} |     } | ||||||
| 		if (item.errors) { |     if (item.errors) { | ||||||
| 			return item; |       return item; | ||||||
| 		} |     } | ||||||
| 
 | 
 | ||||||
| 		item.instruction = new Instruction(match[0].n, inst_targets, inst_args || []); |     item.instruction = new Instruction(match[0].n, inst_targets, inst_args || []); | ||||||
| 	} else { |   } else { | ||||||
| 		item.errors = 'No command matched.'; |     item.errors = 'No command matched.'; | ||||||
| 		return item; |     return item; | ||||||
| 	} |   } | ||||||
| 	item.valid = true; |   item.valid = true; | ||||||
| 	return item; |   return item; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export default function parse(str) { | export default function parse(str) { | ||||||
| 	let parsed = []; |   let parsed = []; | ||||||
| 	let split = str.split('\n').filter((s) => s.length > 0); |   let split = str.split('\n').filter((s) => s.length > 0); | ||||||
| 	split.forEach((item) => { |   split.forEach((item) => { | ||||||
| 		let parsedItem = parseSingleInstruction(item); |     let parsedItem = parseSingleInstruction(item); | ||||||
| 		parsed.push(parsedItem); |     parsed.push(parsedItem); | ||||||
| 		log.debug(item); |     log.debug(item); | ||||||
| 	}); |   }); | ||||||
| 	return parsed; |   return parsed; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -4,26 +4,29 @@ const hostname = '192.168.1.219'; | ||||||
| const port = 29999; | const port = 29999; | ||||||
| 
 | 
 | ||||||
| function getBinarySize(string) { | function getBinarySize(string) { | ||||||
| 	return Buffer.byteLength(string, 'utf8'); |   return Buffer.byteLength(string, 'utf8'); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export default function req(data, callback, errorCallback) { | export default function req(data, callback, errorCallback) { | ||||||
| 	let string_data = JSON.stringify(data); |   let string_data = JSON.stringify(data); | ||||||
| 	let size = getBinarySize(string_data); |   let size = getBinarySize(string_data); | ||||||
|   if (size > 9999) { |   if (size > 9999) { | ||||||
|     errorCallback("too long"); |     errorCallback("too long"); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
| 	let client = new net.Socket(); |   let client = new net.Socket(); | ||||||
| 	client.connect(port, hostname, () => { |  | ||||||
| 		client.write(string_data); |  | ||||||
| 		client.destroy(); |  | ||||||
| 		callback('success.'); |  | ||||||
| 	}); |  | ||||||
| 
 | 
 | ||||||
| 	client.on('error', (e) => { |   let c_port = global.ctx.lights.port || port; | ||||||
| 		client.destroy(); |   let c_addr = global.ctx.lights.addr || hostname; | ||||||
| 		errorCallback(e); |   client.connect(c_port, c_addr, () => { | ||||||
| 	}); |     client.write(string_data); | ||||||
|  |     client.destroy(); | ||||||
|  |     callback('success.'); | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|  |   client.on('error', (e) => { | ||||||
|  |     client.destroy(); | ||||||
|  |     errorCallback(e); | ||||||
|  |   }); | ||||||
| } | } | ||||||
|  |  | ||||||
							
								
								
									
										24
									
								
								pnpm-lock.yaml
									
										
									
										generated
									
									
									
								
							
							
						
						
									
										24
									
								
								pnpm-lock.yaml
									
										
									
										generated
									
									
									
								
							|  | @ -1,7 +1,6 @@ | ||||||
| dependencies: | dependencies: | ||||||
|   eris: 0.14.0 |   eris: 0.14.0 | ||||||
|   socket.engine: 1.0.0 | lockfileVersion: 5.2 | ||||||
| lockfileVersion: 5.1 |  | ||||||
| packages: | packages: | ||||||
|   /eris/0.14.0: |   /eris/0.14.0: | ||||||
|     dependencies: |     dependencies: | ||||||
|  | @ -14,31 +13,11 @@ packages: | ||||||
|       tweetnacl: 1.0.3 |       tweetnacl: 1.0.3 | ||||||
|     resolution: |     resolution: | ||||||
|       integrity: sha512-/W6X0SFR2swtA9oc4ga5Wh1TQcZtPgbUaDDdwYc67fvFUAtwC+V1xzWUZq2yDeJnTfB8Uot9SJWA8Lthe2sDtQ== |       integrity: sha512-/W6X0SFR2swtA9oc4ga5Wh1TQcZtPgbUaDDdwYc67fvFUAtwC+V1xzWUZq2yDeJnTfB8Uot9SJWA8Lthe2sDtQ== | ||||||
|   /fs/0.0.1-security: |  | ||||||
|     dev: false |  | ||||||
|     resolution: |  | ||||||
|       integrity: sha1-invTcYa23d84E/I4WLV+yq9eQdQ= |  | ||||||
|   /ip/1.1.5: |  | ||||||
|     dev: false |  | ||||||
|     resolution: |  | ||||||
|       integrity: sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo= |  | ||||||
|   /net/1.0.2: |  | ||||||
|     dev: false |  | ||||||
|     resolution: |  | ||||||
|       integrity: sha1-0XV+yaf7I3HYPPR1XOPifhCCk4g= |  | ||||||
|   /opusscript/0.0.7: |   /opusscript/0.0.7: | ||||||
|     dev: false |     dev: false | ||||||
|     optional: true |     optional: true | ||||||
|     resolution: |     resolution: | ||||||
|       integrity: sha512-DcBadTdYTUuH9zQtepsLjQn4Ll6rs3dmeFvN+SD0ThPnxRBRm/WC1zXWPg+wgAJimB784gdZvUMA57gDP7FdVg== |       integrity: sha512-DcBadTdYTUuH9zQtepsLjQn4Ll6rs3dmeFvN+SD0ThPnxRBRm/WC1zXWPg+wgAJimB784gdZvUMA57gDP7FdVg== | ||||||
|   /socket.engine/1.0.0: |  | ||||||
|     dependencies: |  | ||||||
|       fs: 0.0.1-security |  | ||||||
|       ip: 1.1.5 |  | ||||||
|       net: 1.0.2 |  | ||||||
|     dev: false |  | ||||||
|     resolution: |  | ||||||
|       integrity: sha512-vwc9JVBNe33686TBl9T3Ro53FaF09z11NVnrf0HIlYUOIDaZ1OFA1J1O6igU57qhP3NyUv4a+kZtgMm6SnziaQ== |  | ||||||
|   /tweetnacl/1.0.3: |   /tweetnacl/1.0.3: | ||||||
|     dev: false |     dev: false | ||||||
|     optional: true |     optional: true | ||||||
|  | @ -60,4 +39,3 @@ packages: | ||||||
|       integrity: sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA== |       integrity: sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA== | ||||||
| specifiers: | specifiers: | ||||||
|   eris: ^0.14.0 |   eris: ^0.14.0 | ||||||
|   socket.engine: ^1.0.0 |  | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue