California292

random message

Sep 11th, 2016
1,263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Discord = require('discord.js');
  2. var bot = new Discord.Client();
  3. var token = "sometoken";
  4. var randomMessage;
  5. var randOn = false;
  6. var responseArray = [ //add more messages here
  7.   "Ayy, lmao!",
  8.   "Say what?",
  9.   "roflmaotntpmp"
  10. ];
  11. var prefix = "!";
  12. var timer = [5,10]; //set min and max in seconds for random messages
  13.  
  14. bot.on("ready", () => {
  15.     console.log("Bot online and ready on " + bot.guilds.size + " server(s).");
  16. });
  17.  
  18. bot.on('message', (msg) => {
  19.   if (msg.content.startsWith(prefix + "on")) {
  20.         if (randOn) {
  21.             msg.channel.sendMessage("Already running.");
  22.         }
  23.         else {
  24.             msg.channel.sendMessage("Random message started.")
  25.         randomMessage = setTimeout(function() {
  26.                 randMsg(msg.channel);
  27.             }, 1000*timer[0]);
  28.         }
  29.   }
  30.   else if (msg.content.startsWith(prefix + "off")) {
  31.         if (randOn) {
  32.             clearTimeout(randomMessage);
  33.             msg.channel.sendMessage("Random message disabled.");
  34.         }
  35.         else {
  36.             msg.channel.sendMessage("Not running.");
  37.         }
  38.   }
  39. });
  40.  
  41. bot.login(token);
  42.  
  43. function randomIntFromInterval(min, max) {
  44.   return Math.floor(Math.random()*(max-min+1)+min);
  45. }
  46.  
  47. function randMsg(msgChan) {
  48.     console.log("callback");
  49.     var interval = 1000*randomIntFromInterval(timer[0],timer[1]);
  50.   var rand = randomIntFromInterval(0,responseArray.length-1);
  51.   if(responseArray[rand]) {
  52.     msgChan.sendMessage(responseArray[rand]);
  53.   }
  54.     randomMessage = setTimeout(function() {
  55.         randMsg(msgChan);
  56.     }, interval);
  57. }
Add Comment
Please, Sign In to add comment