Skip to content

Commit

Permalink
Simplify message age constraints by using a static unit of time
Browse files Browse the repository at this point in the history
  • Loading branch information
cedws committed May 9, 2021
1 parent 922861b commit b430e9f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 61 deletions.
40 changes: 4 additions & 36 deletions client/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"errors"
log "github.com/sirupsen/logrus"
"strconv"
"time"
)

Expand All @@ -21,13 +20,8 @@ func (c *Client) SetSkipChannels(skipChannels []string) {
c.skipChannels = skipChannels
}

func (c *Client) SetMinAge(minAge string) error {
dur, err := parseDuration(minAge)
if err != nil {
return err
}

t := time.Now().Add(-dur)
func (c *Client) SetMinAge(minAge uint) error {
t := time.Now().Add(-time.Duration(minAge) * day)
millis := t.UnixNano() / int64(time.Millisecond)

c.maxID = toSnowflake(millis)
Expand All @@ -36,38 +30,12 @@ func (c *Client) SetMinAge(minAge string) error {
return nil
}

func (c *Client) SetMaxAge(maxAge string) error {
dur, err := parseDuration(maxAge)
if err != nil {
return err
}

t := time.Now().Add(-dur)
func (c *Client) SetMaxAge(maxAge uint) error {
t := time.Now().Add(-time.Duration(maxAge) * day)
millis := t.UnixNano() / int64(time.Millisecond)

c.minID = toSnowflake(millis)
log.Debugf("Message minimum ID must be %v", c.minID)

return nil
}

func parseDuration(duration string) (time.Duration, error) {
if len(duration) == 0 {
return 0, ErrorInvalidDuration
}

last := duration[len(duration)-1:]
mult, err := strconv.Atoi(duration[:len(duration)-1])
if err != nil {
return 0, ErrorInvalidDuration
}

switch last {
case "d":
return time.Duration(mult) * day, nil
default:
return time.ParseDuration(duration)
}

return 0, nil
}
17 changes: 0 additions & 17 deletions client/args_test.go

This file was deleted.

16 changes: 8 additions & 8 deletions cmd/partial.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

var (
dryrun bool
minAge string
maxAge string
minAge uint
maxAge uint
skipChannels []string
)

Expand Down Expand Up @@ -45,20 +45,20 @@ func partial(cmd *cobra.Command, args []string) {
client.SetDryRun(dryrun)
client.SetSkipChannels(skipChannels)

if minAge != "" {
if minAge > 0 {
err = client.SetMinAge(minAge)
if err != nil {
log.Fatal(err)
}
log.Infof("Deleting messages with a minimum age of %v", minAge)
log.Infof("Deleting messages with a minimum age of %v days", minAge)
}

if maxAge != "" {
if maxAge > 0 {
err = client.SetMaxAge(maxAge)
if err != nil {
log.Fatal(err)
}
log.Infof("Deleting messages with a maximum age of %v", maxAge)
log.Infof("Deleting messages with a maximum age of %v days", maxAge)
}

err = client.PartialDelete()
Expand All @@ -69,7 +69,7 @@ func partial(cmd *cobra.Command, args []string) {

func init() {
partialCmd.Flags().BoolVarP(&dryrun, "dry-run", "d", false, "perform dry run without deleting anything")
partialCmd.Flags().StringVarP(&minAge, "min-age", "i", "", "minimum age of messages to delete")
partialCmd.Flags().StringVarP(&maxAge, "max-age", "a", "", "maximum age of messages to delete")
partialCmd.Flags().UintVarP(&minAge, "min-age-days", "i", 0, "minimum age in days of messages to delete")
partialCmd.Flags().UintVarP(&maxAge, "max-age-days", "a", 0, "maximum age in days of messages to delete")
partialCmd.Flags().StringSliceVarP(&skipChannels, "skip", "s", []string{}, "skip message deletion for specified channels/guilds")
}

0 comments on commit b430e9f

Please sign in to comment.