TravBot-v3/src/commands/utility/scanemotes.ts

192 lines
9.5 KiB
TypeScript
Raw Normal View History

import {Command, NamedCommand} from "../../core";
import {pluralise} from "../../lib";
2020-12-15 01:44:28 +00:00
import moment from "moment";
import {Collection, TextChannel} from "discord.js";
2020-08-14 17:51:27 +00:00
2020-12-15 01:44:28 +00:00
const lastUsedTimestamps: {[id: string]: number} = {};
2020-08-14 17:51:27 +00:00
export default new NamedCommand({
2020-12-15 01:44:28 +00:00
description:
"Scans all text channels in the current guild and returns the number of times each emoji specific to the guild has been used. Has a cooldown of 24 hours per guild.",
async run({message, channel, guild, author, member, client, args}) {
if (!guild) {
channel.send(`You must use this command on a server!`);
return;
}
2020-12-15 01:44:28 +00:00
// Test if the command is on cooldown. This isn't the strictest cooldown possible, because in the event that the bot crashes, the cooldown will be reset. But for all intends and purposes, it's a good enough cooldown. It's a per-server cooldown.
const startTime = Date.now();
const cooldown = 86400000; // 24 hours
const lastUsedTimestamp = lastUsedTimestamps[guild.id] ?? 0;
2020-12-15 01:44:28 +00:00
const difference = startTime - lastUsedTimestamp;
const howLong = moment(startTime).to(lastUsedTimestamp + cooldown);
// If it's been less than an hour since the command was last used, prevent it from executing.
if (difference < cooldown) {
channel.send(
2020-12-15 01:44:28 +00:00
`This command requires a day to cooldown. You'll be able to activate this command ${howLong}.`
);
return;
} else lastUsedTimestamps[guild.id] = startTime;
2020-12-15 01:44:28 +00:00
const stats: {
[id: string]: {
name: string;
formatted: string;
users: number;
bots: number;
};
} = {};
let totalUserEmoteUsage = 0;
// IMPORTANT: You MUST check if the bot actually has access to the channel in the first place. It will get the list of all channels, but that doesn't mean it has access to every channel. Without this, it'll require admin access and throw an annoying unhelpful DiscordAPIError: Missing Access otherwise.
const allTextChannelsInCurrentGuild = guild.channels.cache.filter(
2020-12-15 01:44:28 +00:00
(channel) => channel.type === "text" && channel.viewable
) as Collection<string, TextChannel>;
let messagesSearched = 0;
let channelsSearched = 0;
let currentChannelName = "";
const totalChannels = allTextChannelsInCurrentGuild.size;
const statusMessage = await channel.send("Gathering emotes...");
2020-12-15 01:44:28 +00:00
let warnings = 0;
channel.startTyping();
2020-12-15 01:44:28 +00:00
// Initialize the emote stats object with every emote in the current guild.
// The goal here is to cut the need to access guild.emojis.get() which'll make it faster and easier to work with.
for (let emote of guild.emojis.cache.values()) {
2020-12-15 01:44:28 +00:00
// If you don't include the "a" for animated emotes, it'll not only not show up, but also cause all other emotes in the same message to not show up. The emote name is self-correcting but it's better to keep the right value since it'll be used to calculate message lengths that fit.
stats[emote.id] = {
name: emote.name,
2020-12-15 07:56:09 +00:00
formatted: `<${emote.animated ? "a" : ""}:${emote.name}:${emote.id}>`,
2020-12-15 01:44:28 +00:00
users: 0,
bots: 0
};
}
2020-10-15 09:23:24 +00:00
2020-12-15 01:44:28 +00:00
const interval = setInterval(() => {
statusMessage.edit(
`Searching channel \`${currentChannelName}\`... (${messagesSearched} messages scanned, ${channelsSearched}/${totalChannels} channels scanned)`
);
}, 5000);
for (const channel of allTextChannelsInCurrentGuild.values()) {
currentChannelName = channel.name;
let selected = channel.lastMessageID ?? message.id;
2020-12-15 01:44:28 +00:00
let continueLoop = true;
2020-10-15 09:23:24 +00:00
2020-12-15 01:44:28 +00:00
while (continueLoop) {
// Unfortunately, any kind of .fetch call is limited to 100 items at once by Discord's API.
const messages = await channel.messages.fetch({
2020-10-15 09:23:24 +00:00
limit: 100,
2020-12-15 01:44:28 +00:00
before: selected
});
if (messages.size > 0) {
for (const msg of messages.values()) {
// It's very important to not capture an array of matches then do \d+ on each item because emote names can have numbers in them, causing the bot to not count them correctly.
const search = /<a?:.+?:(\d+?)>/g;
const text = msg.content;
let match: RegExpExecArray | null;
while ((match = search.exec(text))) {
const emoteID = match[1];
if (emoteID in stats) {
if (msg.author.bot) stats[emoteID].bots++;
else {
stats[emoteID].users++;
totalUserEmoteUsage++;
}
}
}
for (const reaction of msg.reactions.cache.values()) {
const emoteID = reaction.emoji.id;
let continueReactionLoop = true;
let lastUserID: string | undefined;
let userReactions = 0;
let botReactions = 0;
// An emote's ID will be null if it's a unicode emote.
if (emoteID && emoteID in stats) {
// There is a simple count property on a reaction, but that doesn't separate users from bots.
// So instead, I'll use that property to check for inconsistencies.
while (continueReactionLoop) {
// After logging users, it seems like the order is strictly numerical. As long as that stays consistent, this should work fine.
const users = await reaction.users.fetch({
limit: 100,
after: lastUserID
});
if (users.size > 0) {
for (const user of users.values()) {
if (user.bot) {
stats[emoteID].bots++;
botReactions++;
} else {
stats[emoteID].users++;
totalUserEmoteUsage++;
userReactions++;
}
lastUserID = user.id;
}
} else {
// Then halt the loop and send warnings of any inconsistencies.
continueReactionLoop = false;
2020-12-15 07:56:09 +00:00
if (reaction.count !== userReactions + botReactions) {
console.warn(
2020-12-15 01:44:28 +00:00
`[Channel: ${channel.id}, Message: ${msg.id}] A reaction count of ${reaction.count} was expected but was given ${userReactions} user reactions and ${botReactions} bot reactions.`
);
warnings++;
}
}
}
}
}
selected = msg.id;
messagesSearched++;
2020-10-15 09:23:24 +00:00
}
2020-12-15 01:44:28 +00:00
} else {
continueLoop = false;
channelsSearched++;
2020-10-15 09:23:24 +00:00
}
}
2020-12-15 01:44:28 +00:00
}
2020-10-15 09:23:24 +00:00
2020-12-15 01:44:28 +00:00
// Mark the operation as ended.
const finishTime = Date.now();
clearInterval(interval);
statusMessage.edit(
`Finished operation in ${moment.duration(finishTime - startTime).humanize()} with ${pluralise(
warnings,
2020-12-15 01:44:28 +00:00
"inconsistenc",
"ies",
"y"
)}.`
);
console.log(`Finished operation in ${finishTime - startTime} ms.`);
channel.stopTyping();
2020-12-15 01:44:28 +00:00
// Display stats on emote usage.
// This can work outside the loop now that it's synchronous, and now it's clearer what code is meant to execute at the end.
2020-12-15 07:56:09 +00:00
let sortedEmoteIDs = Object.keys(stats).sort((a, b) => stats[b].users - stats[a].users);
2020-12-15 01:44:28 +00:00
const lines: string[] = [];
let rank = 1;
// It's better to send all the lines at once rather than paginate the data because it's quite a memory-intensive task to search all the messages in a server for it, and I wouldn't want to activate the command again just to get to another page.
for (const emoteID of sortedEmoteIDs) {
const emote = stats[emoteID];
const botInfo = emote.bots > 0 ? ` (Bots: ${emote.bots})` : "";
lines.push(
`\`#${rank++}\` ${emote.formatted} x ${emote.users} - ${(
(emote.users / totalUserEmoteUsage) * 100 || 0
).toFixed(3)}%` + botInfo
);
2020-10-15 09:23:24 +00:00
}
await channel.send(lines, {split: true});
2020-10-15 09:23:24 +00:00
}
});