Skip to content

Commit

Permalink
fix: roles not working, i'll come back to this later
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmusgerdin committed Apr 28, 2020
1 parent b5bca3f commit 022e300
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
42 changes: 30 additions & 12 deletions src/commands/User/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = class extends Command {
aliases: ['roleme', 'team', 'squad', 'role'],
description: language => language.get('COMMAND_ROLES_DESCRIPTION'),
extendedHelp: language => language.get('COMMAND_ROLES_EXTENDED'),
usage: '<add|remove|join|leave|list:default> [target:membername] [role:rolename]',
usage: '<add|remove|join|leave|list:default> [target:member] [roleName:...string]',
usageDelim: ' ',
subcommands: true
});
Expand All @@ -37,36 +37,45 @@ module.exports = class extends Command {
return msg.send('', { disableEveryone: true, embed: embed });
}

async add(msg, [target = msg.member, rolename]) {
async add(msg, [target = msg.member, ...roleName]) {
// Fail if there are no joinable roles
if (!msg.guild.settings.get('roles.joinable').length) return msg.reject(msg.language.get('COMMAND_ROLES_NONE_JOINABLE'));
if (!rolename) return msg.reject(msg.language.get('COMMAND_ROLES_NO_ROLE_NAME'));
if (!roleName.length) return msg.reject(msg.language.get('COMMAND_ROLES_NO_ROLE_NAME'));

if (target.id !== msg.member.id) {
// Fail if user is not at least a moderator and is trying to add roles to other users
if (!msg.hasAtLeastPermissionLevel(5) || !msg.member.permissions.has('MANAGE_ROLES')) return msg.reject(msg.language.get('COMMAND_ROLES_NO_MODERATE'));

const canMod = await msg.member.canMod(target.user);
const canMod = await msg.member.canMod(target);

// Fail if user is trying to add roles to someone higher on the hierarchy than themself
if (!canMod) return msg.reject(msg.language.get('COMMAND_ROLES_NO_PERMS', target));
}

// Parsing the roleName array into a string
roleName = roleName.join(' ');

// Fail if there is no role with this name
if (!msg.guild.roles.find(role => role.name.toLowerCase() === roleName.toLowerCase())) return msg.reject(msg.language.get('COMMAND_ROLES_DOES_NOT_EXIST', roleName));

// Fetching role specified by the user
const targetRole = await msg.guild.roles.find(role => role.name.toLowerCase() === roleName.toLowerCase());

// Fail if this role isn't joinable
if (!msg.guild.settings.get('roles.joinable').includes(rolename.id)) return msg.reject(msg.language.get('COMMAND_ROLES_NOT_JOINABLE', rolename));
if (!msg.guild.settings.get('roles.joinable').includes(targetRole.id)) return msg.reject(msg.language.get('COMMAND_ROLES_NOT_JOINABLE', roleName));

// Fail if target already has this role
if (target.roles.has(rolename.id)) return msg.reject(msg.language.get('COMMAND_ROLES_ALREADY_HAVE', rolename, target));
if (target.roles.has(targetRole.id)) return msg.reject(msg.language.get('COMMAND_ROLES_ALREADY_HAVE', roleName, target));

// Add the role to the target
await target.roles.add(rolename);
await target.roles.add(targetRole);
return msg.affirm();
}

async remove(msg, [target = msg.member, rolename]) {
async remove(msg, [target = msg.member, ...roleName]) {
// Fail if there are no joinable roles
if (!msg.guild.settings.get('roles.joinable').length) return msg.reject(msg.language.get('COMMAND_ROLES_NONE_JOINABLE'));
if (!rolename) return msg.reject(msg.language.get('COMMAND_ROLES_NO_ROLE_NAME'));
if (!roleName.length) return msg.reject(msg.language.get('COMMAND_ROLES_NO_ROLE_NAME'));

if (target.id !== msg.member.id) {
// Fail if user is not at least a moderator and is trying to add roles to other users
Expand All @@ -78,14 +87,23 @@ module.exports = class extends Command {
if (!canMod) return msg.reject(msg.language.get('COMMAND_ROLES_NO_PERMS', target));
}

// Parsing the roleName array into a string
roleName = roleName.join(' ');

// Fail if there is no role with this name
if (!msg.guild.roles.find(role => role.name.toLowerCase() === roleName.toLowerCase())) return msg.reject(msg.language.get('COMMAND_ROLES_DOES_NOT_EXIST', roleName));

// Fetching role specified by the user
const targetRole = await msg.guild.roles.find(role => role.name.toLowerCase() === roleName.toLowerCase());

// Fail if this role isn't joinable (or leavable)
if (!msg.guild.settings.get('roles.joinable').includes(rolename.id)) return msg.reject(msg.language.get('COMMAND_ROLES_NOT_LEAVABLE', rolename));
if (!msg.guild.settings.get('roles.joinable').includes(targetRole.id)) return msg.reject(msg.language.get('COMMAND_ROLES_NOT_LEAVABLE', roleName));

// Fail if target does not have this role
if (!target.roles.has(rolename.id)) return msg.reject(msg.language.get('COMMAND_ROLES_DOES_NOT_HAVE', rolename, target));
if (!target.roles.has(targetRole.id)) return msg.reject(msg.language.get('COMMAND_ROLES_DOES_NOT_HAVE', roleName, target));

// Remove the role from the target
await target.roles.remove(rolename);
await target.roles.remove(targetRole);
return msg.affirm();
}

Expand Down
13 changes: 7 additions & 6 deletions src/languages/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,15 +478,16 @@ module.exports = class extends Language {
COMMAND_ROLES_DESCRIPTION: 'Series of commands to allow you to join roles by yourself.',
COMMAND_ROLES_EXTENDED: '**list** - Lists joinable roles and their member counts.\n**add** - Adds a role by its name.\n**remove** - Removes a role by its name.',
COMMAND_ROLES_NONE_JOINABLE: 'There are no joinable roles on this server.',
COMMAND_ROLES_NO_ROLE_NAME: 'Please specify a role by its name, @mention, or ID.',
COMMAND_ROLES_NO_ROLE_NAME: 'Please specify a role by its name.',
COMMAND_ROLES_NO_MODERATE: 'You don\'t have permission to change the roles of other users.',
COMMAND_ROLES_NO_PERMS: (target) => `You are not allowed to moderate ${target}.`,
COMMAND_ROLES_NOT_JOINABLE: (role) => `The role \`${role.name}\` is not joinable in this server.`,
COMMAND_ROLES_ALREADY_HAVE: (role, target) => `${target} already has the role \`${role.name}\`.`,
COMMAND_ROLES_NOT_LEAVABLE: (role) => `You're not allowed to remove the role \`${role.name}\`.`,
COMMAND_ROLES_DOES_NOT_HAVE: (role, target) => `${target} does not have the role \`${role.name}\`.`,
COMMAND_ROLES_DOES_NOT_EXIST: (roleName) => `Sorry, the role \`${roleName}\` does not exist in this server. Please check to make sure you spelled it correctly.`,
COMMAND_ROLES_NOT_JOINABLE: (roleName) => `The role \`${roleName}\` is not joinable in this server.`,
COMMAND_ROLES_ALREADY_HAVE: (roleName, target) => `${target} already has the role \`${roleName}\`.`,
COMMAND_ROLES_NOT_LEAVABLE: (roleName) => `You're not allowed to remove the role \`${roleName}\`.`,
COMMAND_ROLES_DOES_NOT_HAVE: (roleName, target) => `${target} does not have the role \`${roleName}\`.`,
COMMAND_ROLES_SERVER: 'Joinable roles',
COMMAND_ROLES_ROLE: (role) => `• ${role}: ${role.members.size} members`,
COMMAND_ROLES_ROLE: (role) => `• **${role.name}:** ${role.members.size} members`,
COMMAND_SUPPORT_DESCRIPTION: 'Contacts the bot developers in case of an issue with the bot.',
COMMAND_SUPPORT_REQUESTED: 'Support requested',
COMMAND_SUPPORT_JUMPTO: 'Jump to message',
Expand Down

0 comments on commit 022e300

Please sign in to comment.