Skip to content

Commit 449bf90

Browse files
Keep track of last command usage time per guild
1 parent 8c8e129 commit 449bf90

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

Commands/DebugCommands.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,28 @@ await ctx.FollowupAsync(new DiscordFollowupMessageBuilder()
251251
[Description("Commands for managing which guilds the bot is in.")]
252252
public static class DebugGuildsCommands
253253
{
254+
[Command("lastuse")]
255+
[Description("Show the last time one of the bot's commands was used in a guild.")]
256+
public static async Task DebugGuildsLastUseCommandAsync(SlashCommandContext ctx,
257+
[SlashAutoCompleteProvider(typeof(DebugGuildsGuildSearchAutoCompleteProvider))]
258+
[Parameter("guild"), Description("The guild to show the last use time for. Accepts names or IDs.")] string guildId)
259+
{
260+
await ctx.DeferResponseAsync(ephemeral: ctx.Interaction.ShouldUseEphemeralResponse(false));
261+
262+
var lastUse = await Setup.Storage.Redis.HashGetAsync("lastCommandUse", guildId);
263+
if (!lastUse.HasValue)
264+
{
265+
await ctx.FollowupAsync(new DiscordFollowupMessageBuilder()
266+
.WithContent("Nothing stored for that guild.")
267+
.AsEphemeral(ephemeral: ctx.Interaction.ShouldUseEphemeralResponse(false)));
268+
return;
269+
}
270+
271+
await ctx.FollowupAsync(new DiscordFollowupMessageBuilder()
272+
.WithContent($"<t:{JsonConvert.DeserializeObject<DateTime>(lastUse).ToUnixTimeSeconds()}:F>")
273+
.AsEphemeral(ephemeral: ctx.Interaction.ShouldUseEphemeralResponse(false)));
274+
}
275+
254276
[Command("info")]
255277
[Description("Get information about a guild.")]
256278
public static async Task DebugGuildsInfoCommandAsync(SlashCommandContext ctx,

Events/InteractionEvents.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,13 @@ await Setup.State.Discord.Channels.Home.SendMessageAsync(new DiscordEmbedBuilder
602602
internal static async Task HandleCommandExecutedEventAsync(CommandsExtension _, CommandExecutedEventArgs e)
603603
{
604604
await LogInteractionCommandUsageAsync(e.Context);
605+
if (e.Context.Guild is not null)
606+
await SaveGuildLastCommandUseAsync(e.Context.Guild.Id);
607+
}
608+
609+
private static async Task SaveGuildLastCommandUseAsync(ulong guildId)
610+
{
611+
await Setup.Storage.Redis.HashSetAsync("lastCommandUse", guildId.ToString(), JsonConvert.SerializeObject(DateTime.UtcNow));
605612
}
606613

607614
private static async Task LogInteractionCommandUsageAsync(CommandContext context)

0 commit comments

Comments
 (0)