Skip to content

Commit

Permalink
Merge pull request #213 from eendjebernard/master
Browse files Browse the repository at this point in the history
feat: Added profile lookup using a Udemy profile URL
  • Loading branch information
joshbker authored Nov 3, 2023
2 parents bb0b555 + 776c3b9 commit e887720
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
55 changes: 41 additions & 14 deletions src/main/kotlin/com/learnspigot/bot/profile/ProfileCommand.kt
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import java.util.*
class ProfileRegistry {

val profileCache: MutableMap<String, Profile> = TreeMap(String.CASE_INSENSITIVE_ORDER)
private val urlProfiles: MutableMap<String, Profile> = TreeMap()

val contributorSelectorCache: MutableMap<String, List<String>> = HashMap()
val messagesToRemove: MutableMap<String, Message> = HashMap()
Expand All @@ -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
}
}
}
Expand Down Expand Up @@ -68,4 +71,7 @@ class ProfileRegistry {
}
}

fun findByURL(udemyURL: String): Profile? {
return urlProfiles[udemyURL]
}
}

0 comments on commit e887720

Please sign in to comment.