Commit dbed4b76 authored by Imran Hussain's avatar Imran Hussain
Browse files

Lift and shift a command handler from https://discordjs.guide/command-handling

parent 5ff39796
module.exports = (message) => {
const member = message.mentions.members.first();
module.exports = {
name: 'kick',
description: 'Kick someone',
cooldown: 10,
guildOnly: true,
execute(message) {
const member = message.mentions.members.first();
if (!member) {
return message.reply(
`Who are you trying to kick? You must mention a user.`
);
}
if (!member) {
return message.reply(
`Who are you trying to kick? You must mention a user.`
);
}
if (!member.kickable) {
return message.reply(`I can't kick this user. Sorry!`);
}
if (!member.kickable) {
return message.reply(`I can't kick this user. Sorry!`);
}
return member
.kick()
.then(() => message.reply(`${member.user.tag} was kicked.`))
.catch((error) => message.reply(`Sorry, an error occured.`));
member
.kick()
.then(() => message.reply(`${member.user.tag} was kicked.`))
.catch((error) => message.reply(`Sorry, an error occured.`));
},
};
const kick = require('../commands/kick');
/* global process */
const Discord = require('discord.js');
const cooldowns = new Discord.Collection();
module.exports = (client, message) => {
if (message.content.startsWith('.kick')) {
return kick(message);
}
// let mw people know that they are on discord
if (message.content.startsWith('.e')) {
message.reply(`This is not MW!`);
message.reply('This is not Milliways!');
}
// don't process non commands or bot messages
if (!message.content.startsWith(process.env.PREFIX) ||
message.author.bot) return;
const args = message.content.slice(process.env.PREFIX.length).split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName) ||
client.commands.find((cmd) =>
cmd.aliases && cmd.aliases.includes(commandName));
// not a command we know about? don't do anything...
if (!command) return;
if (command.guildOnly && message.channel.type !== 'text') {
return message.reply('I can\'t execute that command inside DMs!');
}
if (command.args && !args.length) {
let reply = `You didn't provide any arguments, ${message.author}!`;
if (command.usage) {
reply += `\nThe proper usage would be: \`${process.env.PREFIX}${command.name} ${command.usage}\``;
}
return message.channel.send(reply);
}
// cooldowns for commands to stop people from spamming the API via our bot
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!');
}
};
......@@ -2,6 +2,7 @@ require('dotenv').config();
const Discord = require('discord.js');
const fs = require('fs');
const client = new Discord.Client();
client.commands = new Discord.Collection();
// main event handler, look in a folder clled "events" and when one comes in
// that matches use that file
......@@ -13,5 +14,13 @@ fs.readdir('./events/', (err, files) => {
});
});
const commandFiles = fs.readdirSync('./commands')
.filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
/* global process */
client.login(process.env.BOT_TOKEN);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment