forked from izy521/discord.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
97 lines (86 loc) · 2.4 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
86
87
88
89
90
91
92
93
94
95
96
97
/*Variable area*/
var Discordbot = require('discord.io');
var bot = new Discordbot({
email: "",
password: "",
autorun: true
});
/*Event area*/
bot.on("err", function(error) {
console.log(error)
});
bot.on("ready", function(rawEvent) {
console.log("Connected!");
console.log("Logged in as: ");
console.log(bot.username + " - (" + bot.id + ")");
});
bot.on("message", function(user, userID, channelID, message, rawEvent) {
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, rawEvent) {
/*console.log(user + " is now: " + status);*/
});
bot.on("debug", function(rawEvent) {
/*console.log(rawEvent)*/ //Logs every event
});
bot.on("disconnected", function() {
console.log("Bot disconnected");
/*bot.connect()*/ //Auto reconnect
});
/*Function declaration area*/
function sendMessages(ID, messageArr, interval) {
var callback, resArr = [], len = messageArr.length;
typeof(arguments[2]) === 'function' ? callback = arguments[2] : callback = 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) {
if (err) {
resArr.push(err);
} else {
resArr.push(res);
}
if (resArr.length === len) if (typeof(callback) === 'function') callback(resArr);
});
_sendMessages();
}
}, interval);
}
_sendMessages();
}
function sendFiles(channelID, fileArr, interval) {
var callback, resArr = [], len = fileArr.length;
typeof(arguments[2]) === 'function' ? callback = arguments[2] : callback = 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) {
if (err) {
resArr.push(err);
} else {
resArr.push(res);
}
if (resArr.length === len) if (typeof(callback) === 'function') callback(resArr);
});
_sendFiles();
}
}, interval);
}
_sendFiles();
}