From e70db7c6beed674c460c4ad0c7ed63daf4cd02fa Mon Sep 17 00:00:00 2001 From: Bernard Date: Thu, 2 Nov 2023 17:57:46 +0100 Subject: [PATCH 1/5] feat: Added profile lookup using a udemy profile URL --- .../learnspigot/bot/profile/ProfileCommand.kt | 76 +++++++++++++++++-- .../bot/profile/ProfileRegistry.kt | 6 ++ 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt b/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt index a068850..c7f2e52 100644 --- a/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt +++ b/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt @@ -1,5 +1,6 @@ package com.learnspigot.bot.profile +import com.learnspigot.bot.Bot import com.learnspigot.bot.util.embed import gg.flyte.neptune.annotation.Command import gg.flyte.neptune.annotation.Description @@ -21,21 +22,84 @@ class ProfileCommand { ) fun onProfileCommand( event: SlashCommandInteractionEvent, - @Description("User to show profile") @Optional user: User? + @Description("User to show profile") @Optional user: User?, + @Description("URL to show profile") @Optional url: String? ) { - val finalUser = user ?: event.user - val profile = profileRegistry.findByUser(finalUser) + val profileByURL = profileRegistry.findByURL(url ?: "") + + if (user != null && url != null) { + event.replyEmbeds( + embed() + .setTitle("Profile Lookup") + .addField( + "Please choose", + "Please make a choice whether you want to use a user or use a URL.", + false) + .build() + ).setEphemeral(true).queue() + return + } + + if (user == null && url == null) { + showUserProfile(event, event.user) + return + } + + if (profileByURL != null) { + val urlUser = Bot.jda.getUserById(profileByURL.id) + + if (urlUser == null) { + event.replyEmbeds( + embed() + .setTitle("Profile Lookup") + .addField( + "No discord user found", + "No discord user was found using this udemy URL. This is an issue on our end, " + + "please contact a manager (or higher) to solve your issue.", + false) + .build() + ).setEphemeral(true).queue() + return + } + + showUserProfile(event, urlUser) + return + } + + if (user != null) { + showUserProfile(event, user) + return + } + + if (profileByURL == null) { + event.replyEmbeds( + embed() + .setTitle("Profile Lookup") + .addField( + "Invalid URL", + "An invalid udemy URL has been provided, so no profile could be found.", + false) + .build() + ).setEphemeral(true).queue() + return + } + } + + private fun showUserProfile( + event: SlashCommandInteractionEvent, + user: User + ) { + val profile = profileRegistry.findByUser(user) event.replyEmbeds( embed() .setTitle("Profile Lookup") - .addField("Discord", finalUser.name + " (" + finalUser.asMention + ")", false) + .addField("Discord", user.name + " (" + user.asMention + ")", false) .addField("Udemy", profile.udemyProfileUrl ?: "Not linked", false) .addField("Reputation", profile.reputation.size.toString(), true) .addField("(Notifications)", profile.notifyOnRep.toString(), true) - .setThumbnail(finalUser.effectiveAvatarUrl) + .setThumbnail(user.effectiveAvatarUrl) .build() ).setEphemeral(true).queue() } - } \ No newline at end of file diff --git a/src/main/kotlin/com/learnspigot/bot/profile/ProfileRegistry.kt b/src/main/kotlin/com/learnspigot/bot/profile/ProfileRegistry.kt index bcf8f57..c1e6b50 100644 --- a/src/main/kotlin/com/learnspigot/bot/profile/ProfileRegistry.kt +++ b/src/main/kotlin/com/learnspigot/bot/profile/ProfileRegistry.kt @@ -11,6 +11,7 @@ import java.util.* class ProfileRegistry { val profileCache: MutableMap = TreeMap(String.CASE_INSENSITIVE_ORDER) + private val urlProfiles: MutableMap = TreeMap() val contributorSelectorCache: MutableMap> = HashMap() val messagesToRemove: MutableMap = HashMap() @@ -34,6 +35,8 @@ class ProfileRegistry { document.getBoolean("notifyOnRep") ?: true, document.getBoolean("intellijKeyGiven") ?: false).let { profileCache[it.id] = it + if (it.udemyProfileUrl != null) + urlProfiles[it.udemyProfileUrl!!] = it } } } @@ -68,4 +71,7 @@ class ProfileRegistry { } } + fun findByURL(udemyURL: String): Profile? { + return urlProfiles[udemyURL] + } } \ No newline at end of file From ee58872a6a4d779d4eab2b098bcc6e28d647179d Mon Sep 17 00:00:00 2001 From: Bernard Date: Fri, 3 Nov 2023 08:38:13 +0100 Subject: [PATCH 2/5] style: Improved ProfileCommand code to contain less if conditions --- .../learnspigot/bot/profile/ProfileCommand.kt | 85 +++++++------------ 1 file changed, 31 insertions(+), 54 deletions(-) diff --git a/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt b/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt index c7f2e52..3971f53 100644 --- a/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt +++ b/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt @@ -6,7 +6,9 @@ import gg.flyte.neptune.annotation.Command import gg.flyte.neptune.annotation.Description import gg.flyte.neptune.annotation.Inject import gg.flyte.neptune.annotation.Optional +import net.dv8tion.jda.api.EmbedBuilder import net.dv8tion.jda.api.Permission +import net.dv8tion.jda.api.entities.MessageEmbed import net.dv8tion.jda.api.entities.User import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent @@ -27,8 +29,11 @@ class ProfileCommand { ) { val profileByURL = profileRegistry.findByURL(url ?: "") - if (user != null && url != null) { - event.replyEmbeds( + val embed: MessageEmbed = when { + user == null && url == null -> { + userProfileEmbed(event, event.user) + } + user != null && url != null -> { embed() .setTitle("Profile Lookup") .addField( @@ -36,70 +41,42 @@ class ProfileCommand { "Please make a choice whether you want to use a user or use a URL.", false) .build() - ).setEphemeral(true).queue() - return - } - - if (user == null && url == null) { - showUserProfile(event, event.user) - return - } - - if (profileByURL != null) { - val urlUser = Bot.jda.getUserById(profileByURL.id) - - if (urlUser == null) { - event.replyEmbeds( - embed() - .setTitle("Profile Lookup") - .addField( - "No discord user found", - "No discord user was found using this udemy URL. This is an issue on our end, " + - "please contact a manager (or higher) to solve your issue.", - false) - .build() - ).setEphemeral(true).queue() - return } - - showUserProfile(event, urlUser) - return - } - - if (user != null) { - showUserProfile(event, user) - return - } - - if (profileByURL == null) { - event.replyEmbeds( + profileByURL != null -> { + val urlUser = Bot.jda.getUserById(profileByURL.id) + userProfileEmbed(event, urlUser!!) + } + user != null -> { + userProfileEmbed(event, user) + } + else -> { embed() .setTitle("Profile Lookup") .addField( - "Invalid URL", - "An invalid udemy URL has been provided, so no profile could be found.", + "Something went wrong", + "Something went wrong while trying to find a profile matching your query. Please " + + "contact a manager (or higher) to look at this issue.", false) .build() - ).setEphemeral(true).queue() - return + } } + + event.replyEmbeds(embed).setEphemeral(true).queue() } - private fun showUserProfile( + private fun userProfileEmbed( event: SlashCommandInteractionEvent, user: User - ) { + ): MessageEmbed { val profile = profileRegistry.findByUser(user) - event.replyEmbeds( - embed() - .setTitle("Profile Lookup") - .addField("Discord", user.name + " (" + user.asMention + ")", false) - .addField("Udemy", profile.udemyProfileUrl ?: "Not linked", false) - .addField("Reputation", profile.reputation.size.toString(), true) - .addField("(Notifications)", profile.notifyOnRep.toString(), true) - .setThumbnail(user.effectiveAvatarUrl) - .build() - ).setEphemeral(true).queue() + return embed() + .setTitle("Profile Lookup") + .addField("Discord", user.name + " (" + user.asMention + ")", false) + .addField("Udemy", profile.udemyProfileUrl ?: "Not linked", false) + .addField("Reputation", profile.reputation.size.toString(), true) + .addField("(Notifications)", profile.notifyOnRep.toString(), true) + .setThumbnail(user.effectiveAvatarUrl) + .build() } } \ No newline at end of file From f8185a048e1a72ac732710b89ee98dcc1ea4f98d Mon Sep 17 00:00:00 2001 From: Bernard Date: Fri, 3 Nov 2023 08:45:03 +0100 Subject: [PATCH 3/5] style: Remove unused parameter for method 'userProfileEmbed' --- .../kotlin/com/learnspigot/bot/profile/ProfileCommand.kt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt b/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt index 3971f53..e6929ab 100644 --- a/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt +++ b/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt @@ -31,7 +31,7 @@ class ProfileCommand { val embed: MessageEmbed = when { user == null && url == null -> { - userProfileEmbed(event, event.user) + userProfileEmbed(event.user) } user != null && url != null -> { embed() @@ -44,10 +44,10 @@ class ProfileCommand { } profileByURL != null -> { val urlUser = Bot.jda.getUserById(profileByURL.id) - userProfileEmbed(event, urlUser!!) + userProfileEmbed(urlUser!!) } user != null -> { - userProfileEmbed(event, user) + userProfileEmbed(user) } else -> { embed() @@ -65,7 +65,6 @@ class ProfileCommand { } private fun userProfileEmbed( - event: SlashCommandInteractionEvent, user: User ): MessageEmbed { val profile = profileRegistry.findByUser(user) From 8e1487048d2861a08fa99cdcebfd79ca40ed0ccd Mon Sep 17 00:00:00 2001 From: Bernard Date: Fri, 3 Nov 2023 10:24:16 +0100 Subject: [PATCH 4/5] style: Improve 'when' condition to only contain one-liners and make the code less heavy by turning all embed creations into a single method --- .../learnspigot/bot/profile/ProfileCommand.kt | 45 +++++++------------ 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt b/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt index e6929ab..f270d37 100644 --- a/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt +++ b/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt @@ -30,35 +30,14 @@ class ProfileCommand { val profileByURL = profileRegistry.findByURL(url ?: "") val embed: MessageEmbed = when { - user == null && url == null -> { - userProfileEmbed(event.user) - } - user != null && url != null -> { - embed() - .setTitle("Profile Lookup") - .addField( - "Please choose", - "Please make a choice whether you want to use a user or use a URL.", - false) - .build() - } - profileByURL != null -> { - val urlUser = Bot.jda.getUserById(profileByURL.id) - userProfileEmbed(urlUser!!) - } - user != null -> { - userProfileEmbed(user) - } - else -> { - embed() - .setTitle("Profile Lookup") - .addField( - "Something went wrong", - "Something went wrong while trying to find a profile matching your query. Please " + - "contact a manager (or higher) to look at this issue.", - false) - .build() - } + user == null && url == null -> userProfileEmbed(event.user) + user != null && url != null -> createProfileLookupEmbed("Please choose", "Please make a " + + "choice whether you want to use a user or use a URL.") + profileByURL != null -> userProfileEmbed(Bot.jda.getUserById(profileByURL.id)!!) + user != null -> userProfileEmbed(user) + else -> createProfileLookupEmbed("Something went wrong", "Something went wrong while " + + "trying to find a profile matching your query. Please contact a manager (or higher) to look at " + + "this issue.") } event.replyEmbeds(embed).setEphemeral(true).queue() @@ -78,4 +57,12 @@ class ProfileCommand { .setThumbnail(user.effectiveAvatarUrl) .build() } + + fun createProfileLookupEmbed( + title: String, + description: String) + : MessageEmbed = embed() + .setTitle("Profile Lookup") + .addField(title, description, false) + .build() } \ No newline at end of file From 776c3b9dd8faab4e24fe5575e80708912380c7f6 Mon Sep 17 00:00:00 2001 From: Bernard Date: Fri, 3 Nov 2023 10:26:26 +0100 Subject: [PATCH 5/5] style: Make 'createProfileLookupEmbed' private --- src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt b/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt index f270d37..27d0d23 100644 --- a/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt +++ b/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt @@ -58,7 +58,7 @@ class ProfileCommand { .build() } - fun createProfileLookupEmbed( + private fun createProfileLookupEmbed( title: String, description: String) : MessageEmbed = embed()