Skip to content

Commit 7628f42

Browse files
committed
Methodize™ Code
Fixes #84 - Turn one method into many. - Wow this thing really needs tests with all of it's crazy edge cases. - This channel typing + type gaurds are a PITA + Crazy - TODO: `execute` can instantiate an instance of this class - That sould clean up the redundant dependency injection going on
1 parent 7307d8b commit 7628f42

File tree

1 file changed

+48
-35
lines changed

1 file changed

+48
-35
lines changed

src/commands/purge.ts

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { Client, DMChannel, Message } from 'discord.js';
1+
import { Client, DMChannel, GroupDMChannel, Guild, Message, TextChannel } from 'discord.js';
22
import ICommand from '../library/iCommand';
33

44
let Purge: ICommand;
5+
type channelTypes = 'dm' | 'group' | 'text' | 'voice' | 'category' | 'text';
56

67
export 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

Comments
 (0)