-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
142 lines (130 loc) · 4.37 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
'use strict';
/**
@module timebot
@author WizardCM <[email protected]>
@desc This is the core file of the bot. Run it using `npm run bot`
**/
/* System */
global.Discord = require('discord.js');
global.bot = new Discord.Client();
/* Dependencies */
global.fs = require('fs');
global.moment = require('moment');
global.timezone = require('moment-timezone');
global.storage = require('node-persist'); // Docs: https://github.com/simonlast/node-persist
global.schedule = require('node-schedule');
/* Configuration */
global.botConfig = require('./config/bot.js');
global.colorConfig = require('./config/colors.js');
global.defaultConfig = require('./config/defaults.js');
/* Commands */
global.timeCommand = require('./commands/time.js');
global.raidCommand = require('./commands/raid.js');
// TODO loop through the commands dir and automatically add them
storage.initSync();
let scheduledJob = {};
/**
* @desc Primary event handler for incoming messages
* @listens bot:message
* @param msg {Object} Message object from Discord.js
* @function
*/
function handleMessage(msg) {
/**
* @desc Error response handler function, builds and displays a rich embed
* @param type {string} Type of error
* @param title {string} Custom title string
* @param message {message} Custom message string
*/
let errorResponse = function (type, title, message) {
switch (type) {
case 'set':
title = 'Sorry, it looks like you don\'t have permission to set the time for this server. Please contact a moderator or the owner.';
message = "Have them run `!time` for more details.";
default:
if (!title && !message) {
title = "Something went wrong. Try again in a little while.";
message = "If it doesn't improve, contact the bot author.";
}
break;
}
msg.channel.send(new Discord.RichEmbed({
color: colorConfig.bad,
title: botConfig.title,
description: ' ',
url: '',
fields: [{
name: title,
value: message
}]
}));
};
if (msg.content.indexOf(botConfig.prefix + timeCommand.triggers[0]) == 0) {
// TODO Expanding on proper command import, also overhaul this
timeCommand.run(msg);
} else if (msg.content.indexOf(botConfig.prefix + "raid") == 0 || msg.content.indexOf(botConfig.prefix + "join") == 0 || msg.content.indexOf(botConfig.prefix + "leave") == 0) {
//raidCommand.run(msg);
}
}
bot.on('message', handleMessage);
/**
* @desc Initial launch function
* @listens bot:login
* @function
*/
function handleLogin() {
console.log('Discord Time Bot is now online!');
bot.user.setGame('with ' + botConfig.prefix + 'time');
/**
* @desc Time function that updates the bot's nickname in every server
* @function
*/
function setTime() {
bot.guilds.forEach(function (guild) {
guild.fetchMember(bot.user).then(function (member) {
if (member.id == bot.user.id) {
let data = storage.getItemSync(guild.id);
let thisServer = {};
try {
if (data) {
thisServer = JSON.parse(data);
}
} catch (error) {
console.log("Failed to load data for " + guild.name + ": " + error);
}
if (!Object.keys(thisServer).length) {
member.setNickname("Not Configured");
} else {
// console.log(guild.name + " has: " + JSON.stringify(thisServer));
member.setNickname(moment().tz(thisServer.zone).format(thisServer.format));
}
}
}).catch(function (error) {
console.warn("Failed fetching members.");
});
});
}
setTime();
scheduledJob = schedule.scheduleJob('0 * * * * *', setTime);
}
function handleDisconnect() {
if (scheduledJob) {
scheduledJob.cancel();
}
}
/**
* @desc Attempt to log into Discord's servers. Handle as many errors as we can instead of crashing.
* @function
*/
bot.login(botConfig.token);
bot.on('ready', handleLogin);
bot.on('resume', handleLogin);
bot.on('reconnecting', handleDisconnect);
bot.on('error', handleDisconnect);
bot.on('disconnect', function (event) {
console.warn("Disconnected as Discord's servers are unreachable.");
handleDisconnect();
});
// TODO (node:23452) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 11): Error: getaddrinfo ENOENT discordapp.com:443
// TODO (node:13582) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4156): TypeError: Cannot read property 'options' of undefined
process.on("unhandledRejection", console.error);