-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLockModule.cs
More file actions
161 lines (139 loc) · 6.24 KB
/
LockModule.cs
File metadata and controls
161 lines (139 loc) · 6.24 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
using Discord;
using Discord.Interactions;
using Discord.WebSocket;
namespace Ivirius.Zippy
{
public class LockModule : InteractionModuleBase<SocketInteractionContext>
{
/// <summary>
/// Lock the current channel.
/// </summary>
[SlashCommand("lock", "Lock the current channel")]
[DefaultMemberPermissions(GuildPermission.ManageChannels)]
public async Task LockAsync(
[Summary("reason", "The reason for the lock.")] string reason = "No reason provided.")
{
await ExecuteLockAsync(reason, false);
}
/// <summary>
/// Unlock the current channel.
/// </summary>
[SlashCommand("unlock", "Unlock the current channel")]
[DefaultMemberPermissions(GuildPermission.ManageChannels)]
public async Task UnlockAsync()
{
await ExecuteUnlockAsync(false);
}
public async Task<string> ExecuteLockAsync(string reason, bool aiTriggered, DiscordSocketClient? client = null, IGuild? guild = null)
{
var resolvedGuild = aiTriggered ? guild! : Context.Guild;
var resolvedClient = aiTriggered ? client! : Context.Client;
if (!aiTriggered)
await DeferAsync(ephemeral: true);
try
{
var channel = Context.Channel as ITextChannel;
if (channel is null)
{
if (!aiTriggered)
{
await FollowupAsync(embed: new EmbedBuilder()
.WithTitle("❌ This command can only be used in a text channel.")
.WithColor(Color.Parse("#d1533d"))
.Build(), ephemeral: true);
return string.Empty;
}
return "Couldn't lock - not a text channel.";
}
// Post the lock message first, then lock
var lockEmbed = new EmbedBuilder()
.WithTitle("🔒 Channel Locked")
.WithDescription($"This channel has been locked.\n**Reason:** {reason}")
.WithColor(Color.Parse("#d1533d"))
.WithFooter($"Locked by {Context.User.Username}", Context.User.GetAvatarUrl())
.WithCurrentTimestamp()
.Build();
await channel.SendMessageAsync(embed: lockEmbed);
// Now lock
await channel.AddPermissionOverwriteAsync(
resolvedGuild.EveryoneRole,
new OverwritePermissions(sendMessages: PermValue.Deny));
if (aiTriggered)
return $"Channel #{channel.Name} locked.";
await FollowupAsync(embed: new EmbedBuilder()
.WithTitle("✅ Channel locked.")
.WithColor(Color.Parse("#66dea0"))
.Build(), ephemeral: true);
return $"Channel #{channel.Name} locked.";
}
catch (Exception ex)
{
var errorMsg = ex.Message.Contains("error 50013") ? "Missing permissions." : ex.Message;
if (!aiTriggered)
{
await FollowupAsync(embed: new EmbedBuilder()
.WithTitle($"❌ {errorMsg}")
.WithColor(Color.Parse("#d1533d"))
.Build(), ephemeral: true);
return string.Empty;
}
return $"Couldn't lock - {errorMsg}";
}
}
public async Task<string> ExecuteUnlockAsync(bool aiTriggered, DiscordSocketClient? client = null, IGuild? guild = null)
{
var resolvedGuild = aiTriggered ? guild! : Context.Guild;
var resolvedClient = aiTriggered ? client! : Context.Client;
if (!aiTriggered)
await DeferAsync(ephemeral: true);
try
{
var channel = Context.Channel as ITextChannel;
if (channel is null)
{
if (!aiTriggered)
{
await FollowupAsync(embed: new EmbedBuilder()
.WithTitle("❌ This command can only be used in a text channel.")
.WithColor(Color.Parse("#d1533d"))
.Build(), ephemeral: true);
return string.Empty;
}
return "Couldn't unlock - not a text channel.";
}
// Post the unlock message first, then unlock
await channel.AddPermissionOverwriteAsync(
resolvedGuild.EveryoneRole,
new OverwritePermissions(sendMessages: PermValue.Inherit));
var unlockEmbed = new EmbedBuilder()
.WithTitle("🔓 Channel Unlocked")
.WithDescription("This channel has been unlocked.")
.WithColor(Color.Parse("#66dea0"))
.WithFooter($"Unlocked by {Context.User.Username}", Context.User.GetAvatarUrl())
.WithCurrentTimestamp()
.Build();
await channel.SendMessageAsync(embed: unlockEmbed);
if (aiTriggered)
return $"Channel #{channel.Name} unlocked.";
await FollowupAsync(embed: new EmbedBuilder()
.WithTitle("✅ Channel unlocked.")
.WithColor(Color.Parse("#66dea0"))
.Build(), ephemeral: true);
return $"Channel #{channel.Name} unlocked.";
}
catch (Exception ex)
{
var errorMsg = ex.Message.Contains("error 50013") ? "Missing permissions." : ex.Message;
if (!aiTriggered)
{
await FollowupAsync(embed: new EmbedBuilder()
.WithTitle($"❌ {errorMsg}")
.WithColor(Color.Parse("#d1533d"))
.Build(), ephemeral: true);
return string.Empty;
}
return $"Couldn't unlock - {errorMsg}";
}
}
}
}