Skip to content

dev/course-channel-fix-5 #160

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

Merged
merged 12 commits into from
Oct 22, 2023
Merged
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
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