@@ -7,41 +7,82 @@ import { getUserRoles } from './guild.service';
77import { log } from '../logger' ;
88
99/**
10- * This command assigns the role given in the argument to the user that executed the command, if that role can be
11- * self-assigned.
10+ * This command assigns or removes the role given in the argumens
11+ * to the user that executed the command,
12+ * if that role is self-assignable
1213 */
1314export const command = async (
1415 arg : [ string , string ] ,
1516 embed : MessageEmbed ,
1617 message : Message
1718) => {
18- const args = arg [ 1 ] . toLowerCase ( ) . split ( ',' ) ;
19+ // Check for add or remove keywords
20+ const argCheck = arg [ 1 ] . toLowerCase ( ) . split ( ' ' ) ;
21+ let toAdd ;
22+
23+ if ( argCheck [ 0 ] === 'add' ) {
24+ toAdd = true ;
25+ } else if ( argCheck [ 0 ] === 'remove' ) {
26+ toAdd = false ;
27+ } else {
28+ return buildErrorEmbed ( 'Missing arguments' ) ;
29+ }
30+
31+ // get list of roles to modify
32+ argCheck . shift ( ) ;
33+ const args = argCheck . join ( ' ' ) . split ( ',' ) ;
34+
1935 // Check if the user provided the role argument
2036 if ( ! args ) {
2137 return buildErrorEmbed ( 'Missing arguments' ) ;
2238 }
2339
24- const rolesToAssign = args . map ( ( x ) => x . trim ( ) ) ;
40+ const rolesToModify = args . map ( ( x ) => x . trim ( ) ) ;
2541
26- for ( const roleToAssign of rolesToAssign ) {
42+ for ( const roleToModify of rolesToModify ) {
2743 // Check if the provided role is self-assignable
28- if ( selfAssignableRoles . find ( ( role ) => role === roleToAssign ) ) {
44+ if ( selfAssignableRoles . find ( ( role ) => role === roleToModify ) ) {
2945 const role = message . guild ! . roles . cache . find (
30- ( r ) => r . name === roleToAssign
46+ ( r ) => r . name === roleToModify
3147 ) ;
3248 if ( ! role ) {
33- log . error ( `ERROR: Couldn't get the role: ${ roleToAssign } ` ) ;
49+ log . error ( `ERROR: Couldn't get the role: ${ roleToModify } ` ) ;
3450 return buildErrorEmbed ( ) ;
3551 }
36- // Assign the role to the user of the message
52+
3753 const member = message . member ;
38- await member ! . roles . add ( role ) ;
39- await tryAddOpenSourceUserSubscription ( roleToAssign , message ) ;
54+ if ( toAdd ) {
55+ // Checks if the user already has the role
56+ if ( member ! . roles . cache . find ( ( r ) => r . name === roleToModify ) ) {
57+ return buildErrorEmbed ( `You already have the ${ roleToModify } role` ) ;
58+ } else {
59+ // Adds role to author of the message
60+ await member ! . roles . add ( role ) ;
61+ }
62+ } else {
63+ // Unassign role from author of the message
64+ if ( member ! . roles . cache . find ( ( r ) => r . name === roleToModify ) ) {
65+ await member ! . roles . remove ( role ) ;
66+ } else {
67+ // Checks if the user does not have the role
68+ return buildErrorEmbed ( `You do not have the role: ${ roleToModify } ` ) ;
69+ }
70+ }
4071 } else {
4172 return buildErrorEmbed ( `The role you specified is not self-assignable, try one of these roles:
4273 ${ selfAssignableRoles . join ( ', ' ) }
4374 ` ) ;
4475 }
76+
77+ // Checks if role being assigned is Opensource role
78+ if ( roleToModify === config . ROLE . OPEN_SOURCE . name ) {
79+ // Check if subscription is to be added or removed and calls respective function
80+ if ( toAdd ) {
81+ await addOpenSourceUserSubscription ( roleToModify , message ) ;
82+ } else {
83+ await removeOpenSourceUserSubscription ( roleToModify , message ) ;
84+ }
85+ }
4586 }
4687
4788 // Save the user's role to the DB
@@ -57,14 +98,28 @@ export const command = async (
5798 ) ;
5899
59100 const userName = message . member ! . displayName || '' ;
60- if ( rolesToAssign . length > 1 ) {
61- return embed . setDescription (
62- `**${ userName } ** You now have the roles **${ rolesToAssign . join ( ', ' ) } **`
63- ) ;
101+ if ( rolesToModify . length > 1 ) {
102+ if ( toAdd ) {
103+ return embed . setDescription (
104+ `**${ userName } ** You now have the roles **${ rolesToModify . join ( ', ' ) } **`
105+ ) ;
106+ } else {
107+ return embed . setDescription (
108+ `**${ userName } ** You now don't have the roles **${ rolesToModify . join (
109+ ', '
110+ ) } **`
111+ ) ;
112+ }
64113 } else {
65- return embed . setDescription (
66- `**${ userName } ** You now have the **${ rolesToAssign [ 0 ] } ** role`
67- ) ;
114+ if ( toAdd ) {
115+ return embed . setDescription (
116+ `**${ userName } ** You now have the **${ rolesToModify [ 0 ] } ** role`
117+ ) ;
118+ } else {
119+ return embed . setDescription (
120+ `**${ userName } ** You now don't have the **${ rolesToModify [ 0 ] } ** role`
121+ ) ;
122+ }
68123 }
69124
70125 // Auxiliar function
@@ -76,41 +131,57 @@ export const command = async (
76131 }
77132} ;
78133
79- export const description = 'Assign yourself a server role' ;
134+ export const description =
135+ 'Assign yourself a server role or unassign your server role' ;
80136
81137export const triggers = [ 'iam' ] ;
82138
83- export const usage = `${ triggers [ 0 ] } <role name> || ${ triggers [ 0 ] } <role name>, <role name>, ...` ;
139+ export const usage = `${ triggers [ 0 ] } <add/remove> < role name> or ${ triggers [ 0 ] } <add/remove> <role name>, <role name>, ...` ;
84140
85141/**
86- * Check if the role to assign is the open source role. If so, then add the open source subscription for the user to
87- * receive recurrent messages to remind them to contribute to open source. If not then do nothing.
142+ * Adds the open source subscription for the user to
143+ * receive recurrent messages to remind them to contribute to open source.
88144 * @param roleToAssign
89145 * @param message
90146 */
91- async function tryAddOpenSourceUserSubscription (
92- roleToAssign : string ,
147+ async function addOpenSourceUserSubscription (
148+ roleToModify : string ,
93149 message : Message
94150) {
95- if ( roleToAssign === config . ROLE . OPEN_SOURCE . name ) {
96- // Get the subscriptions of the user that sent the given message
97- const doc = await db
98- . collection ( 'usersSubscriptions' )
99- . doc ( message . author . id )
100- . get ( ) ;
101- const data = doc . data ( ) ;
102- const subscriptions : string [ ] = data != null ? data . subscriptions : [ ] ;
103-
104- // Add the open source subscription to the list of existing subscriptions
105- await db
106- . collection ( 'usersSubscriptions' ) // TODO: create a constant variable with all these collection names (e.g. exported in firebase.ts)
107- . doc ( message . author . id )
108- . set (
109- {
110- subscriptions : [ ...subscriptions , UserSubscriptions . OPEN_SOURCE ] ,
111- username : message . author . username ,
112- } ,
113- { merge : true }
114- ) ;
115- }
151+ // Get the subscriptions of the user that sent the given message
152+ const doc = await db
153+ . collection ( 'usersSubscriptions' )
154+ . doc ( message . author . id )
155+ . get ( ) ;
156+ const data = doc . data ( ) ;
157+ const subscriptions : string [ ] = data != null ? data . subscriptions : [ ] ;
158+
159+ // Add the open source subscription to the list of existing subscriptions
160+ await db
161+ . collection ( 'usersSubscriptions' ) // TODO: create a constant variable with all these collection names (e.g. exported in firebase.ts)
162+ . doc ( message . author . id )
163+ . set (
164+ {
165+ subscriptions : [ ...subscriptions , UserSubscriptions . OPEN_SOURCE ] ,
166+ username : message . author . username ,
167+ } ,
168+ { merge : true }
169+ ) ;
170+ }
171+
172+ /**
173+ * Removes the open source subscription for the user to
174+ * stop recieving recurrent messages to remind them to contribute to open source.
175+ * @param roleToAssign
176+ * @param message
177+ *
178+ ! NOTE- This function has to be modified, so that it
179+ ! only deletes the user's OpenSourceSubscription
180+ */
181+ async function removeOpenSourceUserSubscription (
182+ roleToModify : string ,
183+ message : Message
184+ ) {
185+ // Deletes the opensource subscription for the user
186+ await db . collection ( 'userSubscription' ) . doc ( message . author . id ) . delete ( ) ; // TODO: set this to only delete the opensource subscription, not the entire subscriptions field of the user
116187}
0 commit comments