diff --git a/commands/course.js b/commands/course.js index 15d87caa..b55544c1 100644 --- a/commands/course.js +++ b/commands/course.js @@ -128,52 +128,8 @@ module.exports = { }); } - // Otherwise, find a channel with the same name as the course - const channel = await interaction.guild.channels.cache.find( - (c) => c.name.toLowerCase() === course.toLowerCase(), - ); - - // Make sure that the channel exists, and is a text channel - if (channel === undefined) { - return await interaction.reply({ - content: `❌ | The course chat for \`${course}\` does not exist. If you'd like for it to be created, please raise a ticket in <#${MODERATION_REQUEST_CHANNEL}>.`, - ephemeral: true, - }); - } else if (channel.type !== "GUILD_TEXT") { - return await interaction.reply({ - content: `❌ | The course chat for \`${course}\` is not a text channel.`, - ephemeral: true, - }); - } - - const permissions = new Permissions( - channel.permissionsFor(interaction.user.id).bitfield, - ); - - // Check if the member already has an entry in the channel's permission overwrites, and update - // the entry if they do just to make sure that they have the correct permissions - if ( - permissions.has([ - Permissions.FLAGS.VIEW_CHANNEL, - Permissions.FLAGS.SEND_MESSAGES, - ]) - ) { - await channel.permissionOverwrites.edit(interaction.member, { - VIEW_CHANNEL: true, - }); - return await interaction.reply({ - content: `❌ | You are already in the course chat for \`${course_with_alias}\`.`, - ephemeral: true, - }); - } - - // Add the member to the channel's permission overwrites - await channel.permissionOverwrites.create(interaction.member, { - VIEW_CHANNEL: true, - }); - return await interaction.reply({ - content: `✅ | Added you to the chat for ${course_with_alias}.`, + content: `✅ | End of command - ${course_with_alias}.`, ephemeral: true, }); } else if (interaction.options.getSubcommand() === COMMAND_LEAVE) { @@ -182,7 +138,7 @@ module.exports = { if (!is_valid_course(course)) { return await interaction.reply({ - content: `❌ | You are not allowed to leave this channel using this command.`, + content: `❌ | Not a valid course.`, ephemeral: true, }); } @@ -196,7 +152,7 @@ module.exports = { if (role !== undefined) { if (!interaction.member.roles.cache.has(role.id)) { return await interaction.reply({ - content: `❌ | You are not in the course chat for \`${course}\`.`, + content: `❌ | You do not have the role for \`${course}\`.`, ephemeral: true, }); } @@ -204,55 +160,11 @@ module.exports = { // If they do, let's remove the role from them await interaction.member.roles.remove(role); return await interaction.reply({ - content: `✅ | Removed you from the chat for \`${course}\`.`, + content: `✅ | Removed you from the role and chat for \`${course}\`.`, ephemeral: true, }); } - - // Find a channel with the same name as the course - const channel = await interaction.guild.channels.cache.find( - (c) => c.name.toLowerCase() === course.toLowerCase(), - ); - - // Otherwise, make sure that the channel exists, and is a text channel - if (channel === undefined) { - return await interaction.reply({ - content: `❌ | The course chat for \`${course}\` does not exist.`, - ephemeral: true, - }); - } else if (channel.type !== "GUILD_TEXT") { - return await interaction.reply({ - content: `❌ | The course chat for \`${course}\` is not a text channel.`, - ephemeral: true, - }); - } - - const permissions = new Permissions( - channel.permissionsFor(interaction.user.id).bitfield, - ); - - // Check if the member already has an entry in the channel's permission overwrites - if ( - !permissions.has([ - Permissions.FLAGS.VIEW_CHANNEL, - Permissions.FLAGS.SEND_MESSAGES, - ]) - ) { - return await interaction.reply({ - content: `❌ | You are not in the course chat for \`${course}\`.`, - ephemeral: true, - }); - } - - // Remove the member from the channel's permission overwrites - await channel.permissionOverwrites.delete(interaction.member); - - return await interaction.reply({ - content: `✅ | Removed you from the course chat for \`${course}\`.`, - ephemeral: true, - }); } - return await interaction.reply("Error: invalid subcommand."); } catch (error) { await interaction.reply("Error: " + error); diff --git a/commands/rolesPermOverride.js b/commands/rolesPermOverride.js new file mode 100644 index 00000000..21e05e75 --- /dev/null +++ b/commands/rolesPermOverride.js @@ -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); + } + }, +};