For Rubay's Command Handler Maybe

 avatar
unknown
javascript
4 years ago
2.9 kB
5
Indexable
const Discord = require("discord.js");
const fs = require("fs");
const ReactBot = new Discord.Client();
const {prefix, token} = require("./config.json")

ReactBot.commands = new Discord.Collection();
ReactBot.aliases = new Discord.Collection();
ReactBot.categories = fs.readdirSync("./commands/");

fs.readdirSync("./commands/").forEach(dir => {
	const files = fs.readdirSync(`./commands/${dir}`);
	
	for(const file of files) {
		const pull = require(`./commands/${dir}/${file}`);
		
		if(pull.name) {
			ReactBot.commands.set(pull.name.toLowerCase(), pull);
		}
		else {
			continue;
		}

		if(pull.aliases) {
			if(!Array.isArray(pull.aliases))) return;
			for(const alias of pull.aliases) {
				ReactBot.aliases.set(alias.toLowerCase(), pull.name);
			}
		}
	}
})

const cooldowns = new Discord.Collection();


ReactBot.once('ready', () => {
console.log('React is a react!!');
	ReactBot.user.setActivity("React is a React!", {type: "WATCHING"})
});
ReactBot.on('message', message => {
	if (!message.content.startsWith(prefix) || message.author.bot) return;
 
	const args = message.content.slice(prefix.length).trim().split(/ +/);
	const commandName = args.shift().toLowerCase();
 
	const command = ReactBot.commands.get(commandName) || ReactBot.commands.get(ReactBot.aliases.get(commandName));
 
	if (!command) return;
 
	if (command.guildOnly && message.channel.type === 'dm') {
		return message.reply('I can\'t execute that command inside DMs!');
	}
 
	if (command.permissions) {
		const authorPerms = message.channel.permissionsFor(message.author);
		if (!authorPerms || !authorPerms.has(command.permissions)) {
			return message.channel.reply('You can not do this!');
		}
	}
 
	if (command.args && !args.length) {
		let reply = `You didn't provide any arguments, ${message.author}!`;
 
		if (command.usage) {
			reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
		}
 
		return message.channel.send(reply);
	}
	if (!cooldowns.has(command.name)) {
		cooldowns.set(command.name, new Discord.Collection());
	}
 
	const now = Date.now();
	const timestamps = cooldowns.get(command.name);
	const cooldownAmount = (command.cooldown || 3) * 1000;
 
	if (timestamps.has(message.author.id)) {
		const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
 
		if (now < expirationTime) {
			const timeLeft = (expirationTime - now) / 1000;
			return message.reply(`please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`);
		}
	}
 
	timestamps.set(message.author.id, now);
	setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
 
	try {
		command.execute(message, args);
	} catch (error) {
		console.error(error);
		message.reply('there was an error trying to execute that command!');
	}
});
ReactBot.login(token)
Editor is loading...