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