Skip to content

Commit

Permalink
Merge pull request #124 from ritsec/119-save-the-message-contents-bef…
Browse files Browse the repository at this point in the history
…ore-it-gets-purged-with-purge

Add save the message contents before it gets purged with purge
  • Loading branch information
c0untingNumbers authored Jul 13, 2024
2 parents 0623689 + 82a2a63 commit dd0fa0b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
57 changes: 56 additions & 1 deletion commands/slash/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ package slash

import (
"fmt"
"strings"
"time"

"github.com/bwmarrin/discordgo"
"github.com/ritsec/ops-bot-iii/commands/slash/permission"
"github.com/ritsec/ops-bot-iii/config"
"github.com/ritsec/ops-bot-iii/logging"
"github.com/sirupsen/logrus"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

var (
// Channel ID of the purge logs channel
purgeLogsChannel string = config.GetString("commands.purge.channel_id")
)

// Purge is the purge command
func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *discordgo.InteractionCreate)) {
min := float64(1)
Expand Down Expand Up @@ -40,8 +48,15 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco
var (
message_ids []string
messages int64
timeloc *time.Location
)

timeloc, err := time.LoadLocation("EST")

if err != nil {
logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err})
}

if len(i.ApplicationCommandData().Options) != 0 {
messages = i.ApplicationCommandData().Options[0].IntValue()
} else {
Expand All @@ -53,7 +68,27 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco
logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err})
}

for _, message := range raw_messages {
file := fmt.Sprintf("Record of the purge on %v\n", time.Now().In(timeloc).Format("2006-01-02 15:04:05"))
file += "Purged " + fmt.Sprint(len(raw_messages)) + " messages!\n"
file += "------------------------------------------------"

// For the file
// reverses the list of messages to make the file from oldest to newest
reversedMessages := make([]*discordgo.Message, len(raw_messages))
for j, message := range raw_messages {
reversedMessages[len(raw_messages)-1-j] = message
}

for _, message := range reversedMessages {
// Check to see if message is edited
if message.EditedTimestamp == nil {
file += fmt.Sprintf("\n\n%v SENT AT %v", message.Author, message.Timestamp.In(timeloc).Format("2006-01-02 15:04:05"))
file += fmt.Sprintf("\n%v", message.Content)
} else {
file += fmt.Sprintf("\n\n%v SENT AT %v (EDITED AT %v)", message.Author, message.Timestamp.In(timeloc).Format("2006-01-02 15:04:05"), message.EditedTimestamp.In(timeloc).Format("2006-01-02 15:04:05"))
file += fmt.Sprintf("\n%v", message.Content)
}

message_ids = append(message_ids, message.ID)
}

Expand All @@ -74,5 +109,25 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco
if err != nil {
logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err})
}

// Putting this here so that it does not send the file if the ChannelMessagesBulkDelete fails for some reason

con := fmt.Sprintf("PURGE INITIATED AT %v\n", time.Now().In(timeloc).Format("2006-01-02 15:04:05"))
con += "Purged " + fmt.Sprint(len(raw_messages)) + " messages!"

_, err = s.ChannelMessageSendComplex(purgeLogsChannel, &discordgo.MessageSend{
Content: con,
Files: []*discordgo.File{
{
Name: "purge.txt",
ContentType: "text",
Reader: strings.NewReader(file),
},
},
})
if err != nil {
logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err})
return
}
}
}
2 changes: 2 additions & 0 deletions config_example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ commands:
prospective_role_id:
staff_role_id:
alumni_role_id:
purge:
channel_id:
vote:
url: http(s)://example.com(:port)

0 comments on commit dd0fa0b

Please sign in to comment.