diff --git a/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt b/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt index a068850..27d0d23 100644 --- a/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt +++ b/src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt @@ -1,11 +1,14 @@ 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 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 @@ -21,21 +24,45 @@ 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) - - event.replyEmbeds( - embed() - .setTitle("Profile Lookup") - .addField("Discord", finalUser.name + " (" + finalUser.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) - .build() - ).setEphemeral(true).queue() + val profileByURL = profileRegistry.findByURL(url ?: "") + + val embed: MessageEmbed = when { + 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() + } + + private fun userProfileEmbed( + user: User + ): MessageEmbed { + val profile = profileRegistry.findByUser(user) + + 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() } + private fun createProfileLookupEmbed( + title: String, + description: String) + : MessageEmbed = embed() + .setTitle("Profile Lookup") + .addField(title, description, false) + .build() } \ 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