-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot.js
301 lines (278 loc) · 12.4 KB
/
bot.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
const cron = require('node-cron');
const path = require('path');
const NodeCache = require("node-cache");
const { Client } = require('discord.js');
const { connect, disconnect } = require('mongoose');
const events = require('./handlers/events.js');
const users = require('./handlers/users.js');
const config = require('./handlers/config.js');
const poll = require('./handlers/poll.js');
const utils = require('./utils/utils.js');
const commands = require('./utils/commands.js');
const theClient = new Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'], intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_MESSAGE_REACTIONS', 'DIRECT_MESSAGES'] });
/**
* scheduled cron for calendar reminders
*/
let calendarReminderCron;
Client.prototype.dnd_users = users;
Client.prototype.dnd_events = events;
Client.prototype.dnd_config = config;
require('log-timestamp')(function () { return `[${new Date().toISOString()}] [shrd:${client.shard.ids}] %s` });
global.VaultVersion = require('./package.json').version;
global.DiscordJSVersion = require('discord.js').version;
global.Config = require(path.resolve(process.env.CONFIGDIR || __dirname, './config.json'));
global.GuildCache = new NodeCache({ stdTTL: 86400, checkperiod: 14400 });
global.client = theClient;
// to get around jest/nodejs import bug
global.COMMANDS = commands.COMMANDS;
/**
* connect to the mongodb
*/
(async () => {
console.info('connecting as mongo user: %s ...', Config.mongoUser);
await connect(`mongodb://${Config.mongoUser}:${Config.mongoPass}@${Config.mongoServer}:${Config.mongoPort}/${Config.mongoSchema}?authSource=${Config.mongoSchema}`, {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true,
useCreateIndex: true
});
console.info('Connected to mongo DB. Logging into Discord now ...');
return client.login(Config.token);
})();
function getClientApp() {
const app = client.api.applications(client.user.id)
if (Config.debugGuild) {
console.info(`Running in debug mode, returning guild only discord client-app`);
// uncomment this line to remove all bot-wide commands (while in debugGuild mode)
// client.api.applications(client.user.id).commands.put({ data: [] });
app.guilds(Config.debugGuild);
}
return app;
}
async function registerCommands() {
console.info('registerCommands: BEGIN');
try {
// console.debug('shard ids:', client.shard.ids);
// only register commands if I'm shard id '0'
if (client.shard.ids.includes(0)) {
console.info(`registerCommands: ShardId:${client.shard.ids}, registering commands ...`);
let commandsToRegister = utils.transformCommandsToDiscordFormat(commands.COMMANDS);
const registeredCommands = await getClientApp().commands.get();
//console.debug('registeredCommands:', registeredCommands);
let registerCommands = utils.checkIfCommandsChanged(registeredCommands, commandsToRegister);
if (registerCommands) {
console.info('registerCommands: command differences between registered and this version - replacing all ...');
await getClientApp().commands.put({ data: commandsToRegister });
} else {
console.info('registerCommands: no command differences found between registered and this version - nothing to register');
}
}
} catch (error) {
console.error('registerCommands:', error);
}
console.info('registerCommands: END');
}
/**
* listen for emitted events from discordjs
*/
client.once('ready', async () => {
console.info(`D&D Vault Bot - logged in as ${client.user.tag} & ${client.user.id}`);
client.user.setPresence({ activities: [{ name: 'with Tiamat, type /help', type: 'PLAYING' }], status: 'online' });
registerCommands();
calendarReminderCron = cron.schedule(Config.calendarReminderCron, async () => {
await events.sendReminders(client);
await events.recurEvents(client);
await events.removeOldSessionPlanningChannels(client);
await events.removeOldSessionVoiceChannels(client);
await events.removeOldEventPosts(client);
});
});
/**
* guildCreate
*/
client.on("guildCreate", async (guild) => {
console.log(`guildCreate: ${guild.id} (${guild.name})`);
try {
await config.confirmGuildConfig(guild);
let channel = await utils.locateChannelForMessageSend(guild);
if (channel) {
await channel.send({ content: 'Thanks for inviting me! Use the slash command `/help` to find out how to interact with me. Roll initiative!' });
}
} catch (error) {
console.error("guildCreate:", error);
}
});
/**
* guildDelete
*/
client.on("guildDelete", async (guild) => {
console.log(`guildDelete: ${guild.id} (${guild.name}) because of: ${guild.unavailable ? guild.unavailable : 'KICKED'}`);
// if bot was kicked from guild, then this 'unavailable' field will not be populated, otherwise it is just temp unavailable
if (!guild.unavailable) {
try {
await utils.removeAllDataForGuild(guild);
} catch (error) {
console.error('guildDelete:', error);
}
}
});
client.on('messageReactionAdd', async (reaction, user) => {
// When we receive a reaction we check if the reaction is partial or not
try {
if (reaction.partial) {
// If the message this reaction belongs to was removed the fetching might result in an API error, which we need to handle
await reaction.fetch();
}
if (reaction.message.partial) {
await reaction.message.fetch();
}
} catch (error) {
console.error(`messageReactionAdd: Something went wrong when fetching the reaction.message for a partial: ${error.message}`);
// Return as `reaction.message.author` may be undefined/null
return;
}
if (reaction.message.author.id === reaction.message.guild.me.id) {
console.info(`messageReactionAdd:${reaction.message.guild.name}:${user.username}(bot?${user.bot}):${reaction.emoji.name}:${reaction.message.content}`);
if (!user.bot) {
try {
// Now the message has been cached and is fully available
await utils.checkChannelPermissions(reaction.message);
let guildConfig = await config.confirmGuildConfig(reaction.message.guild);
if (reaction.message.embeds[0]?.author?.name.startsWith(poll.POLLSTER_AUTHOR)) {
console.debug(`messageReactionAdd:POLL:${reaction.message.author}'s"${reaction.message.id}" gained a reaction!`);
await poll.handleReactionAdd(reaction, user, guildConfig);
} else {
console.debug(`messageReactionAdd:EVENT:${reaction.message.author}'s "${reaction.message.id}" gained a reaction!`);
await events.handleReactionAdd(reaction, user, guildConfig);
}
} catch (error) {
console.error(`messageReactionAdd:caught exception handling reaction`, error);
await utils.sendDirectOrFallbackToChannelError(error, reaction.message, user);
// we got an exception, let's make sure to remove this user's reaction
await reaction.users.remove(user.id);
}
}
}
});
/**
* handle slash command interactions
*/
client.on('interactionCreate', async (interaction) => {
// client.ws.on('INTERACTION_CREATE', async (interaction) => {
let msg = {
interaction: interaction,
url: utils.getDiscordUrl(interaction.guildId, interaction.channelId, interaction.id),
};
// console.debug(`interaction debug: `, interaction);
// const { name, options } = interaction.data;
const command = interaction.commandName;
try {
// console.debug("interactionCreate:", interaction);
if (interaction.channelId) {
console.debug('interactionCreate: populating channel');
let channel = await client.channels.resolve(interaction.channelId);
msg.channel = channel;
}
if (interaction.member) {
console.debug('interactionCreate: populating member & guild');
msg.member = interaction.member;
msg.guild = interaction.member.guild;
}
if (!msg.guild && interaction.guildId) {
console.debug('interactionCreate: populating guild');
let guild = await client.guilds.resolve(interaction.guildId);
msg.guild = guild;
}
if (interaction.user) {
console.debug('interactionCreate: populating author');
msg.author = interaction.user;
if (!msg.member && msg.guild) {
console.debug('interactionCreate: populating member');
msg.member = await msg.guild.members.fetch(interaction.user);
}
}
let guildConfig = await config.confirmGuildConfig(msg.guild);
//let commandPrefix = guildConfig ? guildConfig.prefix : Config.defaultPrefix;
await commands.handleCommandExec(guildConfig, command, msg, interaction.options.data);
} catch (error) {
console.error(`clientOninteractionCreate: msg NOT processed:${msg.interaction ? 'INTERACTION:' : ''}${msg.guild ? msg.guild.name : "DIRECT"}:${msg.author.tag}${msg.member ? "(" + msg.member.displayName + ")" : ""}:${command}:${error.message}`);
await utils.sendDirectOrFallbackToChannelError(error, msg);
}
});
client.on('messageCreate', async (msg) => {
try {
if (msg.partial) {
// If the message this was removed the fetching might result in an API error, which we need to handle
try {
await msg.fetch();
} catch (error) {
console.error(`clientOnMessage: Something went wrong when fetching the message for a partial: ${error.message}`);
// Return as `reaction.message.author` may be undefined/null
return;
}
}
if (msg.author.bot) {
// don't do anything if this message was authored by a bot
return;
}
let guildConfig = await config.confirmGuildConfig(msg.guild);
let commandPrefix = guildConfig ? guildConfig.prefix : Config.defaultPrefix;
let messageContentLowercase = msg.content.toLowerCase();
/**
* handle commands that don't require a guild interaction (can be direct messaged)
*/
if (messageContentLowercase.startsWith(commandPrefix)) {
// remove the prefix from content
messageContentLowercase = messageContentLowercase.substring(commandPrefix.length);
} else if (msg.guild) {
// don't do anything if the proper commandprefix isn't there and there is a guild
return;
}
await commands.handleCommandExec(guildConfig, messageContentLowercase, msg);
} catch (error) {
console.error(`clientOnMessage: msg NOT processed:${msg.interaction ? 'INTERACTION:' : ''}${msg.guild ? msg.guild.name : "DIRECT"}:${msg.author.tag}${msg.member ? "(" + msg.member.displayName + ")" : ""}:${msg.content}:${error.message}`);
await utils.sendDirectOrFallbackToChannelError(error, msg);
}
});
process.on('SIGTERM', async () => {
console.info('SIGTERM signal received.');
await cleanShutdown(true);
});
process.on('SIGINT', async () => {
console.info('SIGINT signal received.');
await cleanShutdown(true);
});
process.on('SIGUSR1', async () => {
console.info('SIGUSR1 signal received.');
await cleanShutdown(true);
});
process.on('SIGUSR2', async () => {
console.info('SIGUSR2 signal received.');
await cleanShutdown(true);
});
process.on('uncaughtException', async (error) => {
console.info('uncaughtException signal received.', error);
// await cleanShutdown(true);
});
/**
*
* @param {boolean} callProcessExit
*/
async function cleanShutdown(callProcessExit) {
try {
console.log('Closing out shard resources...');
calendarReminderCron.stop();
console.log('Scheduled calendar recuring destroyed.');
client.destroy();
console.log('Discord client destroyed.');
// boolean means [force], see in mongoose doc
await disconnect();
console.log('MongoDb connection closed.');
} catch (error) {
console.error("could not cleanly shutdown shard", error);
}
if (callProcessExit) {
console.log('Exiting.');
process.exit(0);
}
}