forked from WerewolvesRevamped/Werewolves-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhispers.js
More file actions
307 lines (287 loc) · 14.1 KB
/
whispers.js
File metadata and controls
307 lines (287 loc) · 14.1 KB
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
302
303
304
305
306
307
/*
Module for whispering
- Connects channels (with optional disguises)
- Converts yourself into a bot
*/
module.exports = function() {
/* Handles connection command */
this.cmdConnection = function(message, args) {
// Check subcommand
if(!args[0]) {
message.channel.send("⛔ Syntax error. Not enough parameters! Correct usage: `connection [add|remove|reset]`");
return;
}
// Find subcommand
switch(args[0]) {
case "add": cmdConnectionAdd(message.channel, args); break;
case "remove": cmdConnectionRemove(message.channel); break;
case "send": cmdConnectionSend(message.channel, args); break;
case "reset": cmdConfirm(message, "connection reset"); break;
default: message.channel.send("⛔ Syntax error. Invalid subcommand `" + args[0] + "`!"); break;
}
}
this.cmdImpersonate = function(message, argsX) {
let author = getUser(message.channel, argsX.shift());
if(author) cmdWebhook(message.channel, message.guild.members.cache.get(author), argsX);
}
this.cmdWebhookDirect = function(message, argsX) {
if(!message.author.bot) {
cmdWebhook(message.channel, message.member, argsX);
} else {
let msg = ["Leave me alone.", "Please just stop.", "Why are you doing this?","What have I done to deserves this.","No.","Just no.","Seriously, no.","No means no.","Go away.","Why do you hate me?","What have I ever done to you?","I don't want to be part of your evil plots.","I'm a friendly bot, why are you trying to make me do this?","I just want to be nice, not annoying.","Please go away.","Why...","Stop...",":("];
message.channel.send(msg[Math.floor(Math.random() * msg.length)]);
}
}
/* Help for this module */
this.helpWhispers = function(member, args) {
let help = "";
switch(args[0]) {
case "":
help += stats.prefix + "webhook - Repeats a message as a webhook pretending to be you\n";
if(isGameMaster(member)) help += stats.prefix + "impersonate - Repeats a message as a webhook pretending to be somebody\n";
if(isGameMaster(member)) help += stats.prefix + "connection [add|remove|reset] - Manages connections\n";
break;
case "webhook":
help += "```yaml\nSyntax\n\n" + stats.prefix + "webhook <Message>\n```";
help += "```\nFunctionality\n\nRepeats a message as a webhook pretending to be you.\n```";
help += "```fix\nUsage\n\n> " + stats.prefix + "webhook Does this work?\n< Does this work?\n```";
help += "```diff\nAliases\n\n- bot\n- <\n```";
break;
case "impersonate":
help += "```yaml\nSyntax\n\n" + stats.prefix + "impersonate <User> <Message>\n```";
help += "```\nFunctionality\n\nRepeats a message as a webhook pretending to be a certain user.\n```";
help += "```fix\nUsage\n\n> " + stats.prefix + "impersonate 242983689921888256 Does this work?\n< Does this work?\n```";
help += "```diff\nAliases\n\n- imp\n```";
break;
case "connection":
if(!isGameMaster(member)) break;
switch(args[1]) {
default:
help += "```yaml\nSyntax\n\n" + stats.prefix + "connection [add|remove|reset]\n```";
help += "```\nFunctionality\n\nGroup of commands to handle connected channels. " + stats.prefix + "help connection <sub-command> for detailed help.```";
help += "```diff\nAliases\n\n- con\n- whispers\n```";
break;
case "add":
help += "```yaml\nSyntax\n\n" + stats.prefix + "connection add <Connection Name> <Connection Diguise>\n```";
help += "```\nFunctionality\n\nConnects the channel to <Connection Name>. All channels that are connected to the same Connection Name, automatically have all messages copied over to all other channels in the connection, and receive copies from all messages from the other channels in it. If a disguise <Connection Disguise> is set, messages are copied over to other channels using the disguise as a name, instead of the name of a message's author.\n```";
help += "```fix\nUsage\n\n> " + stats.prefix + "connection add example\n< ✅ Added connection example with no disguise!\n\n> " + stats.prefix + "connection add example Disguised\n< ✅ Added connection example with disguise Disguised!```";
break;
case "remove":
help += "```yaml\nSyntax\n\n" + stats.prefix + "connection remove\n```";
help += "```\nFunctionality\n\nRemoves all connections from the current channel.\n```";
help += "```fix\nUsage\n\n> " + stats.prefix + "connection remove\n< ✅ Removed all connections from this channel!\n```";
break;
case "reset":
help += "```yaml\nSyntax\n\n" + stats.prefix + "connection reset\n```";
help += "```\nFunctionality\n\nRemoves all connections from all channels.\n```";
help += "```fix\nUsage\n\n> " + stats.prefix + "connection reset\n< ❗ Click the reaction in the next 20.0 seconds to confirm " + stats.prefix + "connection reset!\n< ✅ Successfully reset connections!\n```";
break;
case "send":
help += "```yaml\nSyntax\n\n" + stats.prefix + "connection send <Connection Name> <Connection Disguise> <Text>\n```";
help += "```\nFunctionality\n\nSend a message <Text> with disguise <Connection Disguise> over a connection <Connection Name>.\n```";
help += "```fix\nUsage\n\n> " + stats.prefix + "connection send bartender fakebartender hi\n```";
break;
}
break;
}
return help;
}
/* Adds a connection */
this.cmdConnectionAdd = function(channel, args) {
// Check arguments
if(!args[1]) {
channel.send("⛔ Syntax error. Not enough parameters!");
return;
} else if(!args[2]) {
args[2] = "";
}
// Add connection to DB
sql("INSERT INTO connected_channels (channel_id, id, name) VALUES (" + connection.escape(channel.id) + "," + connection.escape(args[1]) + "," + connection.escape(args[2]) + ")", result => {
if(args[2] != "") {
// Connection w/ disguise
channel.send("✅ Added connection `" + args[1] + "` with disguise `" + toTitleCase(args[2]) + "`!");
log("Whispers > Created connection `" + args[1] + "` with disguise `" + toTitleCase(args[2]) + "`!");
} else {
// Connection w/o disguise
channel.send("✅ Added connection `" + args[1] + "` with no disguise!");
log("Whispers > Created connection `" + args[1] + "` with no disguise!");
}
}, () => {
// Couldn't add connection
channel.send("⛔ Database error. Couldn't add connection `" + args[1] + "`!");
});
}
/* Removes a connection */
this.cmdConnectionRemove = function(channel) {
// Remove connections from DB
sql("DELETE FROM connected_channels WHERE channel_id = " + connection.escape(channel.id), result => {
channel.send("✅ Removed all connections from this channel!");
log("Whispers > Removed connections from `" + channel.id + "`!");
}, () => {
// Database error
channel.send("⛔ Database error. Couldn't remove connections!");
});
}
/* Rests all connections */
this.cmdConnectionReset = function(channel) {
sql("DELETE FROM connected_channels", result => {
channel.send("✅ Successfully reset connections!");
}, () => {
channel.send("⛔ Database error. Could not reset connections!");
});
}
this.cmdWebhook = function(channel, member, args) {
// Create a webhook for the author
let webhookName = member ? member.displayName : client.user.username;
let webhookAvatar = member ? member.user.displayAvatarURL() : client.user.displayAvatarURL();
let webhookMsg = args.join(" ");
webhookMsg = webhookMsg.replace(/:~/g, ":");
if(!(webhookMsg.length > 0)) webhookMsg = "|| ||";
channel.fetchWebhooks()
.then(webhooks => {
// search for webhook
let webhook = webhooks.find(w => w.name == webhookName);
// webhook exists
if(webhook) {
webhook.send(webhookMsg);
} else { // no webhook
if(webhooks.size < 10) { // empty slot
channel.createWebhook(webhookName, {avatar: webhookAvatar})
.then(webhook => {
// Send webhook
webhook.send(webhookMsg)
})
.catch(err => {
// Webhook couldn't be created
logO(err);
sendError(messsage.channel, err, "Could not create webhook");
});
} else { // no empty slot
channel.send("**" + webhookName + "**: " + webhookMsg);
webhooks.first().delete();
}
}
});
}
/* Copies over messages */
this.connectionExecute = function(message) {
if(connection && !message.author.bot && message.content.indexOf(stats.prefix) !== 0) {
// Find connection id(s)
sql("SELECT id, name FROM connected_channels WHERE channel_id = " + connection.escape(message.channel.id), result => {
// For each connection id, find each connected channel
result.forEach(source => {
sql("SELECT channel_id, name FROM connected_channels WHERE id = " + connection.escape(source.id), result => {
// Write message in each channel
result.forEach(async destination => {
// Ignore if it's same channel as source
if(destination.channel_id != message.channel.id) {
// Create webhook
let pdis = idToDisguise(message.author.id);
let disguiseName = source.name;
let disguiseAvatar = client.user.displayAvatarURL();
// role icon
let roleIcon = await getIconFromName(disguiseName);
if(roleIcon) disguiseAvatar = roleIcon;
// player disguise
if(disguiseName && disguiseName.search(/%n/) != -1) {
disguiseName = disguiseName.replace(/%n|%N/, pdis[0]);
disguiseAvatar = pdis[1];
}
let webhookName = disguiseName != "" ? toTitleCase(disguiseName) : message.member.displayName;
let webhookAvatar = disguiseName != "" ? disguiseAvatar : message.author.displayAvatarURL();
let webhookMsg = message.content;
webhookMsg = webhookMsg.replace(/:~/g, ":");
message.guild.channels.cache.get(destination.channel_id).fetchWebhooks()
.then(webhooks => {
// search for webhook
let webhook = webhooks.find(w => w.name == webhookName);
// webhook exists
if(webhook) {
webhook.send(webhookMsg);
} else { // no webhook
if(webhooks.size < 10) { // empty slot
message.guild.channels.cache.get(destination.channel_id).createWebhook(webhookName, {avatar: webhookAvatar})
.then(webhook => {
// Send webhook
webhook.send(webhookMsg)
})
.catch(err => {
// Webhook couldn't be created
logO(err);
sendError(messsage.channel, err, "Could not create webhook");
});
} else { // no empty slot
message.guild.channels.cache.get(destination.channel_id).send("**" + webhookName + "**: " + webhookMsg);
webhooks.first().delete();
}
}
});
}
});
}, () => {
// Database error
log("⛔ Database error. Could not access connected channels via id!");
});
});
}, () => {
// Database error
log("⛔ Database error. Could not access connected channels via channel!");
});
}
}
/* Send message over a connection */
this.cmdConnectionSend = function(channel, args) {
// Check arguments
if(!args[1] || !args[2] || !args[3]) {
channel.send("⛔ Syntax error. Not enough parameters!");
return;
}
// set values
let conn = args[1];
let disguise = typeof args[2] === 'string' ? toTitleCase(args[2]) : "";
let text = args[3];
sql("SELECT channel_id, name FROM connected_channels WHERE id = " + connection.escape(conn), result => {
// Write message in each channel
result.forEach(destination => {
// Create webhook
let webhookMsg = text;
webhookMsg = webhookMsg.replace(/:~/g, ":");
if(disguise.length > 0) {
let webhookName = disguise;
let webhookAvatar = client.user.displayAvatarURL();
channel.guild.channels.cache.get(destination.channel_id).fetchWebhooks()
.then(webhooks => {
// search for webhook
let webhook = webhooks.find(w => w.name == webhookName);
// webhook exists
if(webhook) {
webhook.send(webhookMsg);
} else { // no webhook
if(webhooks.size < 10) { // empty slot
channel.guild.channels.cache.get(destination.channel_id).createWebhook(webhookName, {avatar: webhookAvatar})
.then(webhook => {
// Send webhook
webhook.send(webhookMsg)
})
.catch(err => {
// Webhook couldn't be created
logO(err);
sendError(channel, err, "Could not create webhook");
});
} else { // no empty slot
channel.guild.channels.cache.get(destination.channel_id).send("**" + webhookName + "**: " + webhookMsg);
webhooks.first().delete();
}
}
});
} else {
let wchannel = channel.guild.channels.cache.get(destination.channel_id);
wchannel.send(webhookMsg);
}
});
}, () => {
// Database error
log("⛔ Database error. Could not access connected channels via id!");
});
}
}