-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnipe.cs
More file actions
184 lines (155 loc) · 8.47 KB
/
snipe.cs
File metadata and controls
184 lines (155 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
namespace Moderation_Bot.commands
{
public class SnipeCommands : BaseCommandModule
{
// Store last 5 deleted messages per channel
private static readonly ConcurrentDictionary<ulong, LinkedList<(string Author, string Content, DateTimeOffset Timestamp)>> DeletedMessages
= new ConcurrentDictionary<ulong, LinkedList<(string, string, DateTimeOffset)>>();
// Store last 5 edited messages per channel
private static readonly ConcurrentDictionary<ulong, LinkedList<(string Author, string OldContent, string NewContent, DateTimeOffset Timestamp)>> EditedMessages
= new ConcurrentDictionary<ulong, LinkedList<(string, string, string, DateTimeOffset)>>();
// Call this once in your bot's setup (e.g., after client is ready)
public static void RegisterEventHandlers(DiscordClient client)
{
client.MessageDeleted += async (s, e) =>
{
if (e.Message == null || string.IsNullOrWhiteSpace(e.Message.Content))
return;
var list = DeletedMessages.GetOrAdd(e.Channel.Id, _ => new LinkedList<(string, string, DateTimeOffset)>());
list.AddFirst((e.Message.Author?.Username ?? "Unknown", e.Message.Content, e.Message.Timestamp));
while (list.Count > 5)
list.RemoveLast();
};
client.MessageUpdated += async (s, e) =>
{
if (e.Message == null || e.MessageBefore == null || string.IsNullOrWhiteSpace(e.MessageBefore.Content))
return;
// Only store if content actually changed
if (e.MessageBefore.Content == e.Message.Content)
return;
var list = EditedMessages.GetOrAdd(e.Channel.Id, _ => new LinkedList<(string, string, string, DateTimeOffset)>());
list.AddFirst((e.Message.Author?.Username ?? "Unknown", e.MessageBefore.Content, e.Message.Content, DateTimeOffset.UtcNow));
while (list.Count > 5)
list.RemoveLast();
};
}
[Command("snipe")]
[Description("Shows the last 5 deleted messages in this channel with navigation buttons.")]
public async Task Snipe(CommandContext ctx)
{
if (!DeletedMessages.TryGetValue(ctx.Channel.Id, out var list) || list.Count == 0)
{
await ctx.RespondAsync("There's nothing to snipe!");
return;
}
var messages = list.ToList();
int index = 0;
var embed = BuildSnipeEmbed(messages, index);
var left = new DiscordButtonComponent(ButtonStyle.Secondary, "snipe_left", "⬅️", disabled: true);
var right = new DiscordButtonComponent(ButtonStyle.Secondary, "snipe_right", "➡️", disabled: messages.Count <= 1);
var msg = await ctx.Channel.SendMessageAsync(
new DiscordMessageBuilder()
.WithEmbed(embed)
.AddComponents(left, right)
);
async Task Handler(DiscordClient client, ComponentInteractionCreateEventArgs e)
{
if (e.Message.Id != msg.Id || e.User.Id != ctx.User.Id)
return;
if (e.Id == "snipe_left" && index > 0)
index--;
else if (e.Id == "snipe_right" && index < messages.Count - 1)
index++;
var newEmbed = BuildSnipeEmbed(messages, index);
var newLeft = new DiscordButtonComponent(ButtonStyle.Secondary, "snipe_left", "⬅️", disabled: index == 0);
var newRight = new DiscordButtonComponent(ButtonStyle.Secondary, "snipe_right", "➡️", disabled: index == messages.Count - 1);
await e.Interaction.CreateResponseAsync(InteractionResponseType.UpdateMessage,
new DiscordInteractionResponseBuilder()
.AddEmbed(newEmbed)
.AddComponents(newLeft, newRight));
}
ctx.Client.ComponentInteractionCreated += Handler;
// Optionally, remove the handler after a timeout to avoid memory leaks
_ = Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromMinutes(2));
ctx.Client.ComponentInteractionCreated -= Handler;
});
}
[Command("editsnipe")]
[Description("Shows the last 5 edited messages in this channel with navigation buttons.")]
public async Task EditSnipe(CommandContext ctx)
{
if (!EditedMessages.TryGetValue(ctx.Channel.Id, out var list) || list.Count == 0)
{
await ctx.RespondAsync("There's nothing to editsnipe!");
return;
}
var messages = list.ToList();
int index = 0;
var embed = BuildEditSnipeEmbed(messages, index);
var left = new DiscordButtonComponent(ButtonStyle.Secondary, "editsnipe_left", "⬅️", disabled: true);
var right = new DiscordButtonComponent(ButtonStyle.Secondary, "editsnipe_right", "➡️", disabled: messages.Count <= 1);
var msg = await ctx.Channel.SendMessageAsync(
new DiscordMessageBuilder()
.WithEmbed(embed)
.AddComponents(left, right)
);
async Task Handler(DiscordClient client, ComponentInteractionCreateEventArgs e)
{
if (e.Message.Id != msg.Id || e.User.Id != ctx.User.Id)
return;
if (e.Id == "editsnipe_left" && index > 0)
index--;
else if (e.Id == "editsnipe_right" && index < messages.Count - 1)
index++;
var newEmbed = BuildEditSnipeEmbed(messages, index);
var newLeft = new DiscordButtonComponent(ButtonStyle.Secondary, "editsnipe_left", "⬅️", disabled: index == 0);
var newRight = new DiscordButtonComponent(ButtonStyle.Secondary, "editsnipe_right", "➡️", disabled: index == messages.Count - 1);
await e.Interaction.CreateResponseAsync(InteractionResponseType.UpdateMessage,
new DiscordInteractionResponseBuilder()
.AddEmbed(newEmbed)
.AddComponents(newLeft, newRight));
}
ctx.Client.ComponentInteractionCreated += Handler;
// Optionally, remove the handler after a timeout to avoid memory leaks
_ = Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromMinutes(2));
ctx.Client.ComponentInteractionCreated -= Handler;
});
}
private static DiscordEmbed BuildSnipeEmbed(List<(string Author, string Content, DateTimeOffset Timestamp)> messages, int index)
{
var (author, content, timestamp) = messages[index];
return new DiscordEmbedBuilder
{
Title = $"Deleted Message #{index + 1}",
Description = $"**Author:** {author}\n**Time:** {timestamp.LocalDateTime:t}\n\n{(string.IsNullOrWhiteSpace(content) ? "*[No content]*" : content)}",
Color = DiscordColor.Orange
};
}
private static DiscordEmbed BuildEditSnipeEmbed(List<(string Author, string OldContent, string NewContent, DateTimeOffset Timestamp)> messages, int index)
{
var (author, oldContent, newContent, timestamp) = messages[index];
return new DiscordEmbedBuilder
{
Title = $"Edited Message #{index + 1}",
Description = $"**Author:** {author}\n**Time:** {timestamp.LocalDateTime:t}\n\n" +
$"**Before:** {(string.IsNullOrWhiteSpace(oldContent) ? "*[No content]*" : oldContent)}\n" +
$"**After:** {(string.IsNullOrWhiteSpace(newContent) ? "*[No content]*" : newContent)}",
Color = DiscordColor.Blurple
};
}
}
}