1- import { Client , DMChannel , Message } from 'discord.js' ;
1+ import { Client , DMChannel , GroupDMChannel , Guild , Message , TextChannel } from 'discord.js' ;
22import ICommand from '../library/iCommand' ;
33
44let Purge : ICommand ;
5+ type channelTypes = 'dm' | 'group' | 'text' | 'voice' | 'category' | 'text' ;
56
67export default Purge = class {
78
@@ -11,42 +12,54 @@ export default Purge = class {
1112 }
1213
1314 public static execute ( args : string [ ] , msg : Message , bot : Client ) {
14- const { guild } = msg ;
15-
16- /* global bot */
17- if ( ! guild . member ( bot . user ) . hasPermission ( 'MANAGE_CHANNELS' ) ) {
18- return msg . reply ( "Bot doesn't have manage channels permissions." ) ;
15+ const { guild, channel } = msg ;
16+ if ( ! this . canManageChannels ( guild , bot ) ) { return this . cannotManageChannelReply ( msg ) ; }
17+ if ( ! this . userHasAccess ( guild , msg ) ) { return this . unauthorizedReply ( msg ) ; }
18+ if ( channel instanceof DMChannel || channel instanceof GroupDMChannel ) {
19+ return this . invalidChannelType ( msg ) ;
1920 }
2021
21- // Make sure the person doing the command is a Board Member
22- const boardRole = guild . roles . find ( role => role . name === 'Board Member' || role . name === 'Admin' ) ;
23- if ( msg . member . roles . has ( boardRole . id ) ) {
24- const { channel } = msg ;
25-
26- if ( channel instanceof DMChannel ) {
27- return ;
28- }
29-
30- // Grab the channels info
31- const chanName = channel . name ;
32- const chanType = channel . type || 'text' ;
33-
34- if ( chanType === 'dm' || chanType === 'group' ) {
35- return ;
36- }
37-
38- // Delete the channel
39- channel . delete ( )
40- . then ( )
41- . catch ( console . error ) ;
42-
43- // Now re-create the channel with the same name and type
44- guild . createChannel ( chanName , chanType )
45- . then ( newChannelName => console . log ( `Created new channel ${ newChannelName } ` ) )
46- . catch ( console . error ) ;
47- } else {
48- return msg . reply ( "Sorry m8, you're not authorized to use that command." ) ;
49- }
22+ const safeChannel : TextChannel = channel ;
23+ return this . recreateChannel ( guild , safeChannel , msg ) ;
5024 }
5125
26+ private static canManageChannels ( guild : Guild , bot : Client ) {
27+ return guild . member ( bot . user ) . hasPermission ( 'MANAGE_CHANNELS' ) ;
28+ }
29+
30+ private static userHasAccess ( guild : Guild , msg : Message ) {
31+ const validRoles = [ 'Board Member' , 'Admin' ] ;
32+ const boardRole = guild . roles . find ( role => validRoles . includes ( role . name ) ) ;
33+ return msg . member . roles . has ( boardRole . id ) ;
34+ }
35+
36+ private static getChannelType ( channel : TextChannel ) : channelTypes {
37+ return channel . type || 'text' ;
38+ }
39+
40+ private static recreateChannel ( guild : Guild , channel : TextChannel , msg : Message ) {
41+ const chanName = channel . name ;
42+ const channelType = this . getChannelType ( channel ) ;
43+ if ( channelType === 'dm' || channelType === 'group' ) { return this . invalidChannelType ( msg ) ; }
44+
45+ channel . delete ( )
46+ . then ( )
47+ . catch ( console . error ) ;
48+
49+ return guild . createChannel ( chanName , channelType )
50+ . then ( newChannelName => console . log ( `Created new channel ${ newChannelName } ` ) )
51+ . catch ( console . error ) ;
52+ }
53+
54+ private static cannotManageChannelReply ( msg : Message ) {
55+ return msg . reply ( "Bot doesn't have manage channels permissions." ) ;
56+ }
57+
58+ private static unauthorizedReply ( msg : Message ) {
59+ return msg . reply ( "Sorry m8, you're not authorized to use that command." ) ;
60+ }
61+
62+ private static invalidChannelType ( msg : Message ) {
63+ return msg . reply ( "Invalid channel type." ) ;
64+ }
5265} ;
0 commit comments