Skip to content

Commit

Permalink
Add relative durations for video log
Browse files Browse the repository at this point in the history
  • Loading branch information
xoltia committed Nov 14, 2023
1 parent c8c0d56 commit 7960f06
Showing 1 changed file with 74 additions and 3 deletions.
77 changes: 74 additions & 3 deletions internal/bot/commands/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ var videoCommandOptions = []*discordgo.ApplicationCommandOption{
Required: false,
Options: []*discordgo.ApplicationCommandOption{},
},
{
Name: "complex-duration",
Type: discordgo.ApplicationCommandOptionString,
Description: "Duration spent on the activity",
Required: false,
Options: []*discordgo.ApplicationCommandOption{},
},
}

var vnCommandOptions = []*discordgo.ApplicationCommandOption{
Expand Down Expand Up @@ -753,12 +760,24 @@ func (c *LogCommand) handleVideo(ctx *bot.InteractionContext, subcommand *discor
return err
}

if discordutil.GetUintOption(args, "duration") != nil {
durationOption, err := discordutil.GetRequiredUintOption(args, "duration")
if durationMinutes := discordutil.GetUintOption(args, "duration"); durationMinutes != nil {
activity.Duration = time.Duration(*durationMinutes) * time.Minute
} else if complexDuration := discordutil.GetStringOption(args, "complex-duration"); complexDuration != nil {
activity.Duration, err = parseDurationComplex(*complexDuration, video.Duration)

if err != nil {
_, err = ctx.Followup(&discordgo.WebhookParams{
Content: fmt.Sprintf("Invalid duration provided: %s", err.Error()),
}, false)
return err
}

if activity.Duration < 0 {
_, err = ctx.Followup(&discordgo.WebhookParams{
Content: fmt.Sprintf("Expected positive duration, got %s", activity.Duration.String()),
}, false)
return err
}
activity.Duration = time.Duration(durationOption) * time.Minute
} else {
activity.Duration = video.Duration
}
Expand Down Expand Up @@ -1039,6 +1058,58 @@ func (c *LogCommand) parseDateOption(
return
}

// Returns difference between two durations
// Should be in one of the following formats:
// duration1:duration2
// (:?)duration2 (duration1 is inferred to be 0)
// duration1: (duration2 is inferred to be upper)
func parseDurationComplex(str string, upper time.Duration) (d time.Duration, err error) {
parts := strings.Split(str, ":")

// duration2
if len(parts) == 1 {
d, err = time.ParseDuration(parts[0])
return
}

if len(parts) != 2 {
err = errors.New("invalid duration")
return
}

if parts[0] == "" && parts[1] == "" {
d = upper
return
}

// :duration2
if parts[0] == "" {
d, err = time.ParseDuration(parts[1])
return
}

// duration1:
if parts[1] == "" {
d, err = time.ParseDuration(parts[0])
d = upper - d
return
}

// duration1:duration2
d1, err := time.ParseDuration(parts[0])
if err != nil {
return
}

d2, err := time.ParseDuration(parts[1])
if err != nil {
return
}

d = d2 - d1
return
}

func getNamedSources(sources []string) map[string]string {
result := make(map[string]string)
for _, source := range sources {
Expand Down

0 comments on commit 7960f06

Please sign in to comment.