-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
bot.js
163 lines (141 loc) · 4.91 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
/*jshint esversion: 9 */
/*
* _____ _ _ ____ _
* |_ _|_ _| | | _| __ ) ___ | |_
* | |/ _` | | |/ / _ \ / _ \| __|
* | | (_| | | <| |_) | (_) | |_
* |_|\__,_|_|_|\_\____/ \___/ \__|
*
* A discord text to speech bot
*
* http://github.com/nullabork/talkbot
*/
(async () => {
//npm imports
require('module-alias/register');
//helpers
const commands = require('@commands'),
figlet = require('figlet'),
botStuff = require('@helpers/bot-stuff'),
Common = require('@helpers/common'),
testing = require('@helpers/runtime-testing');
//models
const world = require('@models/World');
const bot = botStuff.bot;
// runtime testing
await testing.TestIfTTSAPIServicesAreConfigured();
// FANCY SPLASH SCREEN
figlet('TalkBot', (err, data) => console.log(data));
// when the server is ready to go
bot.on('ready', () => {
Common.out('Logged in as: ' + bot.user.username + ' - (' + bot.user.id + ')');
Common.error('LOG CHECKPOINT: THE BOT STARTED. THIS IS NOT AN ERROR MESSAGE');
world.startup();
});
// when the bot is added to new servers
bot.on('guildCreate', (guild) => {
try {
//add the relationships
world.addServer(guild);
} catch (ex) {
Common.error(ex);
}
});
// when the bot is removed from servers
bot.on('guildDelete', (guild) => {
try {
world.removeServer(guild);
} catch (ex) {
Common.error(ex);
}
});
// when a member is removed unfollow
bot.on('guildMemberRemove', (member) => {
try {
const server = world.servers[member.guild.id];
if (server.isMaster(member)) {
server.release();
}
} catch (ex) {
Common.error(ex);
}
});
// handle voice state updates
bot.on('voiceStateUpdate', (oldState, newState) => {
try {
let i = 0;
const server = world.servers[oldState.guild.id];
server.channelJoined(newState);
if (!oldState) return;
if (!server.isMaster(oldState.guild.members.cache.get(oldState.id))) return;
// they've changed voice channels
if (
oldState.channelId &&
(!newState.channelId || !newState.guild.channels.cache.get(newState.channelId).joinable)
) {
server.release();
} else if (oldState.channelId && newState.channelId && oldState.channelId != newState.channelId) {
server.switchVoiceChannel(newState.guild.channels.cache.get(newState.channelId));
}
} catch (ex) {
Common.error(ex);
}
});
// when messages come in
bot.on('messageCreate', (message) => {
try {
// ignore message from myself
if (message.member && message.member.id == bot.user.id) return;
let server = null;
// if its in a server and not a DM
if (message.guild) {
server = world.servers[message.guild.id];
if (server == null) {
Common.error("Can't find server for guild id: " + message.guild.id);
return null;
}
}
// is the message a command?
if (commands.isCommand(message, server)) {
commands.process(message, server, world);
} else if (message.member) {
// say it out loud
//
server.speak(message);
}
} catch (ex) {
Common.error(ex);
}
});
// if we get disconnected???
bot.on('shardDisconnect', (evt, shardID) => {
try {
world.saveAll();
world.dispose();
Common.out(`Shard ${shardID} Disconnected, reconnecting`);
Common.out(evt);
botStuff.connect();
} catch (ex) {
Common.error(ex);
}
});
// capture a whole pile of useful information
bot.on('error', Common.error);
bot.on('guildUnavailable', (guild) => Common.error('guild unavailable: ' + guild.id));
bot.on('rateLimit', (info) => {
Common.error('rate limited');
Common.error(info);
});
bot.on('shardResume', (replayed, shardID) => Common.error(`resume ${shardID}: ` + replayed));
bot.on('warn', (info) => Common.error('warn:' + info));
bot.on('shardReconnecting', (id) => Common.error(`Shard with ID ${id} reconnected.`));
// ctrl-c
process.on('SIGINT', () => world.kill('SIGINT'));
// something goes wrong we didnt think of or having got around to putting a band-aid fix on
process.on('uncaughtException', (err) => {
Common.error(err);
world.kill('uncaughtException: ' + err.message);
});
// start it up!
botStuff.connect();
})(); // wtf async.