mirror of
				https://github.com/keanuplayz/TravBot-v3.git
				synced 2024-08-15 02:33:12 +00:00 
			
		
		
		
	Did multiple things.
- Added command handler. - Added hello command. - Added utility commands. - Added lockfile.
This commit is contained in:
		
							parent
							
								
									c4b252d67e
								
							
						
					
					
						commit
						3071a4820c
					
				
					 8 changed files with 1337 additions and 4 deletions
				
			
		
							
								
								
									
										1223
									
								
								package-lock.json
									
										
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										1223
									
								
								package-lock.json
									
										
									
										generated
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							|  | @ -13,7 +13,8 @@ | ||||||
|   "author": "Keanu Timmermans", |   "author": "Keanu Timmermans", | ||||||
|   "license": "Apache-2.0", |   "license": "Apache-2.0", | ||||||
|   "dependencies": { |   "dependencies": { | ||||||
|     "discord.js": "^12.2.0" |     "discord.js": "^12.2.0", | ||||||
|  |     "ms": "^2.1.2" | ||||||
|   }, |   }, | ||||||
|   "devDependencies": { |   "devDependencies": { | ||||||
|     "eslint": "^7.0.0", |     "eslint": "^7.0.0", | ||||||
|  |  | ||||||
							
								
								
									
										13
									
								
								src/Commands/Hello.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/Commands/Hello.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,13 @@ | ||||||
|  | const Command = require("./../Structures/Command.js"); | ||||||
|  | 
 | ||||||
|  | module.exports = class extends Command { | ||||||
|  |   constructor(...args) { | ||||||
|  |     super(...args, { | ||||||
|  |       aliases: ["hallo"] | ||||||
|  |     }) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   async run(message, args) { | ||||||
|  |     message.channel.send("Hello"); | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										21
									
								
								src/Commands/Utilities/Ping.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/Commands/Utilities/Ping.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,21 @@ | ||||||
|  | const Command = require('../../Structures/Command'); | ||||||
|  | 
 | ||||||
|  | module.exports = class extends Command { | ||||||
|  | 
 | ||||||
|  | 	constructor(...args) { | ||||||
|  | 		super(...args, { | ||||||
|  | 			aliases: ['pong'] | ||||||
|  | 		}); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	async run(message) { | ||||||
|  | 		const msg = await message.channel.send('Pinging...'); | ||||||
|  | 
 | ||||||
|  | 		const latency = msg.createdTimestamp - message.createdTimestamp; | ||||||
|  | 		const choices = ['Is this really my ping?', "Is this okay? I can't look!", "I hope it isn't bad!"]; | ||||||
|  | 		const response = choices[Math.floor(Math.random() * choices.length)]; | ||||||
|  | 
 | ||||||
|  | 		msg.edit(`${response} - Bot Latency: \`${latency}ms\`, API Latency: \`${Math.round(this.client.ws.ping)}ms\``); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | }; | ||||||
							
								
								
									
										10
									
								
								src/Commands/Utilities/Uptime.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/Commands/Utilities/Uptime.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,10 @@ | ||||||
|  | const Command = require("../../Structures/Command.js"); | ||||||
|  | const ms = require("ms"); | ||||||
|  | 
 | ||||||
|  | module.exports = class extends Command { | ||||||
|  | 
 | ||||||
|  |   async run(message) { | ||||||
|  |     message.channel.send(`My uptime is \`${ms(this.client.uptime, {long: true})}\``); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | @ -1,4 +1,5 @@ | ||||||
| const { Client } = require('discord.js'); | const { Client, Collection } = require('discord.js'); | ||||||
|  | const Util = require('./Util.js'); | ||||||
| 
 | 
 | ||||||
| module.exports = class BotClient extends Client { | module.exports = class BotClient extends Client { | ||||||
| 
 | 
 | ||||||
|  | @ -8,6 +9,12 @@ module.exports = class BotClient extends Client { | ||||||
| 		}); | 		}); | ||||||
| 		this.validate(options); | 		this.validate(options); | ||||||
| 
 | 
 | ||||||
|  | 		this.commands = new Collection(); | ||||||
|  | 
 | ||||||
|  | 		this.aliases = new Collection(); | ||||||
|  | 
 | ||||||
|  | 		this.utils = new Util(this); | ||||||
|  | 
 | ||||||
| 		this.once('ready', () => { | 		this.once('ready', () => { | ||||||
| 			console.log(`Logged in as ${this.user.username}.`); | 			console.log(`Logged in as ${this.user.username}.`); | ||||||
| 		}); | 		}); | ||||||
|  | @ -26,8 +33,9 @@ module.exports = class BotClient extends Client { | ||||||
| 			// eslint-disable-next-line no-unused-vars
 | 			// eslint-disable-next-line no-unused-vars
 | ||||||
| 			const [cmd, ...args] = message.content.slice(prefix.length).trim().split(/ +/g); | 			const [cmd, ...args] = message.content.slice(prefix.length).trim().split(/ +/g); | ||||||
| 
 | 
 | ||||||
| 			if (cmd.toLowerCase() === 'hello') { | 			const command = this.commands.get(cmd.toLowerCase()) || this.commands.get(this.aliases.get(cmd.toLowerCase())); | ||||||
| 				message.channel.send('Hello!'); | 			if (command) { | ||||||
|  | 				command.run(message, args); | ||||||
| 			} | 			} | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
|  | @ -44,6 +52,7 @@ module.exports = class BotClient extends Client { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	async start(token = this.token) { | 	async start(token = this.token) { | ||||||
|  | 		this.utils.loadCommands(); | ||||||
| 		super.login(token); | 		super.login(token); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										15
									
								
								src/Structures/Command.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								src/Structures/Command.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,15 @@ | ||||||
|  | module.exports = class Command { | ||||||
|  |   constructor(client, name, options = {}) { | ||||||
|  |     this.client = client; | ||||||
|  |     this.name = options.name || name; | ||||||
|  |     this.aliases = options.aliases || []; | ||||||
|  |     this.description = options.description || "No description provided."; | ||||||
|  |     this.category = options.category || "Miscellaneous"; | ||||||
|  |     this.usage = options.usage || "No usage provided."; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   // eslint-disable-next-line no-unused-vars
 | ||||||
|  |   async run(message, args) { | ||||||
|  |     throw new Error(`Command ${this.name} doesn't provide a run method.`) | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										41
									
								
								src/Structures/Util.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								src/Structures/Util.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,41 @@ | ||||||
|  | const path = require('path'); | ||||||
|  | const { promisify } = require('util'); | ||||||
|  | const glob = promisify(require('glob')); | ||||||
|  | const Command = require('./Command.js'); | ||||||
|  | 
 | ||||||
|  | module.exports = class Util { | ||||||
|  | 
 | ||||||
|  | 	constructor(client) { | ||||||
|  | 		this.client = client; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	isClass(input) { | ||||||
|  | 		return typeof input === 'function' && | ||||||
|  |         typeof input.prototype === 'object' && | ||||||
|  |         input.toString().substring(0, 5) === 'class'; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	get directory() { | ||||||
|  | 		return `${path.dirname(require.main.filename)}${path.sep}`; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	async loadCommands() { | ||||||
|  | 		return glob(`${this.directory}commands/**/*.js`).then(commands => { | ||||||
|  | 			for (const commandFile of commands) { | ||||||
|  | 				delete require.cache[commandFile]; | ||||||
|  | 				const { name } = path.parse(commandFile); | ||||||
|  | 				const File = require(commandFile); | ||||||
|  | 				if (!this.isClass(File)) throw new TypeError(`Command ${name} doesn't export a class.`); | ||||||
|  | 				const command = new File(this.client, name.toLowerCase()); | ||||||
|  | 				if (!(command instanceof Command)) throw new TypeError(`Command ${name} doesn't belong in commands.`); | ||||||
|  | 				this.client.commands.set(command.name, command); | ||||||
|  | 				if (command.aliases.length) { | ||||||
|  | 					for (const alias of command.aliases) { | ||||||
|  | 						this.client.aliases.set(alias, command.name); | ||||||
|  | 					} | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		}); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | }; | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue