Skip to content

Commit

Permalink
Appended check command for role permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Domin0de committed Oct 21, 2023
1 parent e93b126 commit f99bdac
Showing 1 changed file with 85 additions and 8 deletions.
93 changes: 85 additions & 8 deletions commands/rolesPermOverride.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,47 @@ 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")
Expand All @@ -74,7 +115,17 @@ module.exports = {
.addBooleanOption((option) =>
option
.setName("singlechannel")
.setDescription("Should this command only be run on the current channel?")
.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) {
Expand All @@ -90,16 +141,42 @@ module.exports = {
// for all roles with name == chat name involving 4 letter prefix comp, seng, engg or binf,

if (!interaction.options.getBoolean("singlechannel")) {
// Get all channels and run function
// Get all channels and run specified 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("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 {
await editChannels(interaction, [[undefined, interaction.channel]]);
await interaction.editReply(
"Successfully ported user permissions to roles in this channel",
);
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);
Expand Down

0 comments on commit f99bdac

Please sign in to comment.