/
cdndl.go
73 lines (63 loc) · 1.4 KB
/
cdndl.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
package logpull
import (
"log"
"github.com/bwmarrin/discordgo"
"github.com/Jquinn147/pullcord/cdndl"
)
const (
_ = iota
cdnIcon
cdnSplash
cdnAvatar
cdnChannelIcon
)
var skipCodes = map[int]bool{404: true, 403: true}
// TODO: move somewhere else
func handleDLError(err error) error {
if cerr, ok := err.(cdndl.ErrNotOk); ok && skipCodes[cerr.StatusCode] {
log.Printf("warning: skipping %s (%d)", cerr.URL, cerr.StatusCode)
return nil
} else {
// TODO: retry?
return err
}
}
func (p *Puller) cdnDL(v interface{}, subtype int) error {
if p.lightMode {
return nil
}
var err error
switch v.(type) {
case *discordgo.MessageAttachment:
err = cdndl.Attachment(v.(*discordgo.MessageAttachment).URL)
case *discordgo.Guild:
g := v.(*discordgo.Guild)
switch subtype {
case cdnIcon:
err = cdndl.Icon(g.ID, g.Icon)
case cdnSplash:
err = cdndl.Splash(g.ID, g.Splash)
default:
panic("unsupported subtype")
}
case *discordgo.User:
if subtype == cdnAvatar {
err = cdndl.Avatar(v.(*discordgo.User))
} else {
panic("unsupported subtype")
}
case *discordgo.Channel:
c := v.(*discordgo.Channel)
if subtype == cdnChannelIcon {
err = cdndl.ChannelIcon(c.ID, c.Icon)
} else {
panic("unsupported subtype")
}
case *discordgo.Emoji:
e := v.(*discordgo.Emoji)
err = cdndl.Emoji(e.ID, e.Animated)
default:
panic("unsupported type")
}
return handleDLError(err)
}