Skip to content

Commit bfda65a

Browse files
committed
Resolves #24: clear command
1 parent 13b1807 commit bfda65a

File tree

2 files changed

+58
-23
lines changed

2 files changed

+58
-23
lines changed

Boolean/Modules/AdminUtils.cs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.Runtime.InteropServices.ObjectiveC;
2+
using Boolean.Util;
3+
using Discord;
4+
using Discord.Interactions;
5+
using Discord.WebSocket;
6+
7+
namespace Boolean;
8+
9+
[DefaultMemberPermissions(GuildPermission.Administrator)]
10+
public class AdminUtils(DiscordSocketClient client) : InteractionModuleBase<SocketInteractionContext>
11+
{
12+
[SlashCommand("repeat", "Repeats the message you send into the channel")]
13+
public async Task Repeat(string msg)
14+
{
15+
await ReplyAsync(msg, allowedMentions: AllowedMentions.None);
16+
17+
// Annoying we have to do this to end the interaction.
18+
await RespondAsync(embed: new EmbedBuilder
19+
{
20+
Description = "Repeated your message",
21+
Color = EmbedColors.Normal
22+
}.Build(), ephemeral: true);
23+
}
24+
25+
[SlashCommand("clear", "Clears a specified number of messages in a channel")]
26+
public async Task Clear(int messageCount)
27+
{
28+
if (messageCount is < 1 or > 500) {
29+
await RespondAsync(embed: new EmbedBuilder
30+
{
31+
Description = "Number of messages to delete must be between 1-500.",
32+
Color = EmbedColors.Fail,
33+
}.Build(), ephemeral: true);
34+
35+
return;
36+
}
37+
38+
if (Context.Channel is not SocketTextChannel textChannel) {
39+
await RespondAsync(embed: new EmbedBuilder
40+
{
41+
Description = "Can only delete messages in a text channel.",
42+
Color = EmbedColors.Fail,
43+
}.Build(), ephemeral: true);
44+
45+
return;
46+
}
47+
48+
var messages = await Context.Channel.GetMessagesAsync(messageCount).FlattenAsync();
49+
await textChannel.DeleteMessagesAsync(messages);
50+
51+
await RespondAsync(embed: new EmbedBuilder
52+
{
53+
Description = $"Deleted `{messages.Count()}` messages.",
54+
Color = EmbedColors.Success,
55+
}.Build());
56+
}
57+
58+
}

Boolean/Modules/Repeat.cs

-23
This file was deleted.

0 commit comments

Comments
 (0)