Skip to content

Commit

Permalink
Make profile command functional for the error case
Browse files Browse the repository at this point in the history
  • Loading branch information
arnavb committed Jul 18, 2024
1 parent 3fa8345 commit 0c40c58
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
26 changes: 15 additions & 11 deletions JimBroBot/Data.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@ let readExerciseLogEntriesForBotUser (connectionString: string) (botUserId: int)
LogDate = read.dateOnly "log_date" })


let loadDataForUser (connectionString: string, discordId: string) =
let loadDataForUser (connectionString: string) (discordId: string) =
task {
let! botUser = readBotUser connectionString discordId
let! botUserExercises = readExercisesForBotUser connectionString botUser.Id

let exerciseInfos = botUserExercises |> Seq.map databaseExerciseToDomainExerciseInfo

// TODO: Figure out way to load and combine exercise log
return
{ Id = discordId
Exercises = exerciseInfos
ExerciseLog = Seq.empty }
try
let! botUser = readBotUser connectionString discordId
let! botUserExercises = readExercisesForBotUser connectionString botUser.Id

let exerciseInfos = botUserExercises |> Seq.map databaseExerciseToDomainExerciseInfo

// TODO: Figure out way to load and combine exercise log
return
Ok
{ Id = discordId
Exercises = exerciseInfos
ExerciseLog = Seq.empty }
with NoResultsException(_) ->
return Error NoUserFound
}
1 change: 1 addition & 0 deletions JimBroBot/DomainTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type BotError =
| NoEnvBotToken
| BotStartError of string
| MissingCommandHandler
| NoUserFound

type ExerciseType =
| SetBased
Expand Down
33 changes: 26 additions & 7 deletions JimBroBot/UserCommands.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ open TimeSpanParserUtil

open JimBroBot.DomainTypes
open JimBroBot.Converters
open JimBroBot.Data

let addExerciseBuilder name =
(new SlashCommandBuilder())
Expand Down Expand Up @@ -250,14 +251,32 @@ let profileResponder (command: SocketSlashCommand) connectionString =
task {
do! command.DeferAsync()

let user = command.User
let botUserId = command.User.Id |> string

let profileEmbed =
(EmbedBuilder())
.WithTitle($"{user.Mention}'s Profile")
.WithDescription("A summary of various stats")
let! user = loadDataForUser connectionString botUserId

let! _ = command.FollowupAsync(embed = profileEmbed.Build())
match user with
| Ok user ->
let profileEmbed =
(EmbedBuilder())
.WithTitle($"{command.User.GlobalName}'s Profile")
.WithDescription("A summary of various stats")
.AddField(
(EmbedFieldBuilder())
.WithIsInline(true)
.WithName("Exercises")
.WithValue(user.Exercises |> Seq.length)
)

return ()
let! _ = command.FollowupAsync(embed = profileEmbed.Build())

return ()
| Error(NoUserFound) ->
let! _ = command.FollowupAsync "User has no bot account!"

return ()
| Error(_) ->
let! _ = command.FollowupAsync "Unexpected error; try again later."

return ()
}

0 comments on commit 0c40c58

Please sign in to comment.