Skip to content

Instantly share code, notes, and snippets.

@diamondburned
Created April 29, 2021 05:44
Show Gist options
  • Save diamondburned/94ce9dab8f01414be2e9aa882d038bc7 to your computer and use it in GitHub Desktop.
Save diamondburned/94ce9dab8f01414be2e9aa882d038bc7 to your computer and use it in GitHub Desktop.
eraser
package main
import (
"flag"
"log"
"os"
"github.com/diamondburned/arikawa/v2/api"
"github.com/diamondburned/arikawa/v2/discord"
)
var (
channelID uint64
fromMsgID uint64
toMsgID uint64
)
func main() {
flag.Uint64Var(&channelID, "chID", 0, "channel ID to wipe")
flag.Uint64Var(&fromMsgID, "from", 0, "from message ID")
flag.Uint64Var(&toMsgID, "to", 0, "to message ID")
flag.Parse()
if channelID == 0 || fromMsgID <= toMsgID {
log.Fatalln("Missing flags, check -h.")
}
c := api.NewClient(os.Getenv("BOT_TOKEN"))
channelID := discord.ChannelID(channelID)
currentID := discord.MessageID(fromMsgID)
endAtID := discord.MessageID(toMsgID)
idList := make([]discord.MessageID, 0, 100)
var working = true
for working {
// Include the current ID in the delete list.
idList = idList[:1]
idList[0] = currentID
msgs, err := c.MessagesBefore(channelID, currentID, 99)
if err != nil {
log.Fatalln("failed to get messages:", err)
}
// Ensure that the latest message in the list is AFTER the ID to finish
// at. Otherwise, we're already done.
if len(msgs) == 0 || msgs[0].ID < endAtID {
break
}
// Iterate from latest to earliest.
for _, msg := range msgs {
idList = append(idList, msg.ID)
if msg.ID == endAtID {
working = false
break
}
}
log.Println("Deleting", idList)
wipe(c, channelID, idList)
// Use the earliest so we can iterate further.
currentID = idList[len(idList)-1]
}
}
func wipe(c *api.Client, channelID discord.ChannelID, idList []discord.MessageID) {
if err := c.DeleteMessages(channelID, idList); err != nil {
// Work around a potential fail.
failed := 0
for _, id := range idList {
if err := c.DeleteMessage(channelID, id); err != nil {
log.Println("failed to delete a message:", err)
failed++
}
}
if failed == len(idList)-1 {
log.Fatalln("failed to wipe any messages")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment