Skip to content

Commit

Permalink
keyword tracking: reduce number of API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
FloatingMilkshake committed Mar 15, 2024
1 parent ffbb287 commit 0393024
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions Helpers/KeywordTrackingHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public static async Task KeywordCheck(DiscordMessage message, bool isEdit = fals
if (message.Channel.IsPrivate)
return;

await Task.Delay(30000);
var messagesAfter30Sec = await message.Channel.GetMessagesAfterAsync(message.Id);

var fields = await Program.Db.HashGetAllAsync("keywords");

foreach (var field in fields)
Expand Down Expand Up @@ -72,23 +75,20 @@ public static async Task KeywordCheck(DiscordMessage message, bool isEdit = fals

// If user is seemingly present and we should assume presence, ignore
if (fieldValue.AssumePresence)
if ((await message.Channel.GetMessagesBeforeAsync(message.Id, 1)).Count > 0)
if ((await message.Channel.GetMessagesBeforeAsync(message.Id, 1))[0].Author.Id == fieldValue.UserId)
continue;
{
var msgBefore = (await message.Channel.GetMessagesBeforeAsync(message.Id, 1))[0];
if (msgBefore != default && msgBefore.Author.Id == fieldValue.UserId) continue;
}

// If keyword is limited to a guild and this is not that guild, ignore
if (fieldValue.GuildId != default && fieldValue.GuildId != message.Channel.Guild.Id)
continue;

// Wait 30 seconds in case the user sends a message; if they do, ignore
await Task.Delay(30000);
var messagesAfter30Sec = await message.Channel.GetMessagesAfterAsync(message.Id);
//var messagesAfter30Sec = await message.Channel.GetMessagesAfterAsync(message.Id);
if (messagesAfter30Sec.Any(x => x.Author.Id == fieldValue.UserId))
continue;

var messages = await message.Channel.GetMessagesAfterAsync(message.Id);
if (messages.Any(m => m.Author.Id == fieldValue.UserId)) continue;

// Don't DM the user if their keyword was mentioned in a channel they do not have permissions to view.
// If we don't do this we may leak private channels, which - even if the user might want to - I don't want to be doing.
if (!message.Channel.PermissionsFor(member).HasPermission(Permissions.AccessChannels))
Expand Down

0 comments on commit 0393024

Please sign in to comment.