2020-07-06 03:15:06 -04:00
|
|
|
import { Message, Role } from 'eris';
|
2020-07-09 04:41:29 -04:00
|
|
|
import { createPaginationEmbed } from 'eris-pagination';
|
2020-07-06 03:15:06 -04:00
|
|
|
import { Client, Command, RichEmbed } from '../class';
|
|
|
|
|
|
|
|
export default class Rank extends Command {
|
|
|
|
constructor(client: Client) {
|
|
|
|
super(client);
|
|
|
|
this.name = 'rank';
|
|
|
|
this.description = 'Joins/leaves a self-assignable rank. Run this command without arguments to get a list of self-assignable roles.';
|
|
|
|
this.usage = `${this.client.config.prefix}rank <rank>`;
|
|
|
|
this.permissions = 0;
|
|
|
|
this.guildOnly = true;
|
|
|
|
this.enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async run(message: Message, args: string[]) {
|
|
|
|
try {
|
|
|
|
if (!args[0]) {
|
|
|
|
const roles = await this.client.db.Rank.find();
|
2020-07-09 04:41:29 -04:00
|
|
|
const rankArray: [{ name: string, value: string }?] = [];
|
2020-07-06 03:15:06 -04:00
|
|
|
for (const rank of roles.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
|
|
let perms: string;
|
|
|
|
if (rank.permissions.includes('0')) {
|
|
|
|
perms = 'Everyone';
|
|
|
|
} else {
|
|
|
|
const rolesArray: Role[] = [];
|
|
|
|
rank.permissions.forEach((r) => {
|
2020-07-09 04:41:29 -04:00
|
|
|
rolesArray.push(this.mainGuild.roles.get(r));
|
2020-07-06 03:15:06 -04:00
|
|
|
});
|
2020-07-29 00:46:52 -04:00
|
|
|
perms = rolesArray.map((r) => this.mainGuild.roles.get(r.id)).sort((a, b) => b.position - a.position).map((r) => `<@&${r.id}>`).join(', ');
|
2020-07-06 03:15:06 -04:00
|
|
|
}
|
2020-07-06 03:40:54 -04:00
|
|
|
let hasRank = false;
|
|
|
|
if (message.member.roles.includes(rank.roleID)) hasRank = true;
|
2020-07-09 04:41:29 -04:00
|
|
|
rankArray.push({ name: rank.name, value: `${hasRank ? '*You have this role.*\n' : ''}__Description:__ ${rank.description}\n__Permissions:__ ${perms}` });
|
2020-07-06 03:15:06 -04:00
|
|
|
}
|
2020-07-09 04:41:29 -04:00
|
|
|
const ranksSplit = this.client.util.splitFields(rankArray);
|
|
|
|
const cmdPages: RichEmbed[] = [];
|
|
|
|
ranksSplit.forEach((split) => {
|
|
|
|
const embed = new RichEmbed();
|
|
|
|
embed.setTitle('Ranks');
|
|
|
|
embed.setDescription(`Use \`${this.client.config.prefix}rank <rank name>\` to join/leave the rank.`);
|
|
|
|
embed.setFooter(`Requested by: ${message.author.username}#${message.author.discriminator} | ${this.client.user.username}`, message.author.avatarURL);
|
|
|
|
embed.setTimestamp();
|
|
|
|
split.forEach((c) => embed.addField(c.name, c.value));
|
2020-07-12 00:39:34 -04:00
|
|
|
return cmdPages.push(embed);
|
2020-07-09 04:41:29 -04:00
|
|
|
});
|
|
|
|
if (cmdPages.length === 1) return message.channel.createMessage({ embed: cmdPages[0] });
|
|
|
|
return createPaginationEmbed(message, cmdPages);
|
2020-07-06 03:15:06 -04:00
|
|
|
}
|
2020-07-06 03:20:18 -04:00
|
|
|
const role = this.client.util.resolveRole(args.join(' '), this.client.guilds.get(this.client.config.guildID));
|
2020-07-06 03:15:06 -04:00
|
|
|
if (!role) return this.error(message.channel, 'The role you specified doesn\'t exist.');
|
|
|
|
const entry = await this.client.db.Rank.findOne({ roleID: role.id }).lean().exec();
|
|
|
|
if (!entry) return this.error(message.channel, 'The rank you specified doesn\'t exist.');
|
|
|
|
if (!message.member.roles.includes(entry.roleID)) {
|
|
|
|
let permCheck: boolean;
|
|
|
|
if (entry.permissions.includes('0')) {
|
|
|
|
permCheck = true;
|
|
|
|
} else {
|
|
|
|
permCheck = entry.permissions.some((item) => message.member.roles.includes(item));
|
|
|
|
}
|
|
|
|
if (!permCheck) return this.error(message.channel, 'Permission denied.');
|
2020-08-19 16:56:08 -04:00
|
|
|
await message.member.addRole(entry.roleID, 'User self-assigned this role.');
|
2020-07-06 03:15:06 -04:00
|
|
|
this.success(message.channel, `You have self-assigned ${entry.name}.`);
|
|
|
|
} else if (message.member.roles.includes(entry.roleID)) {
|
2020-08-19 16:56:08 -04:00
|
|
|
await message.member.removeRole(entry.roleID, 'User has removed a self-assignable role.');
|
2020-07-06 03:15:06 -04:00
|
|
|
this.success(message.channel, `You have removed ${entry.name}.`);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} catch (err) {
|
|
|
|
return this.client.util.handleError(err, message, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|