Skip to content

Commit 943e09c

Browse files
/rules: Add 'public' option (#235)
* /rules: Add 'public' option * Also check parent channel ID
1 parent 39ed062 commit 943e09c

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

Commands/InteractionCommands/RulesInteractions.cs

+38-10
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ public class RulesInteractions : ApplicationCommandModule
77
internal class RulesSlashCommands
88
{
99
[SlashCommand("all", "Shows all of the community rules.", defaultPermission: true)]
10-
public async Task RulesAllCommand(InteractionContext ctx)
10+
public async Task RulesAllCommand(InteractionContext ctx, [Option("public", "Whether to show the response publicly.")] bool? isPublic = null)
1111
{
12+
var publicResponse = await DeterminePublicResponse(ctx.Member, ctx.Channel, isPublic);
13+
1214
List<string> rules = default;
1315

1416
try
@@ -19,7 +21,7 @@ public async Task RulesAllCommand(InteractionContext ctx)
1921
catch
2022
{
2123
// community must be disabled
22-
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} I don't see any rules set in Discord for this server!");
24+
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} I don't see any rules set in Discord for this server!", ephemeral: !publicResponse);
2325
return;
2426
}
2527

@@ -30,13 +32,15 @@ public async Task RulesAllCommand(InteractionContext ctx)
3032
embed.AddField($"Rule {rules.IndexOf(rule) + 1}", rule);
3133
}
3234

33-
await ctx.RespondAsync(embed: embed);
35+
await ctx.RespondAsync(embed: embed, ephemeral: !publicResponse);
3436

3537
}
3638

3739
[SlashCommand("rule", "Shows a specific rule.", defaultPermission: true)]
38-
public async Task RuleCommand(InteractionContext ctx, [Option("rule_number", "The rule number to show.")] long ruleNumber)
40+
public async Task RuleCommand(InteractionContext ctx, [Option("rule_number", "The rule number to show.")] long ruleNumber, [Option("public", "Whether to show the response publicly.")] bool? isPublic = null)
3941
{
42+
var publicResponse = await DeterminePublicResponse(ctx.Member, ctx.Channel, isPublic);
43+
4044
IReadOnlyList<string> rules = default;
4145

4246
try
@@ -47,24 +51,26 @@ public async Task RuleCommand(InteractionContext ctx, [Option("rule_number", "Th
4751
catch
4852
{
4953
// community must be disabled
50-
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} I don't see any rules set in Discord for this server!");
54+
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} I don't see any rules set in Discord for this server!", ephemeral: !publicResponse);
5155
return;
5256
}
5357

5458
if (ruleNumber < 1 || ruleNumber > rules.Count)
5559
{
56-
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} Rule number must be between 1 and {rules.Count}.");
60+
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} Rule number must be between 1 and {rules.Count}.", ephemeral: !publicResponse);
5761
return;
5862
}
5963

6064
var embed = new DiscordEmbedBuilder().WithTitle($"Rule {ruleNumber}").WithDescription(rules[(int)ruleNumber - 1]).WithColor(new DiscordColor(0xe4717b));
6165

62-
await ctx.RespondAsync(embed: embed);
66+
await ctx.RespondAsync(embed: embed, ephemeral: !publicResponse);
6367
}
6468

6569
[SlashCommand("search", "Search for a rule by keyword.", defaultPermission: true)]
66-
public async Task RuleSearchCommand(InteractionContext ctx, [Option("keyword", "The keyword to search for.")] string keyword)
70+
public async Task RuleSearchCommand(InteractionContext ctx, [Option("keyword", "The keyword to search for.")] string keyword, [Option("public", "Whether to show the response publicly.")] bool? isPublic = null)
6771
{
72+
var publicResponse = await DeterminePublicResponse(ctx.Member, ctx.Channel, isPublic);
73+
6874
List<string> rules = default;
6975

7076
try
@@ -75,7 +81,7 @@ public async Task RuleSearchCommand(InteractionContext ctx, [Option("keyword", "
7581
catch
7682
{
7783
// community must be disabled
78-
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} I don't see any rules set in Discord for this server!");
84+
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} I don't see any rules set in Discord for this server!", ephemeral: !publicResponse);
7985
return;
8086
}
8187

@@ -94,7 +100,29 @@ public async Task RuleSearchCommand(InteractionContext ctx, [Option("keyword", "
94100
embed.AddField($"Rule {rules.IndexOf(rule) + 1}", rule);
95101
}
96102

97-
await ctx.RespondAsync(embed: embed);
103+
await ctx.RespondAsync(embed: embed, ephemeral: !publicResponse);
104+
}
105+
106+
// Returns: true for public response, false for private
107+
private async Task<bool> DeterminePublicResponse(DiscordMember member, DiscordChannel channel, bool? isPublic)
108+
{
109+
if (Program.cfgjson.RulesAllowedPublicChannels.Contains(channel.Id) || Program.cfgjson.RulesAllowedPublicChannels.Contains(channel.Parent.Id))
110+
{
111+
if (isPublic is null)
112+
return true;
113+
114+
return isPublic.Value;
115+
}
116+
117+
if (await GetPermLevelAsync(member) >= ServerPermLevel.TrialModerator)
118+
{
119+
if (isPublic is null)
120+
return false;
121+
122+
return isPublic.Value;
123+
}
124+
125+
return false;
98126
}
99127
}
100128
}

Structs.cs

+3
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ public class ConfigJson
301301

302302
[JsonProperty("forumChannelAutoWarnFallbackChannel")]
303303
public ulong ForumChannelAutoWarnFallbackChannel { get; private set; } = 0;
304+
305+
[JsonProperty("rulesAllowedPublicChannels")]
306+
public List<ulong> RulesAllowedPublicChannels { get; private set; } = new();
304307
}
305308

306309
public enum Level { Information, Warning, Error, Debug, Verbose }

config.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -324,5 +324,13 @@
324324
"lokiURL": "http://100.79.19.82:3100",
325325
"lokiServiceName": "cliptok",
326326
"voiceChannelPurge": true,
327-
"forumChannelAutoWarnFallbackChannel": 150662382874525696
327+
"forumChannelAutoWarnFallbackChannel": 150662382874525696,
328+
"rulesAllowedPublicChannels": [
329+
150909451270881280,
330+
1006577277313744996,
331+
153165424358457345,
332+
740272437719072808,
333+
536572162450915340,
334+
355973419961155584
335+
]
328336
}

0 commit comments

Comments
 (0)