Skip to content
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

rolesPermOverride command #152

Merged
merged 3 commits into from
Sep 20, 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
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);
}
},
};