Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trusted role => autorole #61

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/commands/Moderation/Member Modding/nickname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@ import { CommandOptions } from 'klasa';
permissionLevel: PermissionsLevels.MODERATOR,
requiredPermissions: ['MANAGE_NICKNAMES'],
runIn: ['text'],
usage: '<user:username> [nickname:string]'
usage: '<user:username> [nickname:string{,32}]'
})
export default class extends SteveCommand {

public async run(msg: GuildMessage, [user, nickname]: [User, string]): Promise<Message> {
const member = await msg.guild.members.fetch(user);
if (!member) return msg.channel.send(msg.guild.language.tget('userNotInGuild', user.tag));

await member.setNickname(nickname);
try {
await member.setNickname(nickname);

return msg.channel.send(nickname
? msg.guild.language.tget('commandNicknameSet', user.tag)
: msg.guild.language.tget('commandNicknameCleared', user.tag));
return msg.channel.send(nickname
? msg.guild.language.tget('commandNicknameSet', user.tag)
: msg.guild.language.tget('commandNicknameCleared', user.tag));
} catch (err) {
return msg.channel.send(msg.guild.language.tget('commandNicknameUnableToSet', err, user.tag));
}
}

}
7 changes: 0 additions & 7 deletions src/commands/Self-Assign/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ export default class extends SteveCommand {
public async run(msg: GuildMessage, roles: Role[]): Promise<Message | null> {
if (Reflect.has(msg.flagArgs, 'list')) return this.listAssignableRoles(msg);

const trustedRoleID = msg.guild.settings.get(GuildSettings.Roles.Trusted);
const trustedRoleRequirement = msg.guild.settings.get(GuildSettings.Roles.RequireTrustedRoleForSelfAssign) as boolean;

if (trustedRoleID && trustedRoleRequirement && !msg.member.roles.cache.has(trustedRoleID)) {
throw msg.guild.language.tget('commandAssignRoleNeedTrusted', msg.guild.roles.cache.get(trustedRoleID)!.name);
}

const removed: string[] = [];
const added: string[] = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import { Message, Role } from 'discord.js';
import { CommandOptions } from 'klasa';

@ApplyOptions<CommandOptions>({
description: lang => lang.tget('commandSetTrustedRoleDescription'),
extendedHelp: lang => lang.tget('commandSetTrustedRoleExtended'),
description: lang => lang.tget('commandSetAutoRoleDescription'),
extendedHelp: lang => lang.tget('commandSetAutoRoleExtended'),
permissionLevel: PermissionsLevels.MODERATOR,
runIn: ['text'],
usage: '<role:rolename>'
})
export default class extends SteveCommand {

public async run(msg: GuildMessage, [role]: [Role]): Promise<Message> {
await msg.guild.settings.update(GuildSettings.Roles.Trusted, role.id, msg.guild.id);
await msg.guild.settings.update(GuildSettings.Roles.Auto, role.id, msg.guild.id);

return msg.channel.send(msg.guild.language.tget('commandSetTrustedRoleSet', role.name));
return msg.channel.send(msg.guild.language.tget('commandSetAutoRoleSet', role.name));
}

}
32 changes: 0 additions & 32 deletions src/commands/Server Settings/toggletrustedrolerequirement.ts

This file was deleted.

16 changes: 9 additions & 7 deletions src/events/guildMemberAdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { GuildMember, Message, MessageEmbed, TextChannel } from 'discord.js';
import { GuildSettings } from '@lib/types/settings/GuildSettings';
import { friendlyDuration, getExecutor, floatPromise } from '@utils/util';
import { LogColors } from '@lib/types/Enums';
import { TrustedRoleSetting } from './guildMemberUpdate';

export default class extends Event {

Expand All @@ -13,7 +12,7 @@ export default class extends Event {
if (memberlog && memberlog.isGuildTextChannel()) floatPromise(this, this.handleLog(member, memberlog));
}

this.handleTrustedRole(member);
floatPromise(this, this.handleAutoRole(member));
}

private async handleLog(member: GuildMember, memberlog: TextChannel): Promise<Message> {
Expand Down Expand Up @@ -41,12 +40,15 @@ export default class extends Event {
return memberlog.send(embed);
}

private handleTrustedRole(member: GuildMember): void {
const { guild } = member;
const trustedRole = guild.roles.cache.get(guild.settings.get(GuildSettings.Roles.Trusted));
const trustedRoleSetting: TrustedRoleSetting = guild.settings.get(GuildSettings.Roles.GiveTrustedRoleOn);
private async handleAutoRole(member: GuildMember) {
const autoRoleSetting = member.guild.settings.get(GuildSettings.Roles.AutoRoleSetting) as string;

if (trustedRole && trustedRoleSetting === 'join') floatPromise(this, member.roles.add(trustedRole));
if (autoRoleSetting === 'join') {
const autoRoleID = member.guild.settings.get(GuildSettings.Roles.Auto);
const role = member.guild.roles.cache.get(autoRoleID);

if (role) await member.roles.add(role);
}
}

}
31 changes: 15 additions & 16 deletions src/events/guildMemberUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Event } from 'klasa';
import { GuildMember, TextChannel, Role, Guild, Message, MessageEmbed } from 'discord.js';
import { GuildMember, TextChannel, Guild, Message, MessageEmbed } from 'discord.js';
import { GuildSettings } from '@lib/types/settings/GuildSettings';
import { floatPromise, getExecutor } from '@utils/util';
import { LogColors } from '@lib/types/Enums';
Expand All @@ -16,7 +16,7 @@ export default class extends Event {
}
}

if (!oldMember.roles.cache.equals(newMember.roles.cache)) floatPromise(this, this.handleTrustedRole(oldMember, newMember));
if (!oldMember.roles.cache.equals(newMember.roles.cache)) floatPromise(this, this.handleAutoRole(oldMember, newMember));
}

private async logDisplayNameChange(oldMember: GuildMember, newMember: GuildMember, memberlog: TextChannel): Promise<Message> {
Expand Down Expand Up @@ -54,29 +54,28 @@ export default class extends Event {
return memberlog.send(embed);
}

private async handleTrustedRole(oldMember: GuildMember, newMember: GuildMember): Promise<void> {
private async getRoleFromAuditLogs(guild: Guild) {
const logs = await guild.fetchAuditLogs({ limit: 1, type: 'MEMBER_ROLE_UPDATE' });
return logs.entries.first()!.changes![0].new[0];
}

private async handleAutoRole(oldMember: GuildMember, newMember: GuildMember) {
const { guild } = newMember;
const role = await this.getRoleFromAuditLogs(newMember.guild);
const trustedRole = guild.roles.cache.get(guild.settings.get(GuildSettings.Roles.Trusted));
const trustedRoleSetting: TrustedRoleSetting = guild.settings.get(GuildSettings.Roles.GiveTrustedRoleOn);
const autoRole = guild.roles.cache.get(guild.settings.get(GuildSettings.Roles.Auto));
const autoRoleSetting = guild.settings.get(GuildSettings.Roles.AutoRoleSetting);

const restrictedRoles = guild.settings.get(GuildSettings.Roles.Restricted) as string[];

if (trustedRoleSetting === 'role') {
if (newMember.roles.cache.size === 2 && oldMember.roles.cache.size < newMember.roles.cache.size) {
if (autoRoleSetting === 'role') {
if (newMember.roles.cache.size === 2 && oldMember.roles.cache.size < newMember.roles.cache.size) { // TODO: handle multiple roles being added at once
if (!restrictedRoles.includes(role.id)) {
if (trustedRole && !newMember.roles.cache.has(trustedRole.id)) {
floatPromise(this, newMember.roles.add(trustedRole));
if (autoRole && !newMember.roles.cache.has(autoRole.id)) {
floatPromise(this, newMember.roles.add(autoRole));
}
}
}
}
}

private async getRoleFromAuditLogs(guild: Guild): Promise<Role> {
const logs = await guild.fetchAuditLogs({ limit: 1, type: 'MEMBER_ROLE_UPDATE' });
return logs.entries.first()!.changes![0].new[0];
}

}

export type TrustedRoleSetting = 'none' | 'join' | 'role';
28 changes: 10 additions & 18 deletions src/languages/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ export default class extends Language {
}),
commandNicknameSet: user => `${user}'s nickname has been set.`,
commandNicknameCleared: user => `${user}'s nickname has been cleared.`,
commandNicknameUnableToSet: (error, userTag) => `Unable to change ${userTag}'s nickname: **${error}**`,
commandRoleDescription: 'Add or remove a role from a member',
commandRoleExtended: builder.display('role', {
examples: [
Expand Down Expand Up @@ -755,7 +756,6 @@ export default class extends Language {
commandAssignNotAssignable: role => `${Emojis.REDX} The ${role} role is not self-assignable.`,
commandAssignRoleAdd: roles => `${Emojis.PLUS} Added role(s): \`${roles}\``,
commandAssignRoleRemove: roles => `${Emojis.MINUS} Removed role(s): \`${roles}\``,
commandAssignRoleNeedTrusted: role => `You need to have the **${role}** role to do that!`,
/**
* ################################
* # MEMBER INFO #
Expand Down Expand Up @@ -927,6 +927,13 @@ export default class extends Language {
reminder: 'This command does not add any extra Discord permissions to the role, it just gives this role access to server staff commands.'
}),
commandSetAdministratorRoleSet: role => `The ${role} role will be used as this server's administrator role.`,
commandSetAutoRoleDescription: 'Set the server\'s auto role',
commandSetAutoRoleExtended: builder.display('setautorole', { // TODO: better help
explainedUsage: [
['rolename', 'You can use the name of a role, it\'s snowflake (long id), or tag the role (if it is taggable).']
]
}),
commandSetAutoRoleSet: roleName => `The ${roleName} will be used as this server's auto role.`,
commandSetModeratorRoleDescription: 'Set the server\'s moderator role',
commandSetModeratorRoleExtended: builder.display('setmoderatorrole', {
explainedUsage: [
Expand All @@ -935,14 +942,6 @@ export default class extends Language {
reminder: 'This command does not add any extra Discord permissions to the role, it just gives this role access to server staff commands.'
}),
commandSetModeratorRoleSet: role => `The ${role} role will be used as this server's moderator role.`,
commandSetTrustedRoleDescription: 'Set the server\'s trusted role',
commandSetTrustedRoleExtended: builder.display('settrustedrole', {
explainedUsage: [
['rolename', 'You can use the name of a role, it\'s snowflake (long id), or tag the role (if it is taggable).']
],
reminder: 'This command does not add any extra Discord permissions to the role.'
}),
commandSetTrustedRoleSet: role => `The ${role} role will be used as this server's trusted role.`,
commandSetMutedRoleDescription: 'Set the server\'s muted role',
commandSetMutedRoleExtended: builder.display('setmutedrole', {
explainedUsage: [
Expand Down Expand Up @@ -979,7 +978,7 @@ export default class extends Language {
['reset', 'Clears the list of restricted roles.'],
['show', 'Displays the list of restricted roles.']
],
extendedHelp: 'Restricted roles cannot be viewed with the `roleinfo` command, except for when a server staff member is the person using the command. Members who receive a restricted role as their first role will not be given the server\'s trusted role if one has been set.'
extendedHelp: 'Restricted roles cannot be viewed with the `roleinfo` command, except for when a server staff member is the person using the command.'
}),
commandManageRestrictedRolesManageRemoved: removedRoles => `${Emojis.MINUS} Removed roles: ${removedRoles}\n`,
commandManageRestrictedRolesManageAdded: addedRoles => `${Emojis.PLUS} Added roles: ${addedRoles}`,
Expand Down Expand Up @@ -1029,12 +1028,6 @@ export default class extends Language {
extendedHelp: 'This setting defaults to being off.'
}),
commandToggleDeletePinMessages: disabling => `Pinned message notifications will ${disabling ? 'no longer' : 'now'} be deleted.`,
commandToggleTrustedRoleRequirementDescription: 'Choose whether the server\'s trusted role is required to self-assign roles',
commandToggleTrustedRoleRequirementExtended: builder.display('toggletrustedrolerequirement', {
extendedHelp: 'The server must have a trusted role set before this command can be used.'
}),
commandToggleTrustedRoleRequirementDisable: 'The trusted role is no longer required to self-assign roles.',
commandToggleTrustedRoleRequirementEnable: 'The trusted role is now required to self-assign roles.',
/**
* ################################
* # USER SETTINGS COMMANDS #
Expand Down Expand Up @@ -1248,8 +1241,7 @@ export default class extends Language {
* # SERIALIZERS #
* ################################
*/
serializerColorInvalidHex: code => `${code} is not a valid hex.`,
serializerTrustedRoleSettingInvalidSetting: setting => `${setting} is not a valid setting for giveTrustedRoleOn.`
serializerColorInvalidHex: code => `${code} is not a valid hex.`
};


Expand Down
7 changes: 3 additions & 4 deletions src/lib/schemas/Guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ export default Client.defaultGuildSchema
.add('roles', roles => roles
.add('administrator', 'Role')
.add('assignable', 'Role', { array: true })
.add('auto', 'Role')
.add('autoRoleSetting', 'AutoRole', { 'default': 'none' })
.add('deafened', 'Role')
.add('moderator', 'Role')
.add('muted', 'Role')
.add('restricted', 'Role', { array: true })
.add('trusted', 'Role')
.add('giveTrustedRoleOn', 'TrustedRoleSetting', { 'default': 'none' })
.add('requireTrustedRoleForSelfAssign', 'Boolean', { 'default': false }))
.add('restricted', 'Role', { array: true }))
.add('roleAliases', 'any', { array: true, configurable: false })
.add('snippets', 'any', { array: true, configurable: false })
.add('wordBlacklist', wordBlacklist => wordBlacklist
Expand Down
5 changes: 0 additions & 5 deletions src/lib/setup/PermissionLevels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import { Permissions } from 'discord.js';

export default Client.defaultPermissionLevels = new PermissionLevels()
.add(PermissionsLevels.EVERYONE, () => true)
.add(PermissionsLevels.TRUSTED, msg => msg.member
? msg.guild!.settings.get(GuildSettings.Roles.Trusted)
? msg.member.roles.cache.has(msg.guild!.settings.get(GuildSettings.Roles.Trusted))
: false
: false, { fetch: true })
.add(PermissionsLevels.MODERATOR, msg => msg.member
? msg.guild!.settings.get(GuildSettings.Roles.Moderator)
? msg.member.roles.cache.has(msg.guild!.settings.get(GuildSettings.Roles.Moderator))
Expand Down
1 change: 0 additions & 1 deletion src/lib/types/Enums.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const enum LogColors {

export const enum PermissionsLevels {
EVERYONE = 0,
TRUSTED = 1,
MODERATOR = 7,
ADMINISTRATOR = 8,
OWNER = 10
Expand Down
14 changes: 5 additions & 9 deletions src/lib/types/Languages.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ declare module 'klasa' {
commandNicknameExtended: string;
commandNicknameSet: (user: string) => string;
commandNicknameCleared: (user: string) => string;
commandNicknameUnableToSet: (error: string, userTag: string) => string;
commandRoleDescription: string;
commandRoleExtended: string;
commandRoleRemove: (roles: string) => string;
Expand Down Expand Up @@ -299,7 +300,6 @@ declare module 'klasa' {
commandAssignNotAssignable: (role: string) => string;
commandAssignRoleAdd: (roles: string) => string;
commandAssignRoleRemove: (roles: string) => string;
commandAssignRoleNeedTrusted: (role: string) => string;
commandWhoIsDescription: string;
commandWhoIsExtended: string;
commandWhoIsDate: (duration: string, date: string) => string;
Expand Down Expand Up @@ -378,12 +378,12 @@ declare module 'klasa' {
commandSetAdministratorRoleDescription: string;
commandSetAdministratorRoleExtended: string;
commandSetAdministratorRoleSet: (role: string) => string;
commandSetAutoRoleDescription: string;
commandSetAutoRoleExtended: string;
commandSetAutoRoleSet: (roleName: string) => string;
commandSetModeratorRoleDescription: string;
commandSetModeratorRoleExtended: string;
commandSetModeratorRoleSet: (role: string) => string;
commandSetTrustedRoleDescription: string;
commandSetTrustedRoleExtended: string;
commandSetTrustedRoleSet: (role: string) => string;
commandSetMutedRoleDescription: string;
commandSetMutedRoleExtended: string;
commandSetMutedRoleSet: (role: string) => string;
Expand Down Expand Up @@ -416,10 +416,6 @@ declare module 'klasa' {
commandToggleDeletePinMessagesDescription: string;
commandToggleDeletePinMessagesExtended: string;
commandToggleDeletePinMessages: (disabling: boolean) => string;
commandToggleTrustedRoleRequirementDescription: string;
commandToggleTrustedRoleRequirementExtended: string;
commandToggleTrustedRoleRequirementDisable: string;
commandToggleTrustedRoleRequirementEnable: string;
commandSetEmbedColorDescription: string;
commandSetEmbedColorExtended: string;
resolverInvalidColor: (hex: string) => string;
Expand Down Expand Up @@ -573,6 +569,6 @@ declare module 'klasa' {
inhibitorPingProtectionEveryone: string;
inhibitorPingprotectionEveryone: string;
serializerColorInvalidHex: (code: string) => string;
serializerTrustedRoleSettingInvalidSetting: (setting: string) => string;
serializerAutoRoleInvalid: (setting: string) => string;
}
}
5 changes: 2 additions & 3 deletions src/lib/types/settings/GuildSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ export namespace GuildSettings {
export namespace Roles {
export const Administrator = 'roles.administrator';
export const Assignable = 'roles.assignable';
export const Auto = 'roles.auto';
export const AutoRoleSetting = 'roles.autoRoleSetting';
export const Deafened = 'roles.deafened';
export const Moderator = 'roles.moderator';
export const Muted = 'roles.muted';
export const Restricted = 'roles.restricted';
export const Trusted = 'roles.trusted';
export const GiveTrustedRoleOn = 'roles.giveTrustedRoleOn';
export const RequireTrustedRoleForSelfAssign = 'roles.requireTrustedRoleForSelfAssign';
}

export namespace WordBlacklist {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Language, SchemaPiece, Serializer } from 'klasa';

export default class extends Serializer {

// eslint-disable-next-line @typescript-eslint/require-await
public async deserialize(data: string, piece: SchemaPiece, lang: Language): Promise<string> {
if (data === 'none' || data === 'join' || data === 'role') return data;
throw lang.tget('serializerTrustedRoleSettingInvalidSetting', data);
throw lang.tget('serializerAutoRoleInvalid', data);
}

public serialize(value: string): string {
Expand Down