Skip to content

Commit

Permalink
rolesPermOverride command is a script or one-time use command to atta…
Browse files Browse the repository at this point in the history
…ch every course role with permissions to view and send messages in any text channel with identical name. Please review and test before merging
  • Loading branch information
AcdSoftCo committed Sep 16, 2023
1 parent 9a4c9ec commit 2f59c71
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions commands/rolesPermOverride.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
const { Permissions } = require("discord.js");



// map of course aliases to their actual names
const course_aliases = {
comp6841: "comp6441",
comp9044: "comp2041",
comp3891: "comp3231",
comp9201: "comp3231",
comp9101: "comp3121",
comp9331: "comp3331",
comp9415: "comp3421",
comp9801: "comp3821",
comp9102: "comp3131",
comp9154: "comp3151",
comp9164: "comp3161",
comp9211: "comp3211",
comp9222: "comp3221",
comp9814: "comp3411",
comp9511: "comp3511",
comp9900: "comp3900",
seng4920: "comp4920",
comp9337: "comp4337",
math1141: "math1131",
math1241: "math1231",
};

const get_real_course_name = (course) => {
if (course_aliases[course.toLowerCase()]) {
return course_aliases[course.toLowerCase()];
}
return course.toLowerCase();
};




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())
);
};

module.exports = {
data: new SlashCommandBuilder()
.setName("rolespermoverride")
.setDescription("Looks for matches between roles and course chats and attaches permissions."),
async execute(interaction) {
try {
// 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.
message.guild.roles.forEach(role => {
if (is_valid_course(role)) {
console.log("hi");
message.guild.channels.forEach(channel => {
if (channel.type === "GUILD_TEXT" && channel.name.toLowerCase() === role.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,
});
interaction.reply({
content: `✅ | removed all permissions and set new permission overwrites for
${interaction.channel.name} and ${role.name}.`,
ephemeral: true,
});
}
});
}
});


} catch (error) {
await interaction.reply("Error: " + error);
}
},
};

0 comments on commit 2f59c71

Please sign in to comment.