SHARE
TWEET

Untitled

a guest Oct 18th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Discord = require('discord.js')
  2. const client = new Discord.Client()
  3.  
  4. client.on('message', (receivedMessage) => {
  5.     if (receivedMessage.author == client.user) { // Prevent bot from responding to its own messages
  6.         return
  7.     }
  8.    
  9.     if (receivedMessage.content.startsWith("!")) {
  10.         processCommand(receivedMessage)
  11.     }
  12. })
  13.  
  14. function processCommand(receivedMessage) {
  15.     let fullCommand = receivedMessage.content.substr(1) // Remove the leading exclamation mark
  16.     let splitCommand = fullCommand.split(" ") // Split the message up in to pieces for each space
  17.     let primaryCommand = splitCommand[0] // The first word directly after the exclamation is the command
  18.     let arguments = splitCommand.slice(1) // All other words are arguments/parameters/options for the command
  19.  
  20.     console.log("Command received: " + primaryCommand)
  21.     console.log("Arguments: " + arguments) // There may not be any arguments
  22.  
  23.     if (primaryCommand == "help") {
  24.         helpCommand(arguments, receivedMessage)
  25.     } else if (primaryCommand == "multiply") {
  26.         multiplyCommand(arguments, receivedMessage)
  27.     } else {
  28.         receivedMessage.channel.send("I don't understand the command. Try `!help` or `!multiply`")
  29.     }
  30. }
  31.  
  32. function helpCommand(arguments, receivedMessage) {
  33.     if (arguments.length > 0) {
  34.         receivedMessage.channel.send("It looks like you might need help with " + arguments)
  35.     } else {
  36.         receivedMessage.channel.send("I'm not sure what you need help with. Try `!help [topic]`")
  37.     }
  38. }
  39.  
  40. function multiplyCommand(arguments, receivedMessage) {
  41.     if (arguments.length < 2) {
  42.         receivedMessage.channel.send("Not enough values to multiply. Try `!multiply 2 4 10` or `!multiply 5.2 7`")
  43.         return
  44.     }
  45.     let product = 1
  46.     arguments.forEach((value) => {
  47.         product = product * parseFloat(value)
  48.     })
  49.     receivedMessage.channel.send("The product of " + arguments + " multiplied together is: " + product.toString())
  50. }
  51.  
  52. client.login("token")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top