Skip to content

Commit f3171a8

Browse files
Tweak /debug messagecache
1 parent 2d8e442 commit f3171a8

2 files changed

Lines changed: 59 additions & 19 deletions

File tree

Commands/DebugCommands.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static async Task DebugCachedMessageChannelCommandAsync(SlashCommandConte
1919

2020
var success = Setup.State.Caches.MessageCache.TryGetMessageByChannel(channelId, out var message);
2121
if (success)
22-
await ctx.RespondAsync(await message.ToFancyStringAsync());
22+
await ctx.RespondAsync(await message.GetInformationAsync());
2323
else
2424
await ctx.FollowupAsync(new DiscordFollowupMessageBuilder()
2525
.WithContent("No message cached for that channel")
@@ -35,7 +35,7 @@ public static async Task DebugCachedMessageMessageCommandAsync(SlashCommandConte
3535

3636
var success = Setup.State.Caches.MessageCache.TryGetMessage(messageId, out var message);
3737
if (success)
38-
await ctx.RespondAsync(await message.ToFancyStringAsync());
38+
await ctx.RespondAsync(await message.GetInformationAsync());
3939
else
4040
await ctx.FollowupAsync(new DiscordFollowupMessageBuilder()
4141
.WithContent("No message cached with that ID")
@@ -51,7 +51,7 @@ public static async Task DebugCachedMessageAuthorCommandAsync(SlashCommandContex
5151

5252
var success = Setup.State.Caches.MessageCache.TryGetMessageByAuthor(authorId, out var message);
5353
if (success)
54-
await ctx.RespondAsync(await message.ToFancyStringAsync());
54+
await ctx.RespondAsync(await message.GetInformationAsync());
5555
else
5656
await ctx.FollowupAsync(new DiscordFollowupMessageBuilder()
5757
.WithContent("No message cached by that author")
@@ -67,15 +67,33 @@ public static async Task DebugCachedMessageStatsCommandAsync(SlashCommandContext
6767
var cachedMessagesCount = Setup.State.Caches.MessageCache.Count();
6868
var uniqueChannelsCount = Setup.State.Caches.MessageCache.GetUniqueChannelCount();
6969
var uniqueAuthorsCount = Setup.State.Caches.MessageCache.GetUniqueAuthorCount();
70-
var newestCachedMessage = Setup.State.Caches.MessageCache.GetNewestMessage();
71-
var oldestCachedMessage = Setup.State.Caches.MessageCache.GetOldestMessage();
7270

7371
await ctx.FollowupAsync(new DiscordFollowupMessageBuilder()
74-
.WithContent($"{cachedMessagesCount} messages in cache, from {uniqueAuthorsCount} author{(uniqueAuthorsCount == 1 ? "" : "s")} across {uniqueChannelsCount} channels."
75-
+ $"\nNewest message: {await newestCachedMessage.ToFancyStringAsync()}"
76-
+ $"\nOldest message: {await oldestCachedMessage.ToFancyStringAsync()}")
72+
.WithContent($"{cachedMessagesCount} messages in cache, from {uniqueAuthorsCount} author{(uniqueAuthorsCount == 1 ? "" : "s")} across {uniqueChannelsCount} channels.")
7773
.AsEphemeral(ephemeral: ctx.Interaction.ShouldUseEphemeralResponse(false)));
7874
}
75+
76+
[Command("newest")]
77+
[Description("Get the newest message from the message cache.")]
78+
public static async Task DebugCachedMessageNewestCommandAsync(SlashCommandContext ctx,
79+
[Parameter("skip"), Description("The number of messages to skip over.")] int skip = 0)
80+
{
81+
await ctx.DeferResponseAsync(ephemeral: ctx.Interaction.ShouldUseEphemeralResponse(false));
82+
83+
await ctx.FollowupAsync(new DiscordFollowupMessageBuilder()
84+
.WithContent(await (Setup.State.Caches.MessageCache.GetNewestMessage(skip)).GetInformationAsync()));
85+
}
86+
87+
[Command("oldest")]
88+
[Description("Get the oldest message from the message cache.")]
89+
public static async Task DebugCachedMessageOldestCommandAsync(SlashCommandContext ctx,
90+
[Parameter("skip"), Description("The number of messages to skip over.")] int skip = 0)
91+
{
92+
await ctx.DeferResponseAsync(ephemeral: ctx.Interaction.ShouldUseEphemeralResponse(false));
93+
94+
await ctx.FollowupAsync(new DiscordFollowupMessageBuilder()
95+
.WithContent(await (Setup.State.Caches.MessageCache.GetOldestMessage(skip)).GetInformationAsync()));
96+
}
7997
}
8098

8199
[Command("uptime")]

Setup/Types/MessageCache.cs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ internal CachedMessage GetMessageByAuthor(ulong authorId)
4040
return Messages.Find(x => x.AuthorId == authorId);
4141
}
4242

43-
internal CachedMessage GetNewestMessage()
43+
internal CachedMessage GetNewestMessage(int skip = 0)
4444
{
45-
return GetAllMessages().OrderByDescending(m => m.MessageId).First();
45+
return GetAllMessages().OrderByDescending(m => m.MessageId).Skip(skip).First();
4646
}
4747

48-
internal CachedMessage GetOldestMessage()
48+
internal CachedMessage GetOldestMessage(int skip = 0)
4949
{
50-
return GetAllMessages().OrderBy(m => m.MessageId).First();
50+
return GetAllMessages().OrderBy(m => m.MessageId).Skip(skip).First();
5151
}
5252

5353
internal List<CachedMessage> GetAllMessages()
@@ -140,14 +140,36 @@ internal async Task<string> GetMessageLinkAsync()
140140
return $"https://discord.com/channels/{guildId}/{ChannelId}/{MessageId}";
141141
}
142142

143-
internal async Task<string> ToFancyStringAsync()
143+
internal async Task<string> GetInformationAsync()
144144
{
145-
return $"({GetTimestamp()}) {ToString()} {await GetMessageLinkAsync()}";
146-
}
147-
148-
public override string ToString()
149-
{
150-
return $"Message {MessageId} by user {AuthorId} in channel {ChannelId}";
145+
DiscordChannel channel = default;
146+
DiscordUser author = default;
147+
try
148+
{
149+
channel = await Setup.State.Discord.Client.GetChannelAsync(ChannelId);
150+
author = await Setup.State.Discord.Client.GetUserAsync(AuthorId);
151+
}
152+
catch (Exception ex) when (ex is NotFoundException or UnauthorizedException)
153+
{
154+
// Don't care
155+
}
156+
string channelInformation = "**Channel:** ";
157+
if (channel == default)
158+
channelInformation += ChannelId;
159+
else
160+
channelInformation += $"{channel.Name} {channel.Id}";
161+
string authorInformation = "**Author:** ";
162+
if (author == default)
163+
authorInformation += AuthorId;
164+
else
165+
authorInformation += $"{author.Username} {author.Id}";
166+
167+
168+
return $"**Timestamp:** {GetTimestamp()}"
169+
+ $"\n**Message ID:** {MessageId}"
170+
+ $"\n{channelInformation}"
171+
+ $"\n{authorInformation}"
172+
+ $"\n{await GetMessageLinkAsync()}";
151173
}
152174
}
153175
}

0 commit comments

Comments
 (0)