Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reimplement spam pressure calculation with a more stateless and properly synchronized usage of RecentMessages instead of users.conf #523

Merged
merged 11 commits into from
Nov 16, 2023
Merged
25 changes: 0 additions & 25 deletions Izzy-Moonbot/EventListeners/MessageListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,6 @@ private async Task ProcessMessageReceived(
IIzzyClient client)
{
var author = message.Author;

// RecentMessages updating

if ((message.Channel.Id != _config.ModChannel) && DiscordHelper.IsInGuild(message))
{
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);
}
}
}

// Witty processing

if (author.Id == client.CurrentUser.Id) return; // Don't process self.
if (author.IsBot) return; // Don't listen to bots

Expand Down
4 changes: 0 additions & 4 deletions Izzy-Moonbot/Modules/DevModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ [Remainder] [Summary("Test arguments")]

var paginationHelper = new PaginationHelper(Context, pages, staticParts);
break;
case "pressure-hook":
await Context.Message.ReplyAsync(
$"**Test utility** - Pressure hookin test.\n*Other services or modules can hook into the pressure service to do specific things.*\n*An example of this is getting pressure for a user.*\n*Like, your current pressure is `{_pressureService.GetPressure(Context.User.Id)}`*");
break;
case "dump-users-size":
await Context.Message.ReplyAsync($"UserStore size: {_users.Count}");
break;
Expand Down
2 changes: 1 addition & 1 deletion Izzy-Moonbot/Modules/ModMiscModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ public async Task RecentMessagesCommandAsync([Remainder] string user = "")
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 => $"[{rm.Item1} <t:{rm.Item2.ToUnixTimeSeconds()}:R>] {rm.Item3}")),
String.Join("\n", recentMessages.Select(rm => $"[{rm.GetJumpUrl()} <t:{rm.Timestamp.ToUnixTimeSeconds()}:R>] {rm.Content}")),
allowedMentions: AllowedMentions.None
);
}
Expand Down
46 changes: 0 additions & 46 deletions Izzy-Moonbot/Modules/SpamModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,50 +58,4 @@ public async Task TestableGetPressureAsync(
await context.Channel.SendMessageAsync($"Current Pressure for {user.DisplayName} ({user.Username}/{user.Id}): {pressure}");
}
}

[Command("getmessages")]
[Summary("Get a user's previous messages (the messages which would have been deleted if the user spammed).")]
[ModCommand(Group = "Permissions")]
[DevCommand(Group = "Permissions")]
[Parameter("user", ParameterType.UserResolvable, "The user to get the messages of, or yourself if no user is provided.", true)]
public async Task GetPreviousMessagesAsync(
[Remainder] string userName = "")
{
await TestableGetPreviousMessagesAsync(
new SocketCommandContextAdapter(Context),
userName
);
}

public async Task TestableGetPreviousMessagesAsync(
IIzzyContext context,
string userName = "")
{
// If no target is specified, target self.
if (userName == "") userName = $"<@{context.User.Id}>";

var (userId, userError) = await ParseHelper.TryParseUserResolvable(userName, context.Guild!);
if (userId == null)
{
await context.Channel.SendMessageAsync($"Failed to get user id from \"{userName}\": {userError}");
return;
}

var user = context.Guild?.GetUser((ulong)userId);
if (user == null)
{
await context.Channel.SendMessageAsync($"Couldn't find <@{userId}> in this server", allowedMentions: AllowedMentions.None);
}
else
{
var previousMessages = _spamService.GetPreviousMessages(user.Id);

var messageList = previousMessages.Select(item =>
$"https://discord.com/channels/{item.GuildId}/{item.ChannelId}/{item.Id} at <t:{item.Timestamp.ToUniversalTime().ToUnixTimeSeconds()}:F> (<t:{item.Timestamp.ToUniversalTime().ToUnixTimeSeconds()}:R>)"
);

await context.Channel.SendMessageAsync(
$"I consider the following messages from {user.DisplayName} ({user.Username}/{user.Id}) to be recent: \n{string.Join('\n', messageList)}\n*Note that these messages may not actually be recent as their age is only checked when the user sends more messages.*");
}
}
}
Loading
Loading