Skip to content

Commit

Permalink
Merge pull request #503 from Ixrec/recentmessages-quick-wins
Browse files Browse the repository at this point in the history
.recentmessages quick wins
  • Loading branch information
Ixrec authored Nov 10, 2023
2 parents 4ed6d12 + 248f46c commit bf8217f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
31 changes: 18 additions & 13 deletions Izzy-Moonbot/EventListeners/MessageListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,27 @@ private async Task ProcessMessageReceived(
IIzzyMessage message,
IIzzyClient client)
{
// RecentMessages updating

var author = message.Author;
if (!_state.RecentMessages.ContainsKey(author.Id))
_state.RecentMessages[author.Id] = new();
var recentMessages = _state.RecentMessages[author.Id];
recentMessages.Add((message.Timestamp, message.Content));

if (recentMessages.Count > 5)
// RecentMessages updating

if (message.Channel.Id != _config.ModChannel)
{
var secondsUntilIrrelevant = _config.SpamPressureDecay * (_config.SpamMaxPressure / _config.SpamBasePressure);
while (
(DateTimeOffset.UtcNow - recentMessages[0].Item1).TotalSeconds > secondsUntilIrrelevant &&
recentMessages.Count > 5
) {
recentMessages.RemoveAt(0);
if (!_state.RecentMessages.ContainsKey(author.Id))
_state.RecentMessages[author.Id] = new();
var recentMessages = _state.RecentMessages[author.Id];
recentMessages.Add((message.GetJumpUrl(), message.Timestamp, message.Content));

if (recentMessages.Count > 5)
{
var secondsUntilIrrelevant = _config.SpamPressureDecay * (_config.SpamMaxPressure / _config.SpamBasePressure);
while (
(DateTimeOffset.UtcNow - recentMessages[0].Item2).TotalSeconds > secondsUntilIrrelevant &&
recentMessages.Count > 5
)
{
recentMessages.RemoveAt(0);
}
}
}

Expand Down
17 changes: 13 additions & 4 deletions Izzy-Moonbot/Modules/ModMiscModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,14 @@ public async Task SetBannnerCommandAsync([Remainder] string url = "")

[Command("recentmessages")]
[Summary("Dump all of the recent messages Izzy has cached for a specific user.")]
[Remarks("Izzy caches at least 5 messages for each user, and will not throw away a message until it becomes irrelevant for spam pressure (see SpamPressureDecay, SpamMaxPressure and SpamBasePressure). Thus, this includes deleted messages that Izzy can't log in LogChannel because Discord doesn't produce a MessageDeleted event for them (e.g. deletions as part of a ban). Edits and deletes are ignored; only the original version of the message is cached. Restarting Izzy clears this cache.")]
[Remarks(
"This command is useful because some Discord message deletions (most importantly: banning with deletions) do not produce DeletedMessage events, and thus Izzy won't know to log them in LogChannel. This cache is also an implementation detail of some of Izzy's other systems.\n" +
"- Izzy will cache at least 5 messages for each user\n" +
"- Izzy will not throw away a message while it remains relevant for spam pressure calculations (see SpamPressureDecay, SpamMaxPressure and SpamBasePressure)\n" +
"- Edits and deletes are ignored; only the original version of the message is cached\n" +
"- Restarting Izzy clears this cache\n" +
"- Messages in `ModChannel` are ignored"
)]
[RequireContext(ContextType.Guild)]
[ModCommand(Group = "Permissions")]
[DevCommand(Group = "Permissions")]
Expand All @@ -873,13 +880,15 @@ public async Task RecentMessagesCommandAsync([Remainder] string user = "")
!_state.RecentMessages.TryGetValue((ulong)userId, out var recentMessages) ||
recentMessages.Count == 0
) {
await ReplyAsync($"I haven't seen any messages from <@{userId}> since my last restart. Sorry.");
await ReplyAsync($"I haven't seen any messages from <@{userId}> since my last restart. Sorry.", allowedMentions: AllowedMentions.None);
return;
}

await ReplyAsync($"These are all the recent messages (without edits or deletions) I have cached from <@{userId}>:\n" +
await ReplyAsync(
$"These are all the recent messages (without edits or deletions) I have cached from <@{userId}>:\n" +
"\n" +
String.Join("\n", recentMessages.Select(rm => $"[<t:{rm.Item1.ToUnixTimeSeconds()}:R>] {rm.Item2}"))
String.Join("\n", recentMessages.Select(rm => $"[{rm.Item1} <t:{rm.Item2.ToUnixTimeSeconds()}:R>] {rm.Item3}")),
allowedMentions: AllowedMentions.None
);
}
}
3 changes: 2 additions & 1 deletion Izzy-Moonbot/Settings/TransientState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public class TransientState
// RaidService
public List<ulong> RecentJoins = new();

public Dictionary<ulong, List<(DateTimeOffset, string)>> RecentMessages = new();
// (string, DateTimeOffset, string) = (jump URL, timestamp, content)
public Dictionary<ulong, List<(string, DateTimeOffset, string)>> RecentMessages = new();
}

0 comments on commit bf8217f

Please sign in to comment.