Skip to content

Commit

Permalink
dev/course-channel-fix-5 (#160)
Browse files Browse the repository at this point in the history
* course-chat-fix-2, make new roles where there arent any in /rolespermoverride and also remove individual permission overwrites, and also updates to /course join to catch when there doesn't exist a role for a course and also /course leave to attempt to remove individual permission overwrites along with removing course role PLEASE TEST THOROUGHLY AND INSPECT CODE THOROUGHLY. I wrote this ASAP and haven't got time to properly review it.

* Modified functions for course channel fixes

* Check for if user only has read perms for overwrite replacing and fix looping over members

* Fix to functionality of iteration and editing roles

* Fix to initial working state

* Fix linting

* Additional fixes to ordering and component operations to remove role and individual permissions correctly

* Appended filter for both view only perms and view + send messages perms

* Appended single channel mode for permissions override

* Appended check command for role permissions

* Resolved merge conflicts in dev/course-channel-fix-4

---------

Co-authored-by: AcdSoftCo <[email protected]>
Co-authored-by: Wolfdragon24 <[email protected]>
  • Loading branch information
3 people authored Oct 22, 2023
1 parent 7912884 commit 027f8fe
Showing 1 changed file with 95 additions and 6 deletions.
101 changes: 95 additions & 6 deletions commands/rolesPermOverride.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -79,16 +136,48 @@ module.exports = {
ephemeral: true,
});
}

await interaction.deferReply();

// 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);
}
Expand Down

0 comments on commit 027f8fe

Please sign in to comment.