-
Notifications
You must be signed in to change notification settings - Fork 0
/
pfps.go
88 lines (81 loc) · 2.37 KB
/
pfps.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
/*
Copyright (C) 2021-2023 jlortiz
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"bufio"
"encoding/base64"
"fmt"
"io"
"os"
"time"
"github.com/bwmarrin/discordgo"
"jlortiz.org/jlort2/modules/log"
)
func updatePfp(self *discordgo.Session) {
t := time.Tick(6 * time.Hour)
for {
f, err := os.Open("pfps" + string(os.PathSeparator) + "defs.dat")
if err != nil {
log.Error(err)
return
}
defer f.Close()
rd := bufio.NewReader(f)
ts := time.Now()
ots := ts.Add(-6 * time.Hour)
// fmt.Println(ts, ots)
var buf [4]byte
var dFlag bool
var name string
var n int
for {
n, err = rd.Read(buf[:])
if n != 4 {
break
}
name, err = rd.ReadString(0)
if err != nil {
break
}
name = name[:len(name)-1]
startts := time.Date(ts.Year(), time.Month(buf[0]), int(buf[1]), 0, 0, 0, 0, ts.Location())
endts := time.Date(ts.Year(), time.Month(buf[2]), int(buf[3]), 0, 0, 0, 0, ts.Location())
// fmt.Println(buf, startts.Before(ts), endts.Before(ts), startts.Before(ots), endts.Before(ots))
if startts.Before(ts) && !endts.Before(ts) {
if dFlag || ots.Before(startts) {
avatar, err := os.ReadFile("pfps" + string(os.PathSeparator) + name)
if err == nil {
_, err = self.UserUpdate("", "data:image/png;base64,"+base64.StdEncoding.EncodeToString(avatar))
if err != nil {
log.Error(fmt.Errorf("could not set avatar: %w", err))
} else {
log.Warn("Updated profile picture")
}
} else {
log.Error(fmt.Errorf("could not read avatar: %w", err))
}
}
return
} else if !dFlag && startts.Before(ots) && !endts.Before(ots) {
dFlag = true
}
}
if err != io.EOF {
log.Error(fmt.Errorf("unable to update PFP: %w", err))
} else {
log.Warn("PFP defs missing default!")
}
<-t
}
}