mirror of
				https://github.com/keanuplayz/TravBot-v3.git
				synced 2024-08-15 02:33:12 +00:00 
			
		
		
		
	Modularized command resolution
This commit is contained in:
		
							parent
							
								
									4b03912e89
								
							
						
					
					
						commit
						2488239598
					
				
					 3 changed files with 88 additions and 85 deletions
				
			
		|  | @ -40,43 +40,34 @@ export default new Command({ | |||
| 			let command = commands.get(header); | ||||
| 			 | ||||
| 			if(!command || header === "test") | ||||
| 				$.channel.send(`No command found by the name \`${header}\`!`); | ||||
| 			else | ||||
| 			{ | ||||
| 				return $.channel.send(`No command found by the name \`${header}\`!`); | ||||
| 			 | ||||
| 			let usage = command.usage; | ||||
| 			let invalid = false; | ||||
| 			 | ||||
| 			for(const param of $.args) | ||||
| 			{ | ||||
| 					header += ` ${param}`; | ||||
| 				const type = command.resolve(param); | ||||
| 				 | ||||
| 					if(/<\w+>/g.test(param)) | ||||
| 				switch(type) | ||||
| 				{ | ||||
| 						const type = param.match(/\w+/g)[0]; | ||||
| 						command = command[type]; | ||||
| 					case Command.TYPES.SUBCOMMAND: header += ` ${param}`; break; | ||||
| 					case Command.TYPES.USER: header += " <user>" ; break; | ||||
| 					case Command.TYPES.NUMBER: header += " <number>" ; break; | ||||
| 					case Command.TYPES.ANY: header += " <any>" ; break; | ||||
| 					default: header += ` ${param}`; break; | ||||
| 				} | ||||
| 				 | ||||
| 						if(types.includes(type) && command?.usage) | ||||
| 							usage = command.usage; | ||||
| 						else | ||||
| 				if(type === Command.TYPES.NONE) | ||||
| 				{ | ||||
| 							command = undefined; | ||||
| 					invalid = true; | ||||
| 					break; | ||||
| 				} | ||||
| 					} | ||||
| 					else if(command?.subcommands?.[param]) | ||||
| 					{ | ||||
| 						command = command.subcommands[param]; | ||||
| 				 | ||||
| 						if(command.usage !== "") | ||||
| 							usage = command.usage; | ||||
| 					} | ||||
| 					else | ||||
| 					{ | ||||
| 						command = undefined; | ||||
| 						break; | ||||
| 					} | ||||
| 				command = command.get(param); | ||||
| 			} | ||||
| 			 | ||||
| 				if(!command) | ||||
| 			if(invalid) | ||||
| 				return $.channel.send(`No command found by the name \`${header}\`!`); | ||||
| 			 | ||||
| 			let append = ""; | ||||
|  | @ -102,7 +93,6 @@ export default new Command({ | |||
| 					} | ||||
| 				} | ||||
| 				 | ||||
| 					 | ||||
| 				append = "Usages:" + (list.length > 0 ? `\n${list.join('\n')}` : " None."); | ||||
| 			} | ||||
| 			else | ||||
|  | @ -110,6 +100,5 @@ export default new Command({ | |||
| 			 | ||||
| 			$.channel.send(`Command: \`${header}\`\nDescription: ${command.description}\n${append}`, {split: true}); | ||||
| 		} | ||||
| 		} | ||||
| 	}) | ||||
| }); | ||||
|  | @ -21,6 +21,8 @@ interface CommandOptions | |||
| 	any?: Command; | ||||
| } | ||||
| 
 | ||||
| export enum TYPES {SUBCOMMAND, USER, NUMBER, ANY, NONE}; | ||||
| 
 | ||||
| export default class Command | ||||
| { | ||||
| 	public readonly description: string; | ||||
|  | @ -34,6 +36,7 @@ export default class Command | |||
| 	public any: Command|null; | ||||
| 	//public static readonly PERMISSIONS = PERMISSIONS;
 | ||||
| 	[key: string]: any; // Allow for dynamic indexing. The CommandOptions interface will still prevent users from adding unused properties though.
 | ||||
| 	public static readonly TYPES = TYPES; | ||||
| 	 | ||||
| 	constructor(options?: CommandOptions) | ||||
| 	{ | ||||
|  | @ -78,17 +81,38 @@ export default class Command | |||
| 		this.subcommands[key] = command; | ||||
| 	} | ||||
| 	 | ||||
| 	/** See if a subcommand exists for the command. */ | ||||
| 	/*public has(type: string): boolean | ||||
| 	public resolve(param: string): TYPES | ||||
| 	{ | ||||
| 		return this.subcommands && (type in this.subcommands) || false; | ||||
| 	}*/ | ||||
| 		if(this.subcommands?.[param]) | ||||
| 			return TYPES.SUBCOMMAND; | ||||
| 		// Any Discord ID format will automatically format to a user ID.
 | ||||
| 		else if(this.user && (/\d{17,19}/.test(param))) | ||||
| 			return TYPES.USER; | ||||
| 		// Disallow infinity and allow for 0.
 | ||||
| 		else if(this.number && (Number(param) || param === "0") && !param.includes("Infinity")) | ||||
| 			return TYPES.NUMBER; | ||||
| 		else if(this.any) | ||||
| 			return TYPES.ANY; | ||||
| 		else | ||||
| 			return TYPES.NONE; | ||||
| 	} | ||||
| 	 | ||||
| 	/** Get the requested subcommand if it exists. */ | ||||
| 	/*public get(type: string): Command|null | ||||
| 	public get(param: string): Command | ||||
| 	{ | ||||
| 		return this.subcommands && this.subcommands[type] || null; | ||||
| 	}*/ | ||||
| 		const type = this.resolve(param); | ||||
| 		let command; | ||||
| 		 | ||||
| 		switch(type) | ||||
| 		{ | ||||
| 			case TYPES.SUBCOMMAND: command = this.subcommands![param]; break; | ||||
| 			case TYPES.USER: command = this.user as Command; break; | ||||
| 			case TYPES.NUMBER: command = this.number as Command; break; | ||||
| 			case TYPES.ANY: command = this.any as Command; break; | ||||
| 			default: command = this; break; | ||||
| 		} | ||||
| 		 | ||||
| 		return command; | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| /*export function hasPermission(member: GuildMember, permission: number): boolean | ||||
|  |  | |||
|  | @ -58,28 +58,18 @@ export default new Event({ | |||
| 				break; | ||||
| 			} | ||||
| 			 | ||||
| 			if(command.subcommands?.[param]) | ||||
| 				command = command.subcommands[param]; | ||||
| 			// Any Discord ID format will automatically format to a user ID.
 | ||||
| 			else if(command.user && (/\d{17,19}/.test(param))) | ||||
| 			const type = command.resolve(param); | ||||
| 			command = command.get(param); | ||||
| 			 | ||||
| 			if(type === Command.TYPES.USER) | ||||
| 			{ | ||||
| 				const id = param.match(/\d+/g)![0]; | ||||
| 				command = command.user; | ||||
| 				try {params.push(await message.client.users.fetch(id))} | ||||
| 				catch(error) {return message.channel.send(`No user found by the ID \`${id}\`!`)} | ||||
| 			} | ||||
| 			// Disallow infinity and allow for 0.
 | ||||
| 			else if(command.number && (Number(param) || param === "0") && !param.includes("Infinity")) | ||||
| 			{ | ||||
| 				command = command.number; | ||||
| 			else if(type === Command.TYPES.NUMBER) | ||||
| 				params.push(Number(param)); | ||||
| 			} | ||||
| 			else if(command.any) | ||||
| 			{ | ||||
| 				command = command.any; | ||||
| 				params.push(param); | ||||
| 			} | ||||
| 			else | ||||
| 			else if(type !== Command.TYPES.SUBCOMMAND) | ||||
| 				params.push(param); | ||||
| 		} | ||||
| 		 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue