From d84dbb6ed4c5d22ae320c79ebd375b0c7ec54c93 Mon Sep 17 00:00:00 2001 From: AcdSoftCo <106219586+AcdSoftCo@users.noreply.github.com> Date: Sat, 16 Sep 2023 11:02:11 +1000 Subject: [PATCH 1/3] rolesPermOverride command is a script or one-time use command to attach every course role with permissions to view and send messages in any text channel with identical name. Please review and test before mergin --- commands/rolesPermOverride.js | 96 +++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 commands/rolesPermOverride.js diff --git a/commands/rolesPermOverride.js b/commands/rolesPermOverride.js new file mode 100644 index 00000000..7f484a71 --- /dev/null +++ b/commands/rolesPermOverride.js @@ -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); + } + }, +}; \ No newline at end of file From 4e7f70faa9a78c47d371ed077662484c369bcfcb Mon Sep 17 00:00:00 2001 From: AcdSoftCo <106219586+AcdSoftCo@users.noreply.github.com> Date: Tue, 19 Sep 2023 17:24:35 +1000 Subject: [PATCH 2/3] BUGGY CODE but still progress over the previous commit, I will come back this evening to fix --- commands/rolesPermOverride.js | 60 ++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/commands/rolesPermOverride.js b/commands/rolesPermOverride.js index 7f484a71..f2f3f711 100644 --- a/commands/rolesPermOverride.js +++ b/commands/rolesPermOverride.js @@ -55,6 +55,38 @@ const is_valid_course = (course) => { ); }; +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, + }); + interaction.reply({ + content: `✅ | removed all permissions and set new permission overwrites for + ${interaction.channel.name} and ${role.name}.`, + ephemeral: true, + }); + } + }); +} + +function editRoles(interaction, roles) { + roles.forEach(role => { + if (is_valid_course(role.name)) { + const channels = interaction.guild.channels.fetch() + .then(channels => (editChannels(interaction, channels, role), console.log(`There are ${channels.size} channels.`))) + .catch(console.error); + } + }); +} + module.exports = { data: new SlashCommandBuilder() .setName("rolespermoverride") @@ -63,31 +95,9 @@ module.exports = { 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, - }); - } - }); - } - }); - + const all_roles = 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); From 70eca10a89623e1011d0d8738078b157537a1af1 Mon Sep 17 00:00:00 2001 From: AcdSoftCo <106219586+AcdSoftCo@users.noreply.github.com> Date: Tue, 19 Sep 2023 22:40:48 +1000 Subject: [PATCH 3/3] rolesPermOverride command: now admin-only command, and I've tested it for various cases regarding if a channel exists, if a role exists for a channel that doesnt exist, if there are two channels with the same name as a role and vice versa, and it works without error --- commands/rolesPermOverride.js | 171 ++++++++++++++++------------------ 1 file changed, 79 insertions(+), 92 deletions(-) diff --git a/commands/rolesPermOverride.js b/commands/rolesPermOverride.js index f2f3f711..21e05e75 100644 --- a/commands/rolesPermOverride.js +++ b/commands/rolesPermOverride.js @@ -1,106 +1,93 @@ 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()) - ); + 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, - }); - interaction.reply({ - content: `✅ | removed all permissions and set new permission overwrites for - ${interaction.channel.name} and ${role.name}.`, - ephemeral: true, - }); - } - }); + 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)) { - const channels = interaction.guild.channels.fetch() - .then(channels => (editChannels(interaction, channels, role), console.log(`There are ${channels.size} channels.`))) - .catch(console.error); - } - }); + 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 { - // 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. - const all_roles = interaction.guild.roles.fetch() - .then(roles => (editRoles(interaction, roles), console.log(`There are ${roles.size} roles.`))) - .catch(console.error); + 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, - } catch (error) { - await interaction.reply("Error: " + error); - } - }, -}; \ No newline at end of file + // 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); + } + }, +};