Skip to content

Commit

Permalink
Remove invalid protected roles
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianVennen committed Sep 2, 2024
1 parent c5f22a3 commit fc2b5ac
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ export default class AddProtectedRoleCommand extends SubCommand {
const guildSettings = await GuildSettings.get(interaction.guildId);
const embed = new EmbedWrapper();

if (guildSettings.isProtectedRole(role.id)) {
if (guildSettings.protectedRoles.has(role.id)) {
embed.setColor(colors.RED)
.setDescription(`${roleMention(role.id)} is already a protected role!`);
}
else {
guildSettings.addProtectedRole(role.id);
guildSettings.protectedRoles.add(role.id);
await guildSettings.save();
embed.setColor(colors.GREEN)
.setDescription(`Added ${roleMention(role.id)} to the protected roles!`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import SubCommand from '../../SubCommand.js';
import GuildSettings from '../../../settings/GuildSettings.js';
import LineEmbed from '../../../embeds/LineEmbed.js';
import {roleMention} from 'discord.js';
import GuildWrapper from '../../../discord/GuildWrapper.js';

export default class ListProtectedRolesCommand extends SubCommand {

Expand All @@ -11,9 +12,19 @@ export default class ListProtectedRolesCommand extends SubCommand {
.setTitle('Protected roles')
.setDescription('This server has no protected roles');

for (const role of guildSettings.getProtectedRoles()) {
const guild = new GuildWrapper(interaction.guild);

const validRoles = new Set();
for (const role of guildSettings.protectedRoles) {
if (await guild.fetchRole(role)) {
validRoles.add(role);
}
embed.addLine(`- ${roleMention(role)}`);
}
if (validRoles !== guildSettings.protectedRoles) {
guildSettings.protectedRoles = validRoles;
await guildSettings.save();
}

await interaction.reply(embed.toMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export default class RemoveProtectedRoleCommand extends SubCommand {
const guildSettings = await GuildSettings.get(interaction.guildId);
const embed = new EmbedWrapper();

if (guildSettings.isProtectedRole(role.id)) {
guildSettings.removeProtectedRole(role.id);
if (guildSettings.protectedRoles.has(role.id)) {
guildSettings.protectedRoles.delete(role.id);
await guildSettings.save();
embed.setColor(colors.RED)
.setDescription(`Removed ${roleMention(role.id)} from the protected roles!`);
Expand Down
60 changes: 13 additions & 47 deletions src/settings/GuildSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ export default class GuildSettings extends Settings {
},
};

#protectedRoles = [];
/**
* A list of role ids that can't be targeted by moderations
* @type {Set<import('discord.js').Snowflake>}
*/
protectedRoles = new Set();

/**
* @param {import('discord.js').Snowflake} id guild id
Expand Down Expand Up @@ -104,9 +108,9 @@ export default class GuildSettings extends Settings {
}

if (json.protectedRoles instanceof Array)
this.#protectedRoles = json.protectedRoles;
this.protectedRoles = new Set(json.protectedRoles);
if (json.modRoles instanceof Array)
this.#protectedRoles.push(...json.modRoles);
json.modRoles.forEach(role => this.protectedRoles.add(role));

this.#punishments = json.punishments ?? this.#punishments;
}
Expand Down Expand Up @@ -176,13 +180,13 @@ export default class GuildSettings extends Settings {
* @returns {string}
*/
getModerationSettings() {
const protectedRoles = this.getProtectedRoles().map(role => '- ' + roleMention(role)).join('\n') || 'None';
const protectedRoles = Array.from(this.protectedRoles).map(role => '- ' + roleMention(role)).join('\n') || 'None';

return `Log: ${this.logChannel ? channelMention(this.logChannel) : 'disabled'}\n` +
`Message Log: ${this.messageLogChannel ? channelMention(this.messageLogChannel) : 'disabled'}\n` +
`Join Log: ${this.joinLogChannel ? channelMention(this.joinLogChannel) : 'disabled'}\n` +
`Muted role: ${this.mutedRole ? roleMention(this.mutedRole) : 'disabled'}\n` +
`Protected roles: ${this.getProtectedRoles().length ? '\n' : ''}${protectedRoles}\n`;
`Protected roles: ${this.protectedRoles.size ? '\n' : ''}${protectedRoles}\n`;
}

/**
Expand Down Expand Up @@ -240,15 +244,6 @@ export default class GuildSettings extends Settings {
return config.data.featureWhitelist.includes(this.id);
}

/**
* Is this a protected role?
* @param {import('discord.js').Snowflake} role role id
* @return {Boolean}
*/
isProtectedRole(role) {
return this.#protectedRoles.includes(role);
}

/**
* Is this member protected?
* @async
Expand All @@ -257,43 +252,12 @@ export default class GuildSettings extends Settings {
*/
isProtected(member) {
for (let [key] of member.roles.cache) {
if (this.isProtectedRole(key))
if (this.protectedRoles.has(key))
return true;
}
return false;
}

/**
* Add this role to the protected roles
* @param {import('discord.js').Snowflake} role role id
*/
addProtectedRole(role) {
if (!this.isProtectedRole(role)) {
this.#protectedRoles.push(role);
}
}

/**
* Remove this role from the protected roles
* @param {import('discord.js').Snowflake} role role id
*/
removeProtectedRole(role) {
let newRoles = [];
for (let protectedRole of this.#protectedRoles) {
if (protectedRole !== role)
newRoles.push(role);
}
this.#protectedRoles = newRoles;
}

/**
* get all protected roles
* @return {import('discord.js').Snowflake[]}
*/
getProtectedRoles() {
return this.#protectedRoles;
}

/**
* get a specific punishment
* @param {Number} strikes
Expand Down Expand Up @@ -387,7 +351,9 @@ export default class GuildSettings extends Settings {

//copy private properties
cleanObject.punishments = this.#punishments;
cleanObject.protectedRoles = this.#protectedRoles;

//convert set to array
cleanObject.protectedRoles = Array.from(this.protectedRoles);

return super.getDataObject(cleanObject);
}
Expand Down

0 comments on commit fc2b5ac

Please sign in to comment.