This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
/
archive.go
143 lines (128 loc) · 3.54 KB
/
archive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package modutilcommands
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"github.com/Starshine113/covebotnt/crouter"
"github.com/Starshine113/flagparser"
"github.com/bwmarrin/discordgo"
)
type arc struct {
Version int `json:"version"`
InvokedBy *discordgo.User `json:"invoked_by"`
Channel *discordgo.Channel `json:"channel"`
Messages []*msg `json:"messages"`
}
type msg struct {
ID string `json:"id"`
Content string `json:"content"`
Timestamp discordgo.Timestamp `json:"timestamp"`
Author author `json:"author"`
Attachments []*discordgo.MessageAttachment `json:"attachments"`
Embeds []*discordgo.MessageEmbed `json:"embeds"`
Reactions []*discordgo.MessageReactions `json:"reactions"`
}
type author struct {
ID string `json:"id"`
Username string `json:"username"`
Discriminator string `json:"discriminator"`
Bot bool `json:"bot"`
AvatarURL string `json:"avatar_url"`
}
// Archive ...
func Archive(ctx *crouter.Ctx) (err error) {
archive := arc{InvokedBy: ctx.Author, Channel: ctx.Channel}
fp, _ := flagparser.NewFlagParser(flagparser.Bool("gzip", "gz"), flagparser.String("out", "o", "output"))
args, err := fp.Parse(ctx.Args)
if err != nil {
_, err = ctx.CommandError(err)
return err
}
var gz bool
if args["gzip"].(bool) {
gz = true
}
out := ctx.Channel.ID
if args["out"].(string) != "" {
channel, err := ctx.ParseChannel(args["out"].(string))
if err != nil {
_, err = ctx.CommandError(err)
return err
}
out = channel.ID
}
ctx.Send("Working, please wait...")
if err = ctx.Session.ChannelTyping(out); err != nil {
return err
}
messages := make([]*msg, 0)
var before string
for {
msgs, err := ctx.Session.ChannelMessages(ctx.Channel.ID, 100, before, "", "")
fmt.Printf("Messages before ID %v, got %v messages\n", before, len(msgs))
if len(msgs) == 0 {
break
}
if err != nil {
_, err = ctx.CommandError(err)
return err
}
before = msgs[len(msgs)-1].ID
for _, m := range msgs {
messages = append(messages, &msg{
ID: m.ID,
Content: m.Content,
Timestamp: m.Timestamp,
Author: author{
ID: m.Author.ID,
Username: m.Author.Username,
Discriminator: m.Author.Discriminator,
Bot: m.Author.Bot,
AvatarURL: m.Author.AvatarURL(""),
},
Attachments: m.Attachments,
Embeds: m.Embeds,
Reactions: m.Reactions,
})
}
}
archive.Messages = messages
b, err := json.MarshalIndent(archive, "", " ")
if err != nil {
_, err = ctx.CommandError(err)
return err
}
fn := fmt.Sprintf("export-%v-%v.json", ctx.Channel.Name, ctx.Message.Timestamp)
var buf *bytes.Buffer
if gz {
buf = new(bytes.Buffer)
zw := gzip.NewWriter(buf)
zw.Name = fn
_, err = zw.Write(b)
if err != nil {
_, err = ctx.CommandError(err)
return err
}
err = zw.Close()
if err != nil {
_, err = ctx.CommandError(err)
return err
}
fn = fn + ".gz"
} else {
buf = bytes.NewBuffer(b)
}
file := discordgo.File{
Name: fn,
Reader: buf,
}
_, err = ctx.Session.ChannelMessageSendComplex(out, &discordgo.MessageSend{
Content: fmt.Sprintf("Done!\n> Archive of %v (#%v), invoked by %v at %v.", ctx.Channel.Mention(), ctx.Channel.Name, ctx.Author.String(), ctx.Message.Timestamp),
Files: []*discordgo.File{&file},
AllowedMentions: &discordgo.MessageAllowedMentions{
Parse: []discordgo.AllowedMentionType{},
},
})
return err
}