Skip to content

Commit 5c287e4

Browse files
Add CTS to /roles (#233)
1 parent e5f8069 commit 5c287e4

File tree

1 file changed

+60
-20
lines changed

1 file changed

+60
-20
lines changed

Commands/InteractionCommands/RoleInteractions.cs

+60-20
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,12 @@ internal class RoleSlashCommands
1616
[SlashCommand("grant", "Opt into a role.")]
1717
public async Task GrantRole(
1818
InteractionContext ctx,
19-
[Choice("Windows 11 Insiders (Canary)", "insiderCanary")]
20-
[Choice("Windows 11 Insiders (Dev)", "insiderDev")]
21-
[Choice("Windows 11 Insiders (Beta)", "insiderBeta")]
22-
[Choice("Windows 11 Insiders (Release Preview)", "insiderRP")]
23-
[Choice("Windows 10 Insiders (Release Preview)", "insider10RP")]
24-
[Choice("Windows 10 Insiders (Beta)", "insider10Beta")]
25-
[Choice("Patch Tuesday", "patchTuesday")]
26-
[Choice("Giveaways", "giveaways")]
19+
[Autocomplete(typeof(RolesAutocompleteProvider))]
2720
[Option("role", "The role to opt into.")] string role)
2821
{
2922
DiscordMember member = ctx.Member;
3023

31-
var roleId = role switch
24+
ulong roleId = role switch
3225
{
3326
"insiderCanary" => Program.cfgjson.UserRoles.InsiderCanary,
3427
"insiderDev" => Program.cfgjson.UserRoles.InsiderDev,
@@ -38,8 +31,21 @@ public async Task GrantRole(
3831
"insider10Beta" => Program.cfgjson.UserRoles.Insider10Beta,
3932
"patchTuesday" => Program.cfgjson.UserRoles.PatchTuesday,
4033
"giveaways" => Program.cfgjson.UserRoles.Giveaways,
41-
_ => throw new NotSupportedException()
34+
"cts" => Program.cfgjson.CommunityTechSupportRoleID,
35+
_ => 0
4236
};
37+
38+
if (roleId == 0)
39+
{
40+
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} Invalid role! Please choose from the list.", ephemeral: true);
41+
return;
42+
}
43+
44+
if (roleId == Program.cfgjson.CommunityTechSupportRoleID && await GetPermLevelAsync(ctx.Member) < ServerPermLevel.TechnicalQueriesSlayer)
45+
{
46+
await ctx.RespondAsync($"{Program.cfgjson.Emoji.NoPermissions} You must be a TQS member to get the CTS role!", ephemeral: true);
47+
return;
48+
}
4349

4450
var roleData = await ctx.Guild.GetRoleAsync(roleId);
4551

@@ -50,19 +56,12 @@ public async Task GrantRole(
5056
[SlashCommand("remove", "Opt out of a role.")]
5157
public async Task RemoveRole(
5258
InteractionContext ctx,
53-
[Choice("Windows 11 Insiders (Canary)", "insiderCanary")]
54-
[Choice("Windows 11 Insiders (Dev)", "insiderDev")]
55-
[Choice("Windows 11 Insiders (Beta)", "insiderBeta")]
56-
[Choice("Windows 11 Insiders (Release Preview)", "insiderRP")]
57-
[Choice("Windows 10 Insiders (Release Preview)", "insider10RP")]
58-
[Choice("Windows 10 Insiders (Beta)", "insider10Beta")]
59-
[Choice("Patch Tuesday", "patchTuesday")]
60-
[Choice("Giveaways", "giveaways")]
59+
[Autocomplete(typeof(RolesAutocompleteProvider))]
6160
[Option("role", "The role to opt out of.")] string role)
6261
{
6362
DiscordMember member = ctx.Member;
6463

65-
var roleId = role switch
64+
ulong roleId = role switch
6665
{
6766
"insiderCanary" => Program.cfgjson.UserRoles.InsiderCanary,
6867
"insiderDev" => Program.cfgjson.UserRoles.InsiderDev,
@@ -72,14 +71,55 @@ public async Task RemoveRole(
7271
"insider10Beta" => Program.cfgjson.UserRoles.Insider10Beta,
7372
"patchTuesday" => Program.cfgjson.UserRoles.PatchTuesday,
7473
"giveaways" => Program.cfgjson.UserRoles.Giveaways,
75-
_ => throw new NotSupportedException()
74+
"cts" => Program.cfgjson.CommunityTechSupportRoleID,
75+
_ => 0
7676
};
77+
78+
if (roleId == 0)
79+
{
80+
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} Invalid role! Please choose from the list.", ephemeral: true);
81+
return;
82+
}
7783

7884
var roleData = await ctx.Guild.GetRoleAsync(roleId);
7985

8086
await member.RevokeRoleAsync(roleData, $"/roles remove used by {DiscordHelpers.UniqueUsername(ctx.User)}");
8187
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Success} The role {roleData.Mention} has been successfully removed!", ephemeral: true, mentions: false);
8288
}
8389
}
90+
91+
internal class RolesAutocompleteProvider : IAutocompleteProvider
92+
{
93+
public async Task<IEnumerable<DiscordAutoCompleteChoice>> Provider(AutocompleteContext ctx)
94+
{
95+
Dictionary<string, string> options = new()
96+
{
97+
{ "Windows 11 Insiders (Canary)", "insiderCanary" },
98+
{ "Windows 11 Insiders (Dev)", "insiderDev" },
99+
{ "Windows 11 Insiders (Beta)", "insiderBeta" },
100+
{ "Windows 11 Insiders (Release Preview)", "insiderRP" },
101+
{ "Windows 10 Insiders (Release Preview)", "insider10RP" },
102+
{ "Windows 10 Insiders (Beta)", "insider10Beta" },
103+
{ "Patch Tuesday", "patchTuesday" },
104+
{ "Giveaways", "giveaways" },
105+
{ "Community Tech Support (CTS)", "cts" }
106+
};
107+
108+
var memberHasTqs = await GetPermLevelAsync(ctx.Member) >= ServerPermLevel.TechnicalQueriesSlayer;
109+
110+
List<DiscordAutoCompleteChoice> list = new();
111+
112+
foreach (var option in options)
113+
{
114+
if (ctx.FocusedOption.Value.ToString() == "" || option.Key.Contains(ctx.FocusedOption.Value.ToString(), StringComparison.OrdinalIgnoreCase))
115+
{
116+
if (option.Value == "cts" && !memberHasTqs) continue;
117+
list.Add(new DiscordAutoCompleteChoice(option.Key, option.Value));
118+
}
119+
}
120+
121+
return list;
122+
}
123+
}
84124
}
85125
}

0 commit comments

Comments
 (0)