Skip to content

Commit

Permalink
rolesPermOverride command (#152)
Browse files Browse the repository at this point in the history
* 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

* BUGGY CODE but still progress over the previous commit, I will come back this evening to fix

* 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

---------

Co-authored-by: AcdSoftCo <106219586+AcdSoftCo@users.noreply.github.com>
  • Loading branch information
zcDay1 and AcdSoftCo authored Sep 20, 2023
1 parent 617c373 commit da9b795
Showing 1 changed file with 93 additions and 0 deletions.
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);
}
},
};

0 comments on commit da9b795

Please sign in to comment.