Skip to content

Roles perm override #153

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 4 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
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
96 changes: 4 additions & 92 deletions commands/course.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,52 +128,8 @@ module.exports = {
});
}

// Otherwise, find a channel with the same name as the course
const channel = await interaction.guild.channels.cache.find(
(c) => c.name.toLowerCase() === course.toLowerCase(),
);

// Make sure that the channel exists, and is a text channel
if (channel === undefined) {
return await interaction.reply({
content: `❌ | The course chat for \`${course}\` does not exist. If you'd like for it to be created, please raise a ticket in <#${MODERATION_REQUEST_CHANNEL}>.`,
ephemeral: true,
});
} else if (channel.type !== "GUILD_TEXT") {
return await interaction.reply({
content: `❌ | The course chat for \`${course}\` is not a text channel.`,
ephemeral: true,
});
}

const permissions = new Permissions(
channel.permissionsFor(interaction.user.id).bitfield,
);

// Check if the member already has an entry in the channel's permission overwrites, and update
// the entry if they do just to make sure that they have the correct permissions
if (
permissions.has([
Permissions.FLAGS.VIEW_CHANNEL,
Permissions.FLAGS.SEND_MESSAGES,
])
) {
await channel.permissionOverwrites.edit(interaction.member, {
VIEW_CHANNEL: true,
});
return await interaction.reply({
content: `❌ | You are already in the course chat for \`${course_with_alias}\`.`,
ephemeral: true,
});
}

// Add the member to the channel's permission overwrites
await channel.permissionOverwrites.create(interaction.member, {
VIEW_CHANNEL: true,
});

return await interaction.reply({
content: `✅ | Added you to the chat for ${course_with_alias}.`,
content: `✅ | End of command - ${course_with_alias}.`,
ephemeral: true,
});
} else if (interaction.options.getSubcommand() === COMMAND_LEAVE) {
Expand All @@ -182,7 +138,7 @@ module.exports = {

if (!is_valid_course(course)) {
return await interaction.reply({
content: `❌ | You are not allowed to leave this channel using this command.`,
content: `❌ | Not a valid course.`,
ephemeral: true,
});
}
Expand All @@ -196,63 +152,19 @@ module.exports = {
if (role !== undefined) {
if (!interaction.member.roles.cache.has(role.id)) {
return await interaction.reply({
content: `❌ | You are not in the course chat for \`${course}\`.`,
content: `❌ | You do not have the role for \`${course}\`.`,
ephemeral: true,
});
}

// If they do, let's remove the role from them
await interaction.member.roles.remove(role);
return await interaction.reply({
content: `✅ | Removed you from the chat for \`${course}\`.`,
content: `✅ | Removed you from the role and chat for \`${course}\`.`,
ephemeral: true,
});
}

// Find a channel with the same name as the course
const channel = await interaction.guild.channels.cache.find(
(c) => c.name.toLowerCase() === course.toLowerCase(),
);

// Otherwise, make sure that the channel exists, and is a text channel
if (channel === undefined) {
return await interaction.reply({
content: `❌ | The course chat for \`${course}\` does not exist.`,
ephemeral: true,
});
} else if (channel.type !== "GUILD_TEXT") {
return await interaction.reply({
content: `❌ | The course chat for \`${course}\` is not a text channel.`,
ephemeral: true,
});
}

const permissions = new Permissions(
channel.permissionsFor(interaction.user.id).bitfield,
);

// Check if the member already has an entry in the channel's permission overwrites
if (
!permissions.has([
Permissions.FLAGS.VIEW_CHANNEL,
Permissions.FLAGS.SEND_MESSAGES,
])
) {
return await interaction.reply({
content: `❌ | You are not in the course chat for \`${course}\`.`,
ephemeral: true,
});
}

// Remove the member from the channel's permission overwrites
await channel.permissionOverwrites.delete(interaction.member);

return await interaction.reply({
content: `✅ | Removed you from the course chat for \`${course}\`.`,
ephemeral: true,
});
}

return await interaction.reply("Error: invalid subcommand.");
} catch (error) {
await interaction.reply("Error: " + error);
Expand Down
93 changes: 93 additions & 0 deletions commands/rolesPermOverride.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
const { Permissions } = require("discord.js");

const is_valid_course = (course) => {
const reg_comp_course = /^comp\d{4}$/;
const reg_math_course = /^math\d{4}$/;
const reg_binf_course = /^binf\d{4}$/;
const reg_engg_course = /^engg\d{4}$/;
const reg_seng_course = /^seng\d{4}$/;
const reg_desn_course = /^desn\d{4}$/;
return (
reg_comp_course.test(course.toLowerCase()) ||
reg_math_course.test(course.toLowerCase()) ||
reg_binf_course.test(course.toLowerCase()) ||
reg_engg_course.test(course.toLowerCase()) ||
reg_seng_course.test(course.toLowerCase()) ||
reg_desn_course.test(course.toLowerCase())
);
};

function editChannels(interaction, channels, role) {
channels.forEach((channel) => {
if (
channel.type === "GUILD_TEXT" &&
channel.name.toLowerCase() === role.name.toLowerCase()
) {
// Remove all permissions from a role
role.setPermissions(0n)
.then((updated) =>
console.log(`Updated permissions to ${updated.permissions.bitfield}`),
)
.catch(console.error);
// Set the permissions of the role
// Add the member to the channel's permission overwrites
channel.permissionOverwrites.create(role, {
VIEW_CHANNEL: true,
SEND_MESSAGES: true,
});
console.log(channel.name, role.name);
}
});
}

function editRoles(interaction, roles) {
roles.forEach((role) => {
if (is_valid_course(role.name)) {
interaction.guild.channels
.fetch()
.then(
(channels) => (
editChannels(interaction, channels, role),
console.log(`There are ${channels.size} channels.`)
),
)
.catch(console.error);
}
});
interaction.reply({
content: `✅ | found course chats and matching roles, cleared and set permission overwrites for roles.`,
ephemeral: true,
});
}

module.exports = {
data: new SlashCommandBuilder()
.setName("rolespermoverride")
.setDescription(
"Looks for matches between roles and course chats and attaches permissions.",
),
async execute(interaction) {
try {
if (!interaction.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) {
return await interaction.reply({
content: "You do not have permission to execute this command.",
ephemeral: true,
});
}
// for all roles with name == chat name involving 4 letter prefix comp, seng, engg or binf,

// give the role the permission override to participate in the matching channel.
interaction.guild.roles
.fetch()
.then(
(roles) => (
editRoles(interaction, roles), console.log(`There are ${roles.size} roles.`)
),
)
.catch(console.error);
} catch (error) {
await interaction.reply("Error: " + error);
}
},
};