-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClearCommand.cs
33 lines (28 loc) · 1.32 KB
/
ClearCommand.cs
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
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
namespace HyBot.Commands {
public class ClearCommand : ApplicationCommandModule {
[SlashCommand("clear", "Deletes a specified number of messages in a given channel.")]
public async Task ClearCommandAsync(InteractionContext ctx,
[Option("number", "The number of messages to delete.")]
long number) {
if (number < 1 || number > 100)
{
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder()
.WithContent("The number of messages must be between 1 and 100."));
return;
}
var channel = ctx.Channel;
var messages = await ctx.Channel.GetMessagesAsync((int) number);
await channel.DeleteMessagesAsync(messages);
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder()
.WithContent($"Successfully deleted {number} messages in {channel.Mention}."));
// Auto-delete the response message after 3 seconds
await Task.Delay(3000);
await ctx.DeleteResponseAsync();
}
}
}