Skip to content

Commit

Permalink
Add option to skip pinned messages (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrrfv authored Apr 23, 2022
1 parent 50e170e commit 27739e0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
13 changes: 11 additions & 2 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"discord-delete/client/spoof"
"encoding/json"
"fmt"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"net/http"
"time"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

const api = "https://discord.com/api/v8"
Expand Down Expand Up @@ -42,6 +43,7 @@ type Client struct {
maxID int64
minID int64
skipChannels []string
skipPinned bool
httpClient http.Client
}

Expand Down Expand Up @@ -204,6 +206,12 @@ func (c *Client) DeleteMessages(messages *Messages, seek *int) error {
continue
}

if c.skipPinned && msg.Pinned {
log.Infof("Found pinned message, skipping")
(*seek)++
continue
}

log.Infof("Deleting message %v from channel %v", msg.ID, msg.ChannelID)
if c.dryRun {
// Move seek index forward to simulate message deletion on server's side
Expand Down Expand Up @@ -360,6 +368,7 @@ type Message struct {
Hit bool `json:"hit,omitempty"`
ChannelID string `json:"channel_id"`
Type int `json:"type"`
Pinned bool `json:"pinned"`
}

type Messages struct {
Expand Down
7 changes: 6 additions & 1 deletion client/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package client

import (
"errors"
log "github.com/sirupsen/logrus"
"time"

log "github.com/sirupsen/logrus"
)

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

func (c *Client) SetSkipPinned(skipPinned bool) {
c.skipPinned = skipPinned
}

func (c *Client) SetMinAge(minAge uint) error {
t := time.Now().Add(-time.Duration(minAge) * day)
millis := t.UnixNano() / int64(time.Millisecond)
Expand Down
8 changes: 6 additions & 2 deletions cmd/partial.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package cmd
import (
"discord-delete/client"
"discord-delete/client/token"
"os"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)

var (
dryrun bool
skipPinned bool
minAge uint
maxAge uint
skipChannels []string
Expand Down Expand Up @@ -43,7 +45,8 @@ func partial(cmd *cobra.Command, args []string) {
client := client.New(tok)
client.SetDryRun(dryrun)
client.SetSkipChannels(skipChannels)

client.SetSkipPinned(skipPinned)

if dryrun {
log.Infof("No messages will be deleted in dry-run mode")
}
Expand Down Expand Up @@ -75,4 +78,5 @@ func init() {
partialCmd.Flags().UintVarP(&minAge, "older-than-days", "o", 0, "minimum number in days of messages to be deleted")
partialCmd.Flags().UintVarP(&maxAge, "newer-than-days", "n", 0, "maximum number in days of messages to be deleted")
partialCmd.Flags().StringSliceVarP(&skipChannels, "skip", "s", []string{}, "skip message deletion for specified channels/guilds")
partialCmd.Flags().BoolVarP(&skipPinned, "skip-pinned", "p", false, "skip message deletion for pinned messages")
}

0 comments on commit 27739e0

Please sign in to comment.