This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
msgImageRecall.go
97 lines (78 loc) · 2.43 KB
/
msgImageRecall.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
package main
import (
"bytes"
"encoding/hex"
"fmt"
"net/http"
"strings"
"github.com/bwmarrin/discordgo"
"golang.org/x/crypto/blake2b"
)
const apiURL = "https://api.2bot.ovh"
func msgImageRecall(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
if len(msglist) < 1 {
s.ChannelMessageEdit(m.ChannelID, m.Message.ID, "Available sub-commands for `image`:\n`save`, `delete`, `recall`, `list`, `status`\n"+
"Type `"+conf.Prefix+"help image` to see more info about this command")
return
}
switch msglist[0] {
case "recall":
fimageRecall(s, m, msglist[1:])
/* case "save":
fimageSave(s, m, msglist[1:])
case "list":
fimageList(s, m, msglist[1:])
case "delete":
fimageDelete(s, m, msglist[1:])
case "info":
fimageInfo(s, m, msglist[1:]) */
}
}
func fimageRecall(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
prefixedImgName := m.Author.ID + "_" + strings.Join(msglist, " ")
hash := blake2b.Sum256([]byte(prefixedImgName))
imgFileName := hex.EncodeToString(hash[:])
URL := fmt.Sprintf(apiURL+"/image/%s/recall/%s", m.Author.ID, imgFileName)
resp, err := http.Get(URL)
if err != nil {
errorLog.Println(err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
errorLog.Println(fmt.Sprintf("%d %s", resp.StatusCode, URL))
return
}
var buf bytes.Buffer
if _, err := buf.ReadFrom(resp.Body); err != nil {
errorLog.Println("error reading image response", err)
return
}
imgURL := buf.String()
edit := newEdit(s, m, s.State.UserColor(s.State.User.ID, m.ChannelID))
edit.setDescription(strings.Join(msglist, " "))
edit.setImage(imgURL)
edit.send()
}
func fimageSave(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
resp, err := http.Get(apiURL + "/inServer?id=" + m.Author.ID)
if err != nil {
errorLog.Println(err)
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
errorLog.Println("Need to be in 2Bot server to use this command https://discord.gg/9T34Y6u")
return
}
if len(m.Attachments) == 0 || m.Attachments[0].Height == 0 {
errorLog.Println("Need to send an image to be saved")
return
}
}
func fimageList(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
}
func fimageDelete(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
}
func fimageInfo(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
}