|
| 1 | +const { SlashCommandBuilder } = require("@discordjs/builders"); |
| 2 | +const { Permissions } = require("discord.js"); |
| 3 | + |
| 4 | +const is_valid_course = (course) => { |
| 5 | + const reg_comp_course = /^comp\d{4}$/; |
| 6 | + const reg_math_course = /^math\d{4}$/; |
| 7 | + const reg_binf_course = /^binf\d{4}$/; |
| 8 | + const reg_engg_course = /^engg\d{4}$/; |
| 9 | + const reg_seng_course = /^seng\d{4}$/; |
| 10 | + const reg_desn_course = /^desn\d{4}$/; |
| 11 | + return ( |
| 12 | + reg_comp_course.test(course.toLowerCase()) || |
| 13 | + reg_math_course.test(course.toLowerCase()) || |
| 14 | + reg_binf_course.test(course.toLowerCase()) || |
| 15 | + reg_engg_course.test(course.toLowerCase()) || |
| 16 | + reg_seng_course.test(course.toLowerCase()) || |
| 17 | + reg_desn_course.test(course.toLowerCase()) |
| 18 | + ); |
| 19 | +}; |
| 20 | + |
| 21 | +function editChannels(interaction, channels, role) { |
| 22 | + channels.forEach((channel) => { |
| 23 | + if ( |
| 24 | + channel.type === "GUILD_TEXT" && |
| 25 | + channel.name.toLowerCase() === role.name.toLowerCase() |
| 26 | + ) { |
| 27 | + // Remove all permissions from a role |
| 28 | + role.setPermissions(0n) |
| 29 | + .then((updated) => |
| 30 | + console.log(`Updated permissions to ${updated.permissions.bitfield}`), |
| 31 | + ) |
| 32 | + .catch(console.error); |
| 33 | + // Set the permissions of the role |
| 34 | + // Add the member to the channel's permission overwrites |
| 35 | + channel.permissionOverwrites.create(role, { |
| 36 | + VIEW_CHANNEL: true, |
| 37 | + SEND_MESSAGES: true, |
| 38 | + }); |
| 39 | + console.log(channel.name, role.name); |
| 40 | + } |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +function editRoles(interaction, roles) { |
| 45 | + roles.forEach((role) => { |
| 46 | + if (is_valid_course(role.name)) { |
| 47 | + interaction.guild.channels |
| 48 | + .fetch() |
| 49 | + .then( |
| 50 | + (channels) => ( |
| 51 | + editChannels(interaction, channels, role), |
| 52 | + console.log(`There are ${channels.size} channels.`) |
| 53 | + ), |
| 54 | + ) |
| 55 | + .catch(console.error); |
| 56 | + } |
| 57 | + }); |
| 58 | + interaction.reply({ |
| 59 | + content: `✅ | found course chats and matching roles, cleared and set permission overwrites for roles.`, |
| 60 | + ephemeral: true, |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +module.exports = { |
| 65 | + data: new SlashCommandBuilder() |
| 66 | + .setName("rolespermoverride") |
| 67 | + .setDescription( |
| 68 | + "Looks for matches between roles and course chats and attaches permissions.", |
| 69 | + ), |
| 70 | + async execute(interaction) { |
| 71 | + try { |
| 72 | + if (!interaction.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) { |
| 73 | + return await interaction.reply({ |
| 74 | + content: "You do not have permission to execute this command.", |
| 75 | + ephemeral: true, |
| 76 | + }); |
| 77 | + } |
| 78 | + // for all roles with name == chat name involving 4 letter prefix comp, seng, engg or binf, |
| 79 | + |
| 80 | + // give the role the permission override to participate in the matching channel. |
| 81 | + interaction.guild.roles |
| 82 | + .fetch() |
| 83 | + .then( |
| 84 | + (roles) => ( |
| 85 | + editRoles(interaction, roles), console.log(`There are ${roles.size} roles.`) |
| 86 | + ), |
| 87 | + ) |
| 88 | + .catch(console.error); |
| 89 | + } catch (error) { |
| 90 | + await interaction.reply("Error: " + error); |
| 91 | + } |
| 92 | + }, |
| 93 | +}; |
0 commit comments