Skip to content

Commit

Permalink
Add 'channels' option to /tracking add
Browse files Browse the repository at this point in the history
Allows filtering tracking to specific channels
  • Loading branch information
FloatingMilkshake committed Oct 25, 2024
1 parent 1568aec commit 67a4a77
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 10 deletions.
76 changes: 75 additions & 1 deletion Commands/InteractionCommands/TrackingInteractions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,85 @@ internal class TrackingInteractions : ApplicationCommandModule
public class TrackingSlashCommands
{
[SlashCommand("add", "Track a users messages.")]
public async Task TrackingAddSlashCmd(InteractionContext ctx, [Option("member", "The member to track.")] DiscordUser discordUser)
public async Task TrackingAddSlashCmd(InteractionContext ctx, [Option("member", "The member to track.")] DiscordUser discordUser, [Option("channels", "Optional channels to filter to. Use IDs or mentions, and separate with commas or spaces.")] string channels = "")
{
await ctx.DeferAsync(ephemeral: false);

var channelsUpdated = false;

// Resolve list of filter channels
List<ulong> filterChannels = new();
if (!string.IsNullOrEmpty(channels))
{
channels = channels.Replace(", ", ","); // "#general-chat, #lounge" ~> "#general-chat,#lounge"
var channelIds = channels.Split(' ', ',');
foreach (var channel in channelIds)
{
// If this is a channel mention, get the ID first
var channelId = channel.Replace("<#", "").Replace(">", "");

if (ulong.TryParse(channelId, out var id))
{
if (!filterChannels.Contains(id))
filterChannels.Add(id);
}
else
{
// Invalid ID; couldn't parse as ulong
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"{Program.cfgjson.Emoji.Error} I couldn't parse \"{channel}\" as a channel ID or mention! Please double-check it and try again."));
return;
}
}
}

// If we were passed nothing, filterChannels remains an empty List. Otherwise, it is populated with the parsed channel IDs

// Compare to db; if there is a mismatch, replace whatever is already in the db with what was passed to this command
if (Program.db.HashExists("trackingChannels", discordUser.Id))
{
var dbChannels = Program.db.HashGet("trackingChannels", discordUser.Id).ToString();
string cmdChannels;
if (filterChannels.Count < 1)
{
// No channels were passed. If there are any in the db, remove them
if (await Program.db.HashExistsAsync("trackingChannels", discordUser.Id))
{
await Program.db.HashDeleteAsync("trackingChannels", discordUser.Id);
channelsUpdated = true;
}
}
else
{
cmdChannels = JsonConvert.SerializeObject(filterChannels);
if (dbChannels != cmdChannels)
{
// Passed channels do not match db channels, update db
var newChannels = JsonConvert.SerializeObject(filterChannels);
await Program.db.HashSetAsync("trackingChannels", discordUser.Id, newChannels);
channelsUpdated = true;
}
}
}
else
{
// No channels in db; just add whatever was passed
// If nothing was passed, don't add anything
if (filterChannels.Count > 0)
{
var newChannels = JsonConvert.SerializeObject(filterChannels);
await Program.db.HashSetAsync("trackingChannels", discordUser.Id, newChannels);
channelsUpdated = true;
}
}

if (Program.db.SetContains("trackedUsers", discordUser.Id))
{
if (channelsUpdated)
{
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"{Program.cfgjson.Emoji.Success} Successfully updated tracking for {discordUser.Mention}!"));
return;
}

await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"{Program.cfgjson.Emoji.Error} This user is already tracked!"));
return;
}
Expand Down Expand Up @@ -51,6 +124,7 @@ public async Task TrackingRemoveSlashCmd(InteractionContext ctx, [Option("member
}

await Program.db.SetRemoveAsync("trackedUsers", discordUser.Id);
await Program.db.HashDeleteAsync("trackingChannels", discordUser.Id);

var channelId = Program.db.HashGet("trackingThreads", discordUser.Id);
DiscordThreadChannel thread = (DiscordThreadChannel)await ctx.Client.GetChannelAsync((ulong)channelId);
Expand Down
45 changes: 36 additions & 9 deletions Events/MessageEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,23 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
{
if (Program.db.SetContains("trackedUsers", message.Author.Id))
{
DiscordThreadChannel relayThread;

if (trackingThreadCache.ContainsKey(message.Author.Id))
{
relayThread = trackingThreadCache[message.Author.Id];
// Check current channel against tracking channels
var trackingChannels = await Program.db.HashGetAsync("trackingChannels", message.Author.Id);
var trackingChannelsList = JsonConvert.DeserializeObject<List<ulong>>(trackingChannels);
if (trackingChannelsList.Count > 0)
{
// This user's tracking is filtered to channels; check the channel before relaying the msg to the tracking thread
var channels = JsonConvert.DeserializeObject<List<ulong>>(trackingChannels);
if (channels.Contains(channel.Id))
{
await RelayTrackedMessageAsync(client, message);
}
}
else
{
relayThread = (DiscordThreadChannel)await client.GetChannelAsync((ulong)await Program.db.HashGetAsync("trackingThreads", message.Author.Id));
trackingThreadCache.Add(message.Author.Id, relayThread);
// This user's tracking is not filtered to channels, so just relay the msg to the tracking thread
await RelayTrackedMessageAsync(client, message);
}
var _ = await relayThread.SendMessageAsync(await DiscordHelpers.GenerateMessageRelay(message.BaseMessage, true, true));

}

if (!isAnEdit && channel.IsPrivate && Program.cfgjson.LogChannels.ContainsKey("dms"))
Expand Down Expand Up @@ -935,6 +939,29 @@ public static async Task<bool> InviteCheck(DiscordInvite? invite, MockDiscordMes
return false;
}
}

private static async Task RelayTrackedMessageAsync(DiscordClient client, DiscordMessage message)
{
await RelayTrackedMessageAsync(client, new MockDiscordMessage(message));
}
private static async Task RelayTrackedMessageAsync(DiscordClient client, MockDiscordMessage message)
{
DiscordThreadChannel relayThread;

if (trackingThreadCache.ContainsKey(message.Author.Id))
{
relayThread = trackingThreadCache[message.Author.Id];
}
else
{
relayThread = (DiscordThreadChannel)await client.GetChannelAsync(
(ulong)await Program.db.HashGetAsync("trackingThreads", message.Author.Id));
trackingThreadCache.Add(message.Author.Id, relayThread);
}

var _ = await relayThread.SendMessageAsync(
await DiscordHelpers.GenerateMessageRelay(message.BaseMessage, true, true));
}

}
}

0 comments on commit 67a4a77

Please sign in to comment.