diff --git a/commands/rolesPermOverride.js b/commands/rolesPermOverride.js index 7d72d0af..6e655b3a 100644 --- a/commands/rolesPermOverride.js +++ b/commands/rolesPermOverride.js @@ -65,11 +65,68 @@ async function editChannels(interaction, channels) { } } +async function isFixed(interaction, channel) { + const is_valid = is_valid_course_name(channel.name); + + if (!is_valid || channel.type !== "GUILD_TEXT") return true; + + const role = interaction.guild.roles.cache.find( + (r) => r.name.toLowerCase() === channel.name.toLowerCase(), + ); + + if (!role) return false; + + const permissions = channel.permissionOverwrites.cache; + + // clear every individual user permission overwrite for the channel + for (const user of channel.members) { + const userId = user[0]; + const userObj = user[1]; + + if (userObj.user.bot) continue; + + // Check if the member has access via individual perms + if (in_overwrites(permissions, userId)) return false; + } + + if (!in_overwrites(permissions, role.id)) return false; + + return true; +} + +async function allFixed(interaction, channels) { + const unfixed = []; + for (const data of channels) { + const channel = data[1]; + const fixed = await isFixed(interaction, channel); + + if (!fixed) unfixed.push(channel.name); + } + + return unfixed; +} + module.exports = { data: new SlashCommandBuilder() .setName("rolespermoverride") .setDescription( "Looks for matches between roles and course chats and attaches permissions.", + ) + .addBooleanOption((option) => + option + .setName("singlechannel") + .setDescription( + "Should this command only be run on the current channel? (Default: False)", + ) + .setRequired(false), + ) + .addBooleanOption((option) => + option + .setName("check") + .setDescription( + "Should a check be run on if the channel is fixed? (Default: False)", + ) + .setRequired(false), ), async execute(interaction) { try { @@ -84,11 +141,44 @@ module.exports = { // for all roles with name == chat name involving 4 letter prefix comp, seng, engg or binf, - // Get all channels and run function - const channels = await interaction.guild.channels.fetch(); - - await editChannels(interaction, channels); - await interaction.editReply("Successfully ported all user permissions to roles."); + if (!interaction.options.getBoolean("singlechannel")) { + // Get all channels and run specified function + const channels = await interaction.guild.channels.fetch(); + + if (!interaction.options.getBoolean("check")) { + await editChannels(interaction, channels); + await interaction.editReply( + "Successfully ported all user permissions to roles.", + ); + } else { + const unfixed = await allFixed(interaction, channels); + + if (unfixed.length === 0) { + await interaction.editReply("All channels in this server appear fixed."); + } else { + await interaction.editReply( + `The following channels appear unfixed: ${unfixed.join(", ")}`, + ); + } + } + } else { + const channel = interaction.channel; + + if (!interaction.options.getBoolean("check")) { + await editChannels(interaction, [[undefined, channel]]); + await interaction.editReply( + "Successfully ported user permissions to roles in this channel", + ); + } else { + const fixed = await isFixed(interaction, channel); + + if (fixed) { + await interaction.editReply("This channel appears fixed."); + } else { + await interaction.editReply("This channel appears unfixed."); + } + } + } } catch (error) { await interaction.editReply("Error: " + error); }