From 09d71fe79823e7408545556db7c094df8b4a2d1a Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Mon, 8 Jul 2024 22:07:23 -0400 Subject: [PATCH 01/18] save contents of messages being purged --- commands/slash/purge.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/commands/slash/purge.go b/commands/slash/purge.go index 6640376..8b77f5a 100644 --- a/commands/slash/purge.go +++ b/commands/slash/purge.go @@ -2,6 +2,8 @@ package slash import ( "fmt" + "strings" + "time" "github.com/bwmarrin/discordgo" "github.com/ritsec/ops-bot-iii/commands/slash/permission" @@ -53,8 +55,31 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) } + file := fmt.Sprintf("Record of the purge on %v", time.Now()) + file += "-------------------------------" + for _, message := range raw_messages { message_ids = append(message_ids, message.ID) + // Timestamp "may be" removed in a future API version. Too bad! + file += fmt.Sprintf("\n%v SENT AT %v (EDITED AT %v)", message.Author, message.Timestamp, message.EditedTimestamp) + file += fmt.Sprintf("%v", message.Content) + } + + con := fmt.Sprintf("PURGE INITIATED AT %v", time.Now()) + + _, err = s.ChannelMessageSendComplex(memberApprovalChannel, &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 } err = s.ChannelMessagesBulkDelete(i.ChannelID, message_ids) From c3dfbde39f259252c54396ebeeb7eb8cc1050237 Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Mon, 8 Jul 2024 22:07:38 -0400 Subject: [PATCH 02/18] added a line in config for purge-logs channel --- config_example.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config_example.yml b/config_example.yml index 27502e0..b5e89e0 100644 --- a/config_example.yml +++ b/config_example.yml @@ -48,6 +48,8 @@ commands: prospective_role_id: staff_role_id: alumni_role_id: + purge: + channel_id: vote: url: http(s)://example.com(:port) \ No newline at end of file From 55f8c90fd947a036d93b71710261268fa0cdcc43 Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Tue, 9 Jul 2024 21:45:37 -0400 Subject: [PATCH 03/18] Send file record of the purged contents to the channel --- commands/slash/purge.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/commands/slash/purge.go b/commands/slash/purge.go index 8b77f5a..275dbb3 100644 --- a/commands/slash/purge.go +++ b/commands/slash/purge.go @@ -7,11 +7,17 @@ import ( "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) @@ -56,7 +62,8 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco } file := fmt.Sprintf("Record of the purge on %v", time.Now()) - file += "-------------------------------" + file += "Purged " + fmt.Sprint(len(raw_messages)) + " messages!" + file += "------------------------------------------------------" for _, message := range raw_messages { message_ids = append(message_ids, message.ID) @@ -66,8 +73,9 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco } con := fmt.Sprintf("PURGE INITIATED AT %v", time.Now()) + con += "Purged " + fmt.Sprint(len(raw_messages)) + " messages!" - _, err = s.ChannelMessageSendComplex(memberApprovalChannel, &discordgo.MessageSend{ + _, err = s.ChannelMessageSendComplex(purgeLogsChannel, &discordgo.MessageSend{ Content: con, Files: []*discordgo.File{ { From 651825dadfc5c44710c04840b45213cc1a92eb35 Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Tue, 9 Jul 2024 22:22:53 -0400 Subject: [PATCH 04/18] reversed the order of messages for the file and fixed formatting --- commands/slash/purge.go | 53 +++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/commands/slash/purge.go b/commands/slash/purge.go index 275dbb3..1f4b08d 100644 --- a/commands/slash/purge.go +++ b/commands/slash/purge.go @@ -61,33 +61,20 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) } - file := fmt.Sprintf("Record of the purge on %v", time.Now()) - file += "Purged " + fmt.Sprint(len(raw_messages)) + " messages!" + file := fmt.Sprintf("Record of the purge on %v\n", time.Now().Local().Format("2006-01-02 15:04:05-07:00")) + file += "Purged " + fmt.Sprint(len(raw_messages)) + " messages!\n" file += "------------------------------------------------------" - for _, message := range raw_messages { - message_ids = append(message_ids, message.ID) + // For the file + // reverses the list of messages to make the file from oldest to newest + reversedMessages := make([]*discordgo.Message, len(raw_messages)) + for i, message := range raw_messages { + reversedMessages[len(raw_messages)-1-i] = message // Timestamp "may be" removed in a future API version. Too bad! - file += fmt.Sprintf("\n%v SENT AT %v (EDITED AT %v)", message.Author, message.Timestamp, message.EditedTimestamp) - file += fmt.Sprintf("%v", message.Content) - } + file += fmt.Sprintf("\n%v SENT AT %v (EDITED AT %v)", message.Author, message.Timestamp.Local().Format("2006-01-02 15:04:05-07:00"), message.EditedTimestamp.Local().Format("2006-01-02 15:04:05-07:00")) + file += fmt.Sprintf("\n%v", message.Content) - con := fmt.Sprintf("PURGE INITIATED AT %v", time.Now()) - 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 + message_ids = append(message_ids, message.ID) } err = s.ChannelMessagesBulkDelete(i.ChannelID, message_ids) @@ -107,5 +94,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", time.Now().Local().Format("2006-01-02 15:04:05-07:00")) + 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 + } } } From 16b0973826ec77fc5c6a6d26a69306f4105197a7 Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Tue, 9 Jul 2024 22:34:50 -0400 Subject: [PATCH 05/18] debugging --- commands/slash/purge.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/commands/slash/purge.go b/commands/slash/purge.go index 1f4b08d..777f568 100644 --- a/commands/slash/purge.go +++ b/commands/slash/purge.go @@ -65,6 +65,8 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco file += "Purged " + fmt.Sprint(len(raw_messages)) + " messages!\n" file += "------------------------------------------------------" + logging.Debug(s, "here", i.Member.User, span) + // For the file // reverses the list of messages to make the file from oldest to newest reversedMessages := make([]*discordgo.Message, len(raw_messages)) @@ -77,11 +79,15 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco message_ids = append(message_ids, message.ID) } + logging.Debug(s, "here", i.Member.User, span) + err = s.ChannelMessagesBulkDelete(i.ChannelID, message_ids) if err != nil { logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) } + logging.Debug(s, "here", i.Member.User, span) + err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ @@ -90,11 +96,15 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco }, }) + logging.Debug(s, "here", i.Member.User, span) + logging.Debug(s, "Purged "+fmt.Sprint(len(raw_messages))+" messages!", i.Member.User, span) if err != nil { logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) } + logging.Debug(s, "here", i.Member.User, span) + // 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", time.Now().Local().Format("2006-01-02 15:04:05-07:00")) @@ -114,5 +124,8 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) return } + + logging.Debug(s, "here", i.Member.User, span) + } } From 1f085661e6841cf7a85e12ed8d1a001e51e57a39 Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Tue, 9 Jul 2024 22:41:24 -0400 Subject: [PATCH 06/18] Revert "debugging" This reverts commit 16b0973826ec77fc5c6a6d26a69306f4105197a7. --- commands/slash/purge.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/commands/slash/purge.go b/commands/slash/purge.go index 777f568..1f4b08d 100644 --- a/commands/slash/purge.go +++ b/commands/slash/purge.go @@ -65,8 +65,6 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco file += "Purged " + fmt.Sprint(len(raw_messages)) + " messages!\n" file += "------------------------------------------------------" - logging.Debug(s, "here", i.Member.User, span) - // For the file // reverses the list of messages to make the file from oldest to newest reversedMessages := make([]*discordgo.Message, len(raw_messages)) @@ -79,15 +77,11 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco message_ids = append(message_ids, message.ID) } - logging.Debug(s, "here", i.Member.User, span) - err = s.ChannelMessagesBulkDelete(i.ChannelID, message_ids) if err != nil { logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) } - logging.Debug(s, "here", i.Member.User, span) - err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ @@ -96,15 +90,11 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco }, }) - logging.Debug(s, "here", i.Member.User, span) - logging.Debug(s, "Purged "+fmt.Sprint(len(raw_messages))+" messages!", i.Member.User, span) if err != nil { logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) } - logging.Debug(s, "here", i.Member.User, span) - // 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", time.Now().Local().Format("2006-01-02 15:04:05-07:00")) @@ -124,8 +114,5 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) return } - - logging.Debug(s, "here", i.Member.User, span) - } } From 613c4baa8782e1a08adfd62a412c37ef8af4076a Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Tue, 9 Jul 2024 22:43:22 -0400 Subject: [PATCH 07/18] separated two functions --- commands/slash/purge.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/commands/slash/purge.go b/commands/slash/purge.go index 1f4b08d..5b075ac 100644 --- a/commands/slash/purge.go +++ b/commands/slash/purge.go @@ -70,6 +70,9 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco reversedMessages := make([]*discordgo.Message, len(raw_messages)) for i, message := range raw_messages { reversedMessages[len(raw_messages)-1-i] = message + } + + for _, message := range reversedMessages { // Timestamp "may be" removed in a future API version. Too bad! file += fmt.Sprintf("\n%v SENT AT %v (EDITED AT %v)", message.Author, message.Timestamp.Local().Format("2006-01-02 15:04:05-07:00"), message.EditedTimestamp.Local().Format("2006-01-02 15:04:05-07:00")) file += fmt.Sprintf("\n%v", message.Content) From e3b36b1ec1e960199d7c77bc4bab1f685d565890 Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Tue, 9 Jul 2024 22:48:54 -0400 Subject: [PATCH 08/18] 1 last attempt --- commands/slash/purge.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/slash/purge.go b/commands/slash/purge.go index 5b075ac..c331293 100644 --- a/commands/slash/purge.go +++ b/commands/slash/purge.go @@ -67,9 +67,9 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco // For the file // reverses the list of messages to make the file from oldest to newest - reversedMessages := make([]*discordgo.Message, len(raw_messages)) + reversedMessages := make([]discordgo.Message, len(raw_messages)) for i, message := range raw_messages { - reversedMessages[len(raw_messages)-1-i] = message + reversedMessages[len(raw_messages)-1-i] = *message } for _, message := range reversedMessages { From 91681a2e305b294e8a36b2fdfdd939729f819162 Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Wed, 10 Jul 2024 17:22:13 -0400 Subject: [PATCH 09/18] debugging --- commands/slash/purge.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/commands/slash/purge.go b/commands/slash/purge.go index c331293..a06cc3c 100644 --- a/commands/slash/purge.go +++ b/commands/slash/purge.go @@ -67,9 +67,13 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco // For the file // reverses the list of messages to make the file from oldest to newest - reversedMessages := make([]discordgo.Message, len(raw_messages)) - for i, message := range raw_messages { - reversedMessages[len(raw_messages)-1-i] = *message + reversedMessages := make([]*discordgo.Message, len(raw_messages)) + logging.Debug(s, "raw_messages list: "+fmt.Sprint(raw_messages), i.Member.User, span) + logging.Debug(s, "reversedmessages list: "+fmt.Sprint(reversedMessages), i.Member.User, span) + for j, message := range raw_messages { + logging.Debug(s, "message: "+fmt.Sprint(raw_messages), i.Member.User, span) + reversedMessages[len(raw_messages)-1-j] = message + logging.Debug(s, "reversedmessages list: "+fmt.Sprint(reversedMessages), i.Member.User, span) } for _, message := range reversedMessages { From 70b4e33fc95f96a8eb5f26ae61d4b3cc34595e4c Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Wed, 10 Jul 2024 17:29:08 -0400 Subject: [PATCH 10/18] idk at this point --- commands/slash/purge.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/commands/slash/purge.go b/commands/slash/purge.go index a06cc3c..aabc8da 100644 --- a/commands/slash/purge.go +++ b/commands/slash/purge.go @@ -65,18 +65,12 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco 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)) - logging.Debug(s, "raw_messages list: "+fmt.Sprint(raw_messages), i.Member.User, span) - logging.Debug(s, "reversedmessages list: "+fmt.Sprint(reversedMessages), i.Member.User, span) - for j, message := range raw_messages { - logging.Debug(s, "message: "+fmt.Sprint(raw_messages), i.Member.User, span) - reversedMessages[len(raw_messages)-1-j] = message - logging.Debug(s, "reversedmessages list: "+fmt.Sprint(reversedMessages), i.Member.User, span) + // Reverse the order of the messages + for i, j := 0, len(raw_messages)-1; i < j; i, j = i+1, j-1 { + raw_messages[i], raw_messages[j] = raw_messages[j], raw_messages[i] } - for _, message := range reversedMessages { + for _, message := range raw_messages { // Timestamp "may be" removed in a future API version. Too bad! file += fmt.Sprintf("\n%v SENT AT %v (EDITED AT %v)", message.Author, message.Timestamp.Local().Format("2006-01-02 15:04:05-07:00"), message.EditedTimestamp.Local().Format("2006-01-02 15:04:05-07:00")) file += fmt.Sprintf("\n%v", message.Content) From 6c918053f36a03dfc71b71a8fed36325c2ddc7f7 Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Wed, 10 Jul 2024 17:35:24 -0400 Subject: [PATCH 11/18] Revert "debugging" This reverts commit 91681a2e305b294e8a36b2fdfdd939729f819162. --- commands/slash/purge.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/commands/slash/purge.go b/commands/slash/purge.go index aabc8da..3c7b6c0 100644 --- a/commands/slash/purge.go +++ b/commands/slash/purge.go @@ -65,9 +65,11 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco file += "Purged " + fmt.Sprint(len(raw_messages)) + " messages!\n" file += "------------------------------------------------------" - // Reverse the order of the messages - for i, j := 0, len(raw_messages)-1; i < j; i, j = i+1, j-1 { - raw_messages[i], raw_messages[j] = raw_messages[j], raw_messages[i] + // For the file + // reverses the list of messages to make the file from oldest to newest + reversedMessages := make([]discordgo.Message, len(raw_messages)) + for i, message := range raw_messages { + reversedMessages[len(raw_messages)-1-i] = *message } for _, message := range raw_messages { From cdd04ce5e3f791624d101d57f4a181d8cf98277c Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Wed, 10 Jul 2024 17:44:15 -0400 Subject: [PATCH 12/18] trying to figure out the bug --- commands/slash/purge.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/commands/slash/purge.go b/commands/slash/purge.go index 3c7b6c0..2d6d3e7 100644 --- a/commands/slash/purge.go +++ b/commands/slash/purge.go @@ -65,6 +65,12 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco file += "Purged " + fmt.Sprint(len(raw_messages)) + " messages!\n" file += "------------------------------------------------------" + defer func() { + if r := recover(); r != nil { + logging.Error(s, fmt.Sprintf("Error: %v", r), i.Member.User, span) + } + }() + // For the file // reverses the list of messages to make the file from oldest to newest reversedMessages := make([]discordgo.Message, len(raw_messages)) From 75ecaedc0ef34ec88a7b99f721b33a253e1b6f63 Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Wed, 10 Jul 2024 20:56:57 -0400 Subject: [PATCH 13/18] more debugging --- commands/slash/purge.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/commands/slash/purge.go b/commands/slash/purge.go index 2d6d3e7..24a57b8 100644 --- a/commands/slash/purge.go +++ b/commands/slash/purge.go @@ -73,11 +73,19 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco // For the file // reverses the list of messages to make the file from oldest to newest - reversedMessages := make([]discordgo.Message, len(raw_messages)) - for i, message := range raw_messages { - reversedMessages[len(raw_messages)-1-i] = *message + reversedMessages := make([]*discordgo.Message, len(raw_messages)) + logging.Debug(s, fmt.Sprintf("initial raw_messages: %v", raw_messages), i.Member.User, span) + logging.Debug(s, fmt.Sprintf("initial reversedMessages: %v", reversedMessages), i.Member.User, span) + for j, message := range raw_messages { + logging.Debug(s, fmt.Sprintf("loop count: %v", j), i.Member.User, span) + logging.Debug(s, fmt.Sprintf("message in range loop: %v", message), i.Member.User, span) + reversedMessages[len(raw_messages)-1-j] = message + logging.Debug(s, fmt.Sprintf("range raw_messages: %v", raw_messages), i.Member.User, span) + logging.Debug(s, fmt.Sprintf("range reversedMessages: %v", reversedMessages), i.Member.User, span) } + logging.Debug(s, "THIS LINEEEEEE", i.Member.User, span) + for _, message := range raw_messages { // Timestamp "may be" removed in a future API version. Too bad! file += fmt.Sprintf("\n%v SENT AT %v (EDITED AT %v)", message.Author, message.Timestamp.Local().Format("2006-01-02 15:04:05-07:00"), message.EditedTimestamp.Local().Format("2006-01-02 15:04:05-07:00")) From 8ad6767cba06952a2513a002e6abcd3123a06327 Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Wed, 10 Jul 2024 21:29:42 -0400 Subject: [PATCH 14/18] more debugging --- commands/slash/purge.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/commands/slash/purge.go b/commands/slash/purge.go index 24a57b8..df99e80 100644 --- a/commands/slash/purge.go +++ b/commands/slash/purge.go @@ -74,31 +74,32 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco // For the file // reverses the list of messages to make the file from oldest to newest reversedMessages := make([]*discordgo.Message, len(raw_messages)) - logging.Debug(s, fmt.Sprintf("initial raw_messages: %v", raw_messages), i.Member.User, span) - logging.Debug(s, fmt.Sprintf("initial reversedMessages: %v", reversedMessages), i.Member.User, span) for j, message := range raw_messages { - logging.Debug(s, fmt.Sprintf("loop count: %v", j), i.Member.User, span) - logging.Debug(s, fmt.Sprintf("message in range loop: %v", message), i.Member.User, span) reversedMessages[len(raw_messages)-1-j] = message - logging.Debug(s, fmt.Sprintf("range raw_messages: %v", raw_messages), i.Member.User, span) - logging.Debug(s, fmt.Sprintf("range reversedMessages: %v", reversedMessages), i.Member.User, span) + } logging.Debug(s, "THIS LINEEEEEE", i.Member.User, span) - for _, message := range raw_messages { + for _, message := range reversedMessages { // Timestamp "may be" removed in a future API version. Too bad! + logging.Debug(s, "THIS LINEEEEEE", i.Member.User, span) file += fmt.Sprintf("\n%v SENT AT %v (EDITED AT %v)", message.Author, message.Timestamp.Local().Format("2006-01-02 15:04:05-07:00"), message.EditedTimestamp.Local().Format("2006-01-02 15:04:05-07:00")) file += fmt.Sprintf("\n%v", message.Content) + logging.Debug(s, "THIS LINEEEEEE", i.Member.User, span) message_ids = append(message_ids, message.ID) } + logging.Debug(s, "THIS LINEEEEEE", i.Member.User, span) + err = s.ChannelMessagesBulkDelete(i.ChannelID, message_ids) if err != nil { logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) } + logging.Debug(s, "THIS LINEEEEEE", i.Member.User, span) + err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ From 284a4f8a8025bbb3b52218cd3c8138ba09e60fa6 Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Wed, 10 Jul 2024 21:38:59 -0400 Subject: [PATCH 15/18] should be good now --- commands/slash/purge.go | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/commands/slash/purge.go b/commands/slash/purge.go index df99e80..3cd4ffc 100644 --- a/commands/slash/purge.go +++ b/commands/slash/purge.go @@ -65,41 +65,31 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco file += "Purged " + fmt.Sprint(len(raw_messages)) + " messages!\n" file += "------------------------------------------------------" - defer func() { - if r := recover(); r != nil { - logging.Error(s, fmt.Sprintf("Error: %v", r), i.Member.User, span) - } - }() - // 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 - } - logging.Debug(s, "THIS LINEEEEEE", i.Member.User, span) - for _, message := range reversedMessages { - // Timestamp "may be" removed in a future API version. Too bad! - logging.Debug(s, "THIS LINEEEEEE", i.Member.User, span) - file += fmt.Sprintf("\n%v SENT AT %v (EDITED AT %v)", message.Author, message.Timestamp.Local().Format("2006-01-02 15:04:05-07:00"), message.EditedTimestamp.Local().Format("2006-01-02 15:04:05-07:00")) - file += fmt.Sprintf("\n%v", message.Content) - logging.Debug(s, "THIS LINEEEEEE", i.Member.User, span) + // Check to see if message is edited + if message.EditedTimestamp == nil { + file += fmt.Sprintf("\n%v SENT AT %v", message.Author, message.Timestamp.Local().Format("2006-01-02 15:04:05-07:00")) + file += fmt.Sprintf("\n%v", message.Content) + } else { + file += fmt.Sprintf("\n%v SENT AT %v (EDITED AT %v)", message.Author, message.Timestamp.Local().Format("2006-01-02 15:04:05-07:00"), message.EditedTimestamp.Local().Format("2006-01-02 15:04:05-07:00")) + file += fmt.Sprintf("\n%v", message.Content) + } message_ids = append(message_ids, message.ID) } - logging.Debug(s, "THIS LINEEEEEE", i.Member.User, span) - err = s.ChannelMessagesBulkDelete(i.ChannelID, message_ids) if err != nil { logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) } - logging.Debug(s, "THIS LINEEEEEE", i.Member.User, span) - err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ From 3cdad43c6a9b7eff02072307ef8e1e48f4440ede Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Wed, 10 Jul 2024 22:31:11 -0400 Subject: [PATCH 16/18] clean up --- commands/slash/purge.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/commands/slash/purge.go b/commands/slash/purge.go index 3cd4ffc..78fcfb1 100644 --- a/commands/slash/purge.go +++ b/commands/slash/purge.go @@ -48,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 { @@ -61,9 +68,9 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) } - file := fmt.Sprintf("Record of the purge on %v\n", time.Now().Local().Format("2006-01-02 15:04:05-07:00")) + file := fmt.Sprintf("Record of the purge on %v\n", time.Now().In(timeloc).Format(" 15:04:05")) file += "Purged " + fmt.Sprint(len(raw_messages)) + " messages!\n" - file += "------------------------------------------------------" + file += "------------------------------------------------" // For the file // reverses the list of messages to make the file from oldest to newest @@ -75,10 +82,10 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco for _, message := range reversedMessages { // Check to see if message is edited if message.EditedTimestamp == nil { - file += fmt.Sprintf("\n%v SENT AT %v", message.Author, message.Timestamp.Local().Format("2006-01-02 15:04:05-07:00")) + 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%v SENT AT %v (EDITED AT %v)", message.Author, message.Timestamp.Local().Format("2006-01-02 15:04:05-07:00"), message.EditedTimestamp.Local().Format("2006-01-02 15:04:05-07:00")) + 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) } From 6298c85b62e65957e90d702f3a14525b65180657 Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Wed, 10 Jul 2024 22:36:07 -0400 Subject: [PATCH 17/18] missing date --- commands/slash/purge.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/slash/purge.go b/commands/slash/purge.go index 78fcfb1..1b2bbf2 100644 --- a/commands/slash/purge.go +++ b/commands/slash/purge.go @@ -68,7 +68,7 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err}) } - file := fmt.Sprintf("Record of the purge on %v\n", time.Now().In(timeloc).Format(" 15:04:05")) + 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 += "------------------------------------------------" From 9548f2beb6b3200b027d7587086092b39c2ebfa8 Mon Sep 17 00:00:00 2001 From: Lukas Peters Date: Wed, 10 Jul 2024 22:50:53 -0400 Subject: [PATCH 18/18] finished formatting --- commands/slash/purge.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/slash/purge.go b/commands/slash/purge.go index 1b2bbf2..e4bc6c9 100644 --- a/commands/slash/purge.go +++ b/commands/slash/purge.go @@ -112,7 +112,7 @@ func Purge() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco // 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", time.Now().Local().Format("2006-01-02 15:04:05-07:00")) + 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{