| 1 | /*Variable area*/ |
| 2 | var Discord = require('discord.io'); |
| 3 | var bot = new Discord.Client({ |
| 4 | token: "", |
| 5 | autorun: true |
| 6 | }); |
| 7 | |
| 8 | /*Event area*/ |
| 9 | |
| 10 | bot.on("ready", function(event) { |
| 11 | console.log("Connected!"); |
| 12 | console.log("Logged in as: "); |
| 13 | console.log(bot.username + " - (" + bot.id + ")"); |
| 14 | }); |
| 15 | |
| 16 | bot.on("message", function(user, userID, channelID, message, event) { |
| 17 | console.log(user + " - " + userID); |
| 18 | console.log("in " + channelID); |
| 19 | console.log(message); |
| 20 | console.log("----------"); |
| 21 | |
| 22 | if (message === "ping") { |
| 23 | sendMessages(channelID, ["Pong"]); //Sending a message with our helper function |
| 24 | } else if (message === "picture") { |
| 25 | sendFiles(channelID, ["fillsquare.png"]); //Sending a file with our helper function |
| 26 | } |
| 27 | }); |
| 28 | |
| 29 | bot.on("presence", function(user, userID, status, game, event) { |
| 30 | /*console.log(user + " is now: " + status);*/ |
| 31 | }); |
| 32 | |
| 33 | bot.on("any", function(event) { |
| 34 | /*console.log(rawEvent)*/ //Logs every event |
| 35 | }); |
| 36 | |
| 37 | bot.on("disconnect", function() { |
| 38 | console.log("Bot disconnected"); |
| 39 | /*bot.connect()*/ //Auto reconnect |
| 40 | }); |
| 41 | |
| 42 | /*Function declaration area*/ |
| 43 | function sendMessages(ID, messageArr, interval) { |
| 44 | var resArr = [], len = messageArr.length; |
| 45 | var callback = typeof(arguments[2]) === 'function' ? arguments[2] : arguments[3]; |
| 46 | if (typeof(interval) !== 'number') interval = 1000; |
| 47 | |
| 48 | function _sendMessages() { |
| 49 | setTimeout(function() { |
| 50 | if (messageArr[0]) { |
| 51 | bot.sendMessage({ |
| 52 | to: ID, |
| 53 | message: messageArr.shift() |
| 54 | }, function(err, res) { |
| 55 | resArr.push(err || res); |
| 56 | if (resArr.length === len) if (typeof(callback) === 'function') callback(resArr); |
| 57 | }); |
| 58 | _sendMessages(); |
| 59 | } |
| 60 | }, interval); |
| 61 | } |
| 62 | _sendMessages(); |
| 63 | } |
| 64 | |
| 65 | function sendFiles(channelID, fileArr, interval) { |
| 66 | var resArr = [], len = fileArr.length; |
| 67 | var callback = typeof(arguments[2]) === 'function' ? arguments[2] : arguments[3]; |
| 68 | if (typeof(interval) !== 'number') interval = 1000; |
| 69 | |
| 70 | function _sendFiles() { |
| 71 | setTimeout(function() { |
| 72 | if (fileArr[0]) { |
| 73 | bot.uploadFile({ |
| 74 | to: channelID, |
| 75 | file: fileArr.shift() |
| 76 | }, function(err, res) { |
| 77 | resArr.push(err || res); |
| 78 | if (resArr.length === len) if (typeof(callback) === 'function') callback(resArr); |
| 79 | }); |
| 80 | _sendFiles(); |
| 81 | } |
| 82 | }, interval); |
| 83 | } |
| 84 | _sendFiles(); |
| 85 | } |