-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord-util.js
339 lines (315 loc) · 10.9 KB
/
discord-util.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Helper functions not specific to any particular Discord bot.
const config = require('./config');
const Discord = require('discord.js');
const fs = require('fs');
const moment = require('moment');
const Sleep = require('./sleep');
// Create the Discord client. Does not connect yet.
const client = new Discord.Client({
fetchAllMembers: true,
intents: [
Discord.GatewayIntentBits.DirectMessages,
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMembers,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.GuildMessageReactions,
Discord.GatewayIntentBits.GuildModeration,
Discord.GatewayIntentBits.GuildScheduledEvents,
Discord.GatewayIntentBits.GuildVoiceStates,
Discord.GatewayIntentBits.MessageContent,
],
partials: [
Discord.Partials.Channel,
Discord.Partials.GuildMember,
Discord.Partials.Message,
Discord.Partials.Reaction,
Discord.Partials.User,
]
});
// Set to true once the guild roles have been cached once.
let guildRolesCached = false;
const threeTicks = '```';
// Returns a Promise that resolves when the Discord bot is connected.
async function Connect() {
return new Promise((resolve, reject) => {
client.on('ready', () => {
resolve(client);
});
client.login(config.discordBotToken);
});
}
// Looks up a Discord role by name. Returns the entire structured Discord Role object.
async function GetRoleByName(guild, roleName) {
if (!guildRolesCached) {
await guild.roles.fetch();
guildRolesCached = true;
}
const role = guild.roles.cache.find(role => role.name === roleName);
if (role) {
return role;
} else {
return null;
}
}
// Checks if a Discord guild member has a role. The targetRole is a structured Discord Role object.
async function GuildMemberHasRole(member, targetRole) {
if (!guildRolesCached) {
await member.guild.roles.fetch();
guildRolesCached = true;
}
const foundRole = member.roles.cache.find(role => (role.id === targetRole.id) || (role.id === targetRole));
return foundRole ? true : false;
}
// Adds a role to a GuildMember.
//
// Tries to be efficient by checking if the member already has the role.
async function AddRole(member, role) {
const has = await GuildMemberHasRole(member, role);
if (!role || has) {
return;
}
console.log('Adding role', role.name || role, 'to', member.nickname);
await member.roles.add(role);
}
// Removes a role from a GuildMember.
//
// Tries to be efficient by checking if the member already has the role.
async function RemoveRole(member, role) {
const has = await GuildMemberHasRole(member, role);
if (!role || !has) {
return;
}
console.log('Removing role', role.name || role, 'from', member.nickname);
await member.roles.remove(role);
}
async function GetCategoryChannelByName(channelName) {
const guild = await GetMainDiscordGuild();
for (const [id, channel] of guild.channels.cache) {
if (channel.name === channelName && channel.type === 4) {
return channel;
}
}
return null;
}
async function GetBanCourtCategoryChannel() {
return await GetCategoryChannelByName('Ban Court');
}
// Returns a list of text channels with names that match channelName.
function GetAllMatchingTextChannels(guild, channelName) {
const matchingChannels = [];
guild.channels.cache.forEach((channel) => {
if (channel.name === channelName && channel.type === 0) {
matchingChannels.push(channel);
}
});
return matchingChannels;
}
// The the "main" Discord Guild for the Secret Clan.
async function GetMainDiscordGuild() {
const guildID = '305840605328703500';
const guild = await client.guilds.fetch(guildID);
return guild;
}
// Returns the #public text chat channel in the main discord guild.
async function GetPublicChatChannel() {
const guild = await GetMainDiscordGuild();
const chatroomNames = ['main', 'public', 'general'];
for (const i in chatroomNames) {
const roomName = chatroomNames[i];
const matchingRooms = GetAllMatchingTextChannels(guild, roomName);
if (matchingRooms.length > 0) {
return matchingRooms[0];
}
}
throw 'Failed to find any main chat channel!';
}
// Send a message to the main Discord server's #public channel.
async function MessagePublicChatChannel(discordMessage) {
const channel = await GetPublicChatChannel();
await channel.send(discordMessage);
}
async function UpdateHarmonicCentralityChatChannel(mostCentralUsers) {
const daysAgo = 30;
const currentTime = moment();
const cutoff = currentTime.subtract(daysAgo, 'days');
const guild = await GetMainDiscordGuild();
const channels = GetAllMatchingTextChannels(guild, 'ranks');
if (channels.length === 0) {
throw new Error('Could not find #harmonic-centrality chat channel.');
}
const channel = channels[0];
await channel.bulkDelete(99);
await channel.send(`Harmonic Centrality is a math formula that calculates 'influence' in a social network. It is impartial and fair. Anyone can become a General.\n\nMembers active in the last 30 days are shown in green.`);
const lines = [];
let maxLength = 0;
for (const user of mostCentralUsers) {
const score = user.harmonic_centrality / 1000;
const scoreString = Math.ceil(score).toString(); // To enable commas add .replace(/\B(?=(\d{3})+(?!\d))/g, ',');
maxLength = Math.max(scoreString.length, maxLength);
const paddedScore = scoreString.padStart(maxLength, ' ');
const name = user.getNicknameOrTitleWithInsignia();
let plusOrMinus = '-';
if (user.last_seen) {
const lastSeen = moment(user.last_seen);
if (lastSeen.isAfter(cutoff)) {
plusOrMinus = '+';
}
}
const line = `${plusOrMinus} ${paddedScore} ${name}`;
lines.push(line);
}
await SendLongList(lines, channel, true);
}
async function ParseExactlyOneMentionedDiscordMember(discordMessage) {
// Look for exactly one member being mentioned.
let mentionedMember;
// First, check for explicit @mentions. There must be at most 1 or it's an error.
if (!discordMessage ||
!discordMessage.mentions ||
!discordMessage.mentions.members ||
discordMessage.mentions.members.size < 1) {
// No members mentioned using @mention. Do nothing. A member might be mentioned
// in another way, such as by Discord ID.
} else if (discordMessage.mentions.members.size === 1) {
return discordMessage.mentions.members.first();
} else if (discordMessage.mentions.members.size > 1) {
return null;
}
// Second, check for mentions by full Discord user ID. This will usually be a long
// sequence of digits. Still, finding more than 1 mentioned member is an error.
const tokens = discordMessage.content.split(' ');
// Throw out the first token, which we know is the command itself. Keep only the arguments.
tokens.shift();
for (const token of tokens) {
const isNumber = /^\d+$/.test(token);
if (token.length > 5 && isNumber) {
try {
const guild = await GetMainDiscordGuild();
mentionedMember = await guild.members.fetch(token);
} catch (error) {
console.log('Error while fetching a member from the discord guild:');
console.log(error);
return null;
}
}
}
// We might get this far and find no member mentioned by @ or by ID.
if (!mentionedMember) {
return null;
}
// If we get this far, it means we found exactly one member mentioned,
// whether by @mention or by user ID.
return mentionedMember;
}
// Sends a long list of text strings to a text channel. Breaks it up into
// multiple messages if too big.
async function SendLongList(list, channel, diff) {
const ifDiff = diff ? 'diff\n' : '';
const maxMessageLength = 1900;
let message = '';
for (const s of list) {
message += s + '\n';
if (message.length > maxMessageLength) {
await channel.send(threeTicks + ifDiff + message + threeTicks);
message = '';
}
}
if (message.length > 0) {
await channel.send(threeTicks + ifDiff + message + threeTicks);
}
}
// Deletes recent messages from a member.
//
// Only one of these requests can be in-flight per member at any given time.
const membersWithOngoingMessageDeletion = {};
async function DeleteMessagesByMember(member, maxAgeInSeconds) {
if (member.id in membersWithOngoingMessageDeletion) {
// Use a mutex to enforce only one process per member at a time.
return;
}
membersWithOngoingMessageDeletion[member.id] = 1;
console.log(`Deleting messages by ${member.nickname} from the last ${maxAgeInSeconds} seconds.`);
const maxAgeInMillis = maxAgeInSeconds * 1000;
const currentTime = new Date().getTime();
const cutoffTime = currentTime - maxAgeInMillis;
let failureCount = 0;
while (failureCount < 3) {
await Sleep(1000);
const message = member.lastMessage;
console.log(`Deleting message ID ${message.id}`);
if (!message) {
console.log(`No more messages to delete.`);
failureCount++;
continue;
}
if (message.createdTimestamp < cutoffTime) {
continue;
}
try {
await message.delete();
} catch (error) {
console.log(`Failed to delete a message. ${error}`);
failureCount++;
continue;
}
}
console.log(`Finishes deleting messages from member ${member.nickname}.`);
// Release the lock.
delete membersWithOngoingMessageDeletion[member.id];
}
async function moveMemberToAfk(member) {
const afkLoungeId = '703716669452714054';
await member.voice.setChannel(afkLoungeId, `${member.nickname} has been sent to the AFK channel.`);
return;
}
const lastTimeNameChangedByChannelId = {};
async function TryToSetChannelNameWithRateLimit(channel, newName) {
if (channel.name === newName) {
return;
}
const t = Date.now();
const s = lastTimeNameChangedByChannelId[channel.id] || 0;
const elapsed = t - s;
const tenMinutes = 6 * 60 * 1000;
if (elapsed < tenMinutes) {
return;
}
lastTimeNameChangedByChannelId[channel.id] = t;
// Do not await. This call is known to hang for a long time when rate limited.
// Best thing in such cases is to move on.
channel.setName(newName);
}
const lastTimePermsChangedByChannelId = {};
async function TryToSetChannelPermsWithRateLimit(channel, newPerms) {
const t = Date.now();
const s = lastTimePermsChangedByChannelId[channel.id] || 0;
const elapsed = t - s;
const tenMinutes = 6 * 60 * 1000;
if (elapsed < tenMinutes) {
return;
}
lastTimePermsChangedByChannelId[channel.id] = t;
// Do not await. This call is known to hang for a long time when rate limited.
// Best thing in such cases is to move on.
channel.permissionOverwrites.set(newPerms);
}
module.exports = {
AddRole,
Connect,
DeleteMessagesByMember,
GetAllMatchingTextChannels,
GetBanCourtCategoryChannel,
GetCategoryChannelByName,
GetPublicChatChannel,
GetMainDiscordGuild,
GetRoleByName,
GuildMemberHasRole,
MessagePublicChatChannel,
ParseExactlyOneMentionedDiscordMember,
RemoveRole,
SendLongList,
TryToSetChannelNameWithRateLimit,
TryToSetChannelPermsWithRateLimit,
UpdateHarmonicCentralityChatChannel,
moveMemberToAfk
};