KallelGN

Command Handling Discord.JS v12

Sep 16th, 2020
1,021
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* INDEX.JS | START */
  2.  
  3. client.on('message', message => {
  4.     if (!message.content.startsWith(prefix) || message.author.bot) return;
  5.  
  6.     const args = message.content.slice(prefix.length).trim().split(/ +/);
  7.     const commandName = args.shift().toLowerCase();
  8.     const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
  9.  
  10.     if (!command) return message.channel.send({
  11.         embed: {
  12.             title: 'This commands does not exist!',
  13.             color: color,
  14.             description: `Try using \`${prefix}help\``,
  15.             footer: {
  16.                 text: message.author.tag,
  17.                 icon_url: message.author.displayAvatarURL({ format:'png', dynamic: true })
  18.             }
  19.         }
  20.     });
  21.  
  22.     if (command.guildOnly && message.channel.type === 'dm') return message.channel.send('**<:error:755511540320436385> Use commands in guild channel!**');
  23.  
  24.     if (!cooldowns.has(command.name)) {
  25.         cooldowns.set(command.name, new Collection());
  26.     }
  27.  
  28.     const now = Date.now();
  29.     const timestamps = cooldowns.get(command.name);
  30.     const cooldownAmount = (command.cooldown || 3) * 1000;
  31.  
  32.     if (timestamps.has(message.author.id)) {
  33.         const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
  34.  
  35.         if (now < expirationTime) {
  36.             const timeLeft = (expirationTime - now) / 1000;
  37.             return message.reply(`**<:error:755511540320436385> Please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${prefix}${command.name}\` command.**`);
  38.         }
  39.     }
  40.    
  41.     timestamps.set(message.author.id, now);
  42.     setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
  43.  
  44.     try {
  45.         command.execute(client, message, args);
  46.     } catch (error) {
  47.         console.error(error);
  48.         message.reply('**<:error:755511540320436385> Error, try again later.**');
  49.     }
  50. });
  51.  
  52. /* INDEX.JS | END */
  53.  
  54. /* commands/{command}.JS | START */
  55.  
  56. module.exports = {
  57.     name: 'ban',
  58.     description: 'Ban a member',
  59.     aliases: ['thor', 'banir'],
  60.     usage: '<@user> [reason]',
  61.     cooldown: 5,
  62.     permission: 'BAN_MEMBERS',
  63.     guildOnly: true,
  64.     async execute(client, message, args) {
  65.         message.mentions.members.first().ban();
  66.     }
  67. };
  68.  
  69. /* commands/{command}.JS | END */
  70.  
  71. /*
  72. MIT License
  73.  
  74. Copyright (c) 2020 KallelGaNewk
  75.  
  76. Permission is hereby granted, free of charge, to any person obtaining a copy
  77. of this software and associated documentation files (the "Software"), to deal
  78. in the Software without restriction, including without limitation the rights
  79. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  80. copies of the Software, and to permit persons to whom the Software is
  81. furnished to do so, subject to the following conditions:
  82.  
  83. The above copyright notice and this permission notice shall be included in all
  84. copies or substantial portions of the Software.
  85.  
  86. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  87. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  88. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  89. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  90. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  91. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  92. SOFTWARE.
  93. */
RAW Paste Data