-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
144 lines (124 loc) · 4.43 KB
/
index.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*Variable area*/
var initHandler = require('./initiative-handler');
var rollHandler = require('./roll-handler');
var destinyHandler = require('./destiny-handler');
const HomeServer = process.env.home_server;
var Discord = require('discord.io');
var bot = new Discord.Client({
token: process.env.token,
autorun: true
});
var serverDice = {};
var serverSymbols = {};
var serverSetup = {};
/*Event area*/
bot.on("ready", function(event) {
console.log("Connected!");
console.log("Logged in as: ");
console.log(bot.username + " - (" + bot.id + ")");
setTimeout(console.log, 9000, bot.inviteURL);
bot.setPresence( {game:"!Roll Help"} );
});
bot.on("message", function(user, userID, channelID, message, event) {
if (user === bot.username){
return;
}
var serverID = bot.channels[channelID].guild_id;
var emojiServerID = serverID;
if (hasPermission(0x00040000, serverID)) {
emojiServerID = HomeServer;
}
if(!(emojiServerID in serverDice)) {
setupServer(emojiServerID);
}
initHandler.handleMessage(message, channelID, serverDice[serverID], serverSymbols[serverID], emojiServerID, sendMessages, printSymbols);
rollHandler.handleMessage(message, channelID, serverDice[serverID], serverSymbols[serverID], emojiServerID, sendMessages, printSymbols);
destinyHandler.handleMessage(message, channelID, serverDice[serverID], serverSymbols[serverID], emojiServerID, sendMessages, printSymbols);
});
bot.on("disconnect", function(err, code) {
try {
console.log("Bot disconnected: " + err + " " + code);
bot.connect(); //Auto reconnect
} catch (err){
console.log(err);
}
});
/*Function declaration area*/
function printSymbols(symbols, serverID) {
var result = "";
symbols.forEach(function(symbol){
result += serverSymbols[serverID][symbol.toLowerCase()].code;
});
return result;
}
function hasPermission(permission, serverID) {
var roleID = bot.servers[serverID].members[bot.id].roles[0];
var role = bot.servers[serverID].roles[roleID];
if (!role) {
return false;
}
return ((role._permissions & permission) == permission);
}
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 = 100;
function _sendMessages() {
setTimeout(function() {
if (messageArr[0]) {
var message = messageArr.shift();
bot.sendMessage({
to: ID,
message: message
}, function(err, res) {
resArr.push(err || res);
if (err != null && err.statusCode == 429) {
sendMessages(ID, [message], err.response.retry_after);
}
if (resArr.length === len) if (typeof(callback) === 'function') callback(resArr);
});
_sendMessages();
}
}, interval);
}
_sendMessages();
}
function setupServer(serverID) {
var server = bot.servers[serverID];
var newConfig = JSON.parse(JSON.stringify(require('./dice.js')));
var diceEM = {
"a":newConfig.ability,
"d":newConfig.difficulty,
"s":newConfig.setback,
"b":newConfig.boost,
"p":newConfig.proficiency,
"c":newConfig.challenge,
"f":newConfig.force
}
var symbols = newConfig.symbols;
var serverEmojis = server.emojis;
// console.log(serverEmojis);
Object.keys(diceEM).forEach(function(dieKey) {
diceEM[dieKey].forEach(function(side){
var emoji = Object.keys(serverEmojis).find(function(it){
return (serverEmojis[it].name === side.face);
});
emoji = serverEmojis[emoji];
if (emoji) {
side.code = "<:"+emoji.name+":"+emoji.id+">";
}
});
});
Object.keys(symbols).forEach(function(symbol) {
var emoji = Object.keys(serverEmojis).find(function(it){
return (serverEmojis[it].name === symbols[symbol].name);
});
emoji = serverEmojis[emoji];
if (emoji) {
symbols[symbol].code = "<:"+emoji.name+":"+emoji.id+">";
}
});
serverDice[serverID] = diceEM;
serverSymbols[serverID] = symbols;
serverSetup[serverID] = true;
}