@@ -16,19 +16,12 @@ internal class RoleSlashCommands
16
16
[ SlashCommand ( "grant" , "Opt into a role." ) ]
17
17
public async Task GrantRole (
18
18
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 ) ) ]
27
20
[ Option ( "role" , "The role to opt into." ) ] string role )
28
21
{
29
22
DiscordMember member = ctx . Member ;
30
23
31
- var roleId = role switch
24
+ ulong roleId = role switch
32
25
{
33
26
"insiderCanary" => Program . cfgjson . UserRoles . InsiderCanary ,
34
27
"insiderDev" => Program . cfgjson . UserRoles . InsiderDev ,
@@ -38,8 +31,21 @@ public async Task GrantRole(
38
31
"insider10Beta" => Program . cfgjson . UserRoles . Insider10Beta ,
39
32
"patchTuesday" => Program . cfgjson . UserRoles . PatchTuesday ,
40
33
"giveaways" => Program . cfgjson . UserRoles . Giveaways ,
41
- _ => throw new NotSupportedException ( )
34
+ "cts" => Program . cfgjson . CommunityTechSupportRoleID ,
35
+ _ => 0
42
36
} ;
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
+ }
43
49
44
50
var roleData = await ctx . Guild . GetRoleAsync ( roleId ) ;
45
51
@@ -50,19 +56,12 @@ public async Task GrantRole(
50
56
[ SlashCommand ( "remove" , "Opt out of a role." ) ]
51
57
public async Task RemoveRole (
52
58
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 ) ) ]
61
60
[ Option ( "role" , "The role to opt out of." ) ] string role )
62
61
{
63
62
DiscordMember member = ctx . Member ;
64
63
65
- var roleId = role switch
64
+ ulong roleId = role switch
66
65
{
67
66
"insiderCanary" => Program . cfgjson . UserRoles . InsiderCanary ,
68
67
"insiderDev" => Program . cfgjson . UserRoles . InsiderDev ,
@@ -72,14 +71,55 @@ public async Task RemoveRole(
72
71
"insider10Beta" => Program . cfgjson . UserRoles . Insider10Beta ,
73
72
"patchTuesday" => Program . cfgjson . UserRoles . PatchTuesday ,
74
73
"giveaways" => Program . cfgjson . UserRoles . Giveaways ,
75
- _ => throw new NotSupportedException ( )
74
+ "cts" => Program . cfgjson . CommunityTechSupportRoleID ,
75
+ _ => 0
76
76
} ;
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
+ }
77
83
78
84
var roleData = await ctx . Guild . GetRoleAsync ( roleId ) ;
79
85
80
86
await member . RevokeRoleAsync ( roleData , $ "/roles remove used by { DiscordHelpers . UniqueUsername ( ctx . User ) } ") ;
81
87
await ctx . RespondAsync ( $ "{ Program . cfgjson . Emoji . Success } The role { roleData . Mention } has been successfully removed!", ephemeral : true , mentions : false ) ;
82
88
}
83
89
}
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
+ }
84
124
}
85
125
}
0 commit comments