From ee40e2462a0051251acfd87d7efdcecc9baa6ba4 Mon Sep 17 00:00:00 2001 From: Jack Li Date: Sat, 14 Jan 2023 00:48:58 +0800 Subject: [PATCH 1/3] I can finally sleep now --- commands/course.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/commands/course.js b/commands/course.js index c8e23a91..52b974a9 100644 --- a/commands/course.js +++ b/commands/course.js @@ -37,6 +37,12 @@ const get_real_course_name = (course) => { }; const is_valid_course = (course) => { + const reg_valid_course = /^[a-zA-Z]{4}\d{4}$/; + + return reg_valid_course.test(course); +}; + +const is_supported_course = (course) => { const reg_comp_course = /^comp\d{4}$/; const reg_math_course = /^math\d{4}$/; const reg_binf_course = /^binf\d{4}$/; @@ -86,17 +92,14 @@ module.exports = { const input_course = await interaction.options.getString("course"); const course = get_real_course_name(input_course); - const other_courses = /^[a-zA-Z]{4}\d{4}$/; - const is_valid = is_valid_course(course); - - if (!is_valid && other_courses.test(course.toLowerCase())) { + if (!is_valid_course(course)) { return await interaction.reply({ - content: `❌ | Course chats for other faculties are not supported.`, + content: `❌ | You are not allowed to join this channel using this command.`, ephemeral: true, }); - } else if (!is_valid) { + } else if (!is_supported_course(course)) { return await interaction.reply({ - content: `❌ | You are not allowed to join this channel using this command.`, + content: `❌ | Course chats for other faculties are not supported.`, ephemeral: true, }); } @@ -158,7 +161,7 @@ module.exports = { const input_course = await interaction.options.getString("course"); const course = get_real_course_name(input_course); - if (!is_valid_course(course)) { + if (!is_supported_course(course)) { return await interaction.reply({ content: `❌ | You are not allowed to leave this channel using this command.`, ephemeral: true, From 9b30dbf4e32b6cfc94ebd0ab4f919652a64521e6 Mon Sep 17 00:00:00 2001 From: Wolfdragon24 Date: Wed, 15 May 2024 15:12:24 +1000 Subject: [PATCH 2/3] Implementation of suggested modifications --- commands/course.js | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/commands/course.js b/commands/course.js index 36da0582..aa27da60 100644 --- a/commands/course.js +++ b/commands/course.js @@ -41,20 +41,20 @@ const is_valid_course = (course) => { }; const is_supported_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}$/; + const reg_comp_course = /^(?i)comp\d{4}$/; + const reg_math_course = /^(?i)math\d{4}$/; + const reg_binf_course = /^(?i)binf\d{4}$/; + const reg_engg_course = /^(?i)engg\d{4}$/; + const reg_seng_course = /^(?i)seng\d{4}$/; + const reg_desn_course = /^(?i)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()) + reg_comp_course.test(course) || + reg_math_course.test(course) || + reg_binf_course.test(course) || + reg_engg_course.test(course) || + reg_seng_course.test(course) || + reg_desn_course.test(course) ); }; @@ -93,29 +93,24 @@ module.exports = { const input_course = await interaction.options.getString("course").toLowerCase(); const course = get_real_course_name(input_course); - const other_courses = /^[a-zA-Z]{4}\d{4}$/; const is_valid = is_valid_course(course); + const is_supported = is_supported_course(course); const course_with_alias = course != input_course ? `${course} (same course chat as ${input_course})` : `${course}`; - if (!is_valid && other_courses.test(course.toLowerCase())) { + if (!is_supported && is_valid) { return await interaction.reply({ content: `❌ | Course chats for other faculties are not supported.`, ephemeral: true, }); - } else if (!is_valid) { + } else if (!is_supported) { return await interaction.reply({ content: `❌ | You are not allowed to join this channel using this command.`, ephemeral: true, }); - } else if (!is_supported_course(course)) { - return await interaction.reply({ - content: `❌ | Course chats for other faculties are not supported.`, - ephemeral: true, - }); } // First, let's see if there's a role that matches the name of the course From ad22eb9770a9a9fe1fe91b5427ead24978c96272 Mon Sep 17 00:00:00 2001 From: Wolfdragon24 Date: Thu, 16 May 2024 21:13:10 +1000 Subject: [PATCH 3/3] Modification to ensure correct regex form for case-insensitive match --- commands/course.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/commands/course.js b/commands/course.js index aa27da60..07428e13 100644 --- a/commands/course.js +++ b/commands/course.js @@ -1,4 +1,5 @@ const { SlashCommandBuilder } = require("@discordjs/builders"); +const { ChannelType } = require("discord.js"); const COMMAND_JOIN = "join"; const COMMAND_LEAVE = "leave"; @@ -41,12 +42,12 @@ const is_valid_course = (course) => { }; const is_supported_course = (course) => { - const reg_comp_course = /^(?i)comp\d{4}$/; - const reg_math_course = /^(?i)math\d{4}$/; - const reg_binf_course = /^(?i)binf\d{4}$/; - const reg_engg_course = /^(?i)engg\d{4}$/; - const reg_seng_course = /^(?i)seng\d{4}$/; - const reg_desn_course = /^(?i)desn\d{4}$/; + const reg_comp_course = /^comp\d{4}$/i; + const reg_math_course = /^math\d{4}$/i; + const reg_binf_course = /^binf\d{4}$/i; + const reg_engg_course = /^engg\d{4}$/i; + const reg_seng_course = /^seng\d{4}$/i; + const reg_desn_course = /^desn\d{4}$/i; return ( reg_comp_course.test(course) || @@ -161,7 +162,7 @@ module.exports = { content: `❌ | The course chat for \`${course}\` does not exist.`, ephemeral: true, }); - } else if (channel.type !== "GUILD_TEXT") { + } else if (channel.type !== ChannelType.GuildText) { return await interaction.reply({ content: `❌ | The course chat for \`${course}\` is not a text channel.`, ephemeral: true,