aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIRONM00N <64110067+IRONM00N@users.noreply.github.com>2022-04-02 18:51:25 -0400
committerIRONM00N <64110067+IRONM00N@users.noreply.github.com>2022-04-02 18:51:25 -0400
commit4fbc1e0f743e81e718762679c0d3e842c2a58ee9 (patch)
tree873cea77719e946a798283e545f7522ff56cd596 /src
parent47be8a310c0954b078fb4ea0ffd5cad2a5725a71 (diff)
downloadtanzanite-4fbc1e0f743e81e718762679c0d3e842c2a58ee9.tar.gz
tanzanite-4fbc1e0f743e81e718762679c0d3e842c2a58ee9.tar.bz2
tanzanite-4fbc1e0f743e81e718762679c0d3e842c2a58ee9.zip
feat(hl): add last talked cooldown
Diffstat (limited to 'src')
-rw-r--r--src/lib/common/HighlightManager.ts63
-rw-r--r--src/listeners/message/highlight.ts3
2 files changed, 59 insertions, 7 deletions
diff --git a/src/lib/common/HighlightManager.ts b/src/lib/common/HighlightManager.ts
index 4347c27..6781f50 100644
--- a/src/lib/common/HighlightManager.ts
+++ b/src/lib/common/HighlightManager.ts
@@ -3,6 +3,10 @@ import assert from 'assert';
import { Collection, type Snowflake } from 'discord.js';
import { Time } from '../utils/BushConstants.js';
+const NOTIFY_COOLDOWN = 5 * Time.Minute;
+const OWNER_NOTIFY_COOLDOWN = 1 * Time.Minute;
+const LAST_MESSAGE_COOLDOWN = 5 * Time.Minute;
+
type users = Set<Snowflake>;
type channels = Set<Snowflake>;
type word = HighlightWord;
@@ -154,7 +158,7 @@ export class HighlightManager {
highlight.words = util.addToArray(highlight.words, hl);
- return !!(await highlight.save().catch(() => false));
+ return Boolean(await highlight.save().catch(() => false));
}
/**
@@ -181,7 +185,7 @@ export class HighlightManager {
highlight.words = util.removeFromArray(highlight.words, toRemove);
- return !!(await highlight.save().catch(() => false));
+ return Boolean(await highlight.save().catch(() => false));
}
/**
@@ -205,20 +209,52 @@ export class HighlightManager {
highlight.words = [];
- return !!(await highlight.save().catch(() => false));
+ return Boolean(await highlight.save().catch(() => false));
}
+ /**
+ * Sends a user a direct message to alert them of their highlight being triggered.
+ * @param message The message that triggered the highlight.
+ * @param user The user who's highlights was triggered.
+ * @param hl The highlight that was matched.
+ * @returns Whether or a dm was sent.
+ */
public async notify(message: BushMessage, user: Snowflake, hl: HighlightWord): Promise<boolean> {
assert(message.inGuild());
- if (this.lastedDMedUserCooldown.has(user)) {
- const lastDM = this.lastedDMedUserCooldown.get(user)!;
- if (new Date().getTime() - lastDM.getTime() < 5 * Time.Minute) {
+ dmCooldown: {
+ const lastDM = this.lastedDMedUserCooldown.get(user);
+ if (!lastDM) break dmCooldown;
+
+ const cooldown = client.ownerID.includes(user) ? OWNER_NOTIFY_COOLDOWN : NOTIFY_COOLDOWN;
+
+ if (new Date().getTime() - lastDM.getTime() < cooldown) {
void client.console.verbose('Highlight', `User <<${user}>> has been dmed recently.`);
return false;
}
}
+ talkCooldown: {
+ const lastTalked = this.userLastTalkedCooldown.get(message.guildId)?.get(user);
+ if (!lastTalked) break talkCooldown;
+
+ const now = new Date().getTime();
+ const talked = lastTalked.getTime();
+
+ if (now - talked < LAST_MESSAGE_COOLDOWN) {
+ void client.console.verbose('Highlight', `User <<${user}>> has talked too recently.`);
+
+ setTimeout(() => {
+ const newTalked = this.userLastTalkedCooldown.get(message.guildId)?.get(user)?.getTime();
+ if (talked !== newTalked) return;
+
+ void this.notify(message, user, hl);
+ }, LAST_MESSAGE_COOLDOWN).unref();
+
+ return false;
+ }
+ }
+
const recentMessages = message.channel.messages.cache
.filter((m) => m.createdTimestamp <= message.createdTimestamp && m.id !== message.id)
.filter((m) => m.cleanContent?.trim().length > 0)
@@ -254,4 +290,19 @@ export class HighlightManager {
})
.catch(() => false);
}
+
+ /**
+ * Updates the time that a user last talked in a particular guild.
+ * @param message The message the user sent.
+ */
+ public updateLastTalked(message: BushMessage): void {
+ if (!message.inGuild()) return;
+ const lastTalked = (
+ this.userLastTalkedCooldown.has(message.guildId)
+ ? this.userLastTalkedCooldown
+ : this.userLastTalkedCooldown.set(message.guildId, new Collection())
+ ).get(message.guildId)!;
+
+ lastTalked.set(message.author.id, new Date());
+ }
}
diff --git a/src/listeners/message/highlight.ts b/src/listeners/message/highlight.ts
index c3a6ce0..d7965c5 100644
--- a/src/listeners/message/highlight.ts
+++ b/src/listeners/message/highlight.ts
@@ -12,8 +12,9 @@ export default class HighlightListener extends BushListener {
public override async exec(...[message]: BushClientEvents['messageCreate']) {
if (!message.inGuild()) return;
if (message.author.bot || message.system) return;
- if (!((await message.guild.hasFeature('highlight')) && !message.author.isOwner())) return; // allows highlighting to be disabled on a guild-by-guild basis
+ if (!(await message.guild.hasFeature('highlight'))) return; // allows highlighting to be disabled on a guild-by-guild basis
+ client.highlightManager.updateLastTalked(message);
const res = client.highlightManager.checkMessage(message);
for (const [user, hl] of res.entries()) {