Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit 4f32d4c

Browse files
committed
Largely finished image recall. Now onto the next ones
1 parent 2cf913a commit 4f32d4c

6 files changed

Lines changed: 162 additions & 57 deletions

File tree

commands.go

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,45 @@ type command struct {
1212

1313
var (
1414
commMap = make(map[string]command)
15-
gitCommand = command{
15+
16+
)
17+
18+
func prepareCommands() {
19+
command{
1620
Name: "git",
17-
Help: "Args: none\n\nLinks 2Bots github page.\n\nExample:\n`!owo git`",
21+
Help: "Args: none\n\nLinks 2Bots github page.\n\nExample:\n`"+ conf.Prefix + "git`",
1822
Exec: msgGit,
1923
}.add()
20-
emojiCommand = command{
21-
Name: "",
22-
Help: "Args: [emoji]\n\nSends a large image of the given emoji.\n\nExample:\n`!owo :smile:`",
24+
command{
25+
Name: "emoji",
26+
Help: "Args: [emoji]\n\nSends a large image of the given emoji.\n\nExample:\n`"+ conf.Prefix + ":smile:`",
2327
Exec: msgEmoji,
2428
}.add()
25-
gameCommand = command{
29+
command{
2630
Name: "setGame",
2731
Help: "Args: [game]\n\nSets your current game to 'game'",
2832
Exec: msgGame,
2933
}.add()
30-
findEmojiCommand = command{
34+
command{
3135
Name: "findEmoji",
3236
Help: "Args: [emoji | name]\n\nReturns all the emojis that match the given emoji or emoji name in all the servers you are in",
3337
Exec: msgFindEmoji,
3438
}.add()
35-
imageCommand = command{
39+
command{
3640
Name: "image",
3741
Help: "Args: [save,recall,delete,list,status] [name]\n\nSave images and recall them at anytime! Everyone gets 8MB of image storage. Any name counts so long theres no `/` in it." +
3842
"Only you can 'recall' your saved images. There's a review process to make sure nothing illegal is being uploaded but we're fairly relaxed for the most part\n\n" +
3943
"Example:\n`!owo image save 2B Happy`\n2Bot downloads the image and sends it off for reviewing\n\n" +
40-
"`!owo image recall 2B Happy`\nIf your image was confirmed, 2Bot will send the image named `2B Happy`\n\n" +
41-
"`!owo image delete 2B Happy`\nThis will delete the image you saved called `2B Happy`\n\n" +
42-
"`!owo image list`\nThis will list your saved images along with a preview!\n\n" +
43-
"`!owo image status`\nShows some details on your saved images and quota",
44+
"`"+ conf.Prefix + "image recall 2B Happy`\nIf your image was confirmed, 2Bot will send the image named `2B Happy`\n\n" +
45+
"`"+ conf.Prefix + "image delete 2B Happy`\nThis will delete the image you saved called `2B Happy`\n\n" +
46+
"`"+ conf.Prefix + "image list`\nThis will list your saved images along with a preview!\n\n" +
47+
"`"+ conf.Prefix + "image status`\nShows some details on your saved images and quota",
4448
Exec: msgImageRecall,
4549
}.add()
46-
helpComm = command{"help",
50+
command{"help",
4751
"", msgHelp,
4852
}.add()
49-
)
53+
}
5054

5155
//Small wrapper function to reduce clutter
5256
func l(s string) (r string) {
@@ -69,7 +73,7 @@ func parseCommand(s *discordgo.Session, m *discordgo.MessageCreate, message stri
6973

7074
//if data passed as command isnt a valid command,
7175
//check if its an emoji
72-
emojiCommand.Exec(s, m, msglist)
76+
commMap["emoji"].Exec(s, m, msglist)
7377
}
7478

7579
func listCommands(s *discordgo.Session, m *discordgo.MessageCreate) {
@@ -83,36 +87,34 @@ func listCommands(s *discordgo.Session, m *discordgo.MessageCreate) {
8387

8488
userColor := s.State.UserColor(s.State.User.ID, m.ChannelID)
8589

86-
s.ChannelMessageDelete(m.ChannelID, m.Message.ID)
87-
s.ChannelMessageSendEmbed(m.ChannelID, &discordgo.MessageEmbed{
88-
Title: "Help",
89-
90-
Color: userColor,
91-
92-
Fields: []*discordgo.MessageEmbedField{
93-
{Name: "List", Value: strings.Join(commands, ", ")},
94-
{Name: "Info", Value: "\n\nUse `" + conf.Prefix + "help [command]` for detailed info about a command."},
95-
},
90+
edit := newEdit(s, m, userColor)
91+
edit.setFields([]*discordgo.MessageEmbedField{
92+
{Name: "List", Value: strings.Join(commands, ", ")},
93+
{Name: "Info", Value: "\n\nUse `" + conf.Prefix + "help [command]` for detailed info about a command."},
9694
})
95+
edit.send()
9796
}
9897

99-
func (c command) msgHelp(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
98+
func msgHelp(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
10099
if len(msglist) == 0 {
101100
listCommands(s, m)
102101
return
103102
}
104-
userColor := s.State.UserColor(s.State.User.ID, m.ChannelID)
105103

106-
s.ChannelMessageDelete(m.ChannelID, m.Message.ID)
107-
s.ChannelMessageSendEmbed(m.ChannelID, &discordgo.MessageEmbed{
108-
Title: "Help: " + c.Name,
104+
command := msglist[0]
105+
val, ok := commMap[command]
106+
if !ok {
107+
return
108+
}
109109

110-
Color: userColor,
110+
userColor := s.State.UserColor(s.State.User.ID, m.ChannelID)
111111

112-
Fields: []*discordgo.MessageEmbedField{
113-
{Name: "Details", Value: c.Help},
114-
},
112+
edit := newEdit(s, m, userColor)
113+
edit.setFields([]*discordgo.MessageEmbedField{
114+
{Name: "Details", Value: val.Help},
115115
})
116+
edit.setTitle("Help "+val.Name)
117+
edit.send()
116118
return
117119
}
118120

main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ var (
2828
errorLog *log.Logger
2929
infoLog *log.Logger
3030
logF *os.File
31+
// Zero width whitespace to replace message content
32+
content = "​"
3133
)
3234

3335
func createConfig() error {
@@ -173,16 +175,16 @@ func main() {
173175
fmt.Println(err)
174176
errorLog.Fatalln(err)
175177
}
178+
defer dg.Close()
179+
180+
prepareCommands()
176181

177182
dg.AddHandlerOnce(ready)
178183
dg.AddHandler(message)
179184

180185
sc := make(chan os.Signal, 1)
181186
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
182187
<-sc
183-
184-
// Cleanly close down the Discord session.
185-
dg.Close()
186188
}
187189

188190
func ready(s *discordgo.Session, m *discordgo.Ready) {

message.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"github.com/bwmarrin/discordgo"
5+
)
6+
7+
type editComplex struct {
8+
Content *string
9+
Embed *discordgo.MessageEmbed
10+
11+
MessageCreate *discordgo.MessageCreate
12+
Session *discordgo.Session
13+
}
14+
15+
func newEdit(s *discordgo.Session, m *discordgo.MessageCreate, color int) *editComplex {
16+
return &editComplex {
17+
Content: &content,
18+
19+
MessageCreate: m,
20+
Session: s,
21+
22+
Embed: &discordgo.MessageEmbed {
23+
Color: color,
24+
},
25+
}
26+
}
27+
28+
func (e *editComplex) setTitle(title string) {
29+
e.Embed.Title = title
30+
}
31+
32+
func (e *editComplex) setImage(url string) {
33+
e.Embed.Image = &discordgo.MessageEmbedImage {
34+
URL: url,
35+
}
36+
}
37+
38+
func (e *editComplex) setFields(fields []*discordgo.MessageEmbedField) {
39+
e.Embed.Fields = fields
40+
}
41+
42+
func (e *editComplex) setDescription(text string) {
43+
e.Embed.Description = text
44+
}
45+
46+
func (e *editComplex) send() *discordgo.Message{
47+
msg, err := e.Session.ChannelMessageEditComplex(&discordgo.MessageEdit{
48+
Channel: e.MessageCreate.ChannelID,
49+
ID: e.MessageCreate.Message.ID,
50+
51+
Content: &content,
52+
53+
Embed: &discordgo.MessageEmbed {
54+
Color: e.Embed.Color,
55+
Title: e.Embed.Title,
56+
Description: e.Embed.Description,
57+
58+
Fields: e.Embed.Fields,
59+
60+
Image: e.Embed.Image,
61+
},
62+
})
63+
64+
if err != nil {
65+
errorLog.Println(err)
66+
return nil
67+
}
68+
69+
return msg
70+
}

msgEmoji.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ func msgEmoji(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string
4343
defer resp.Body.Close()
4444

4545
s.ChannelFileSend(m.ChannelID, "emoji.png", resp.Body)
46-
4746
s.ChannelMessageDelete(m.ChannelID, m.ID)
4847
} else {
4948
emoji := emojiFile(msglist[0])
@@ -65,6 +64,7 @@ func msgEmoji(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string
6564

6665
func msgFindEmoji(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
6766
var emojiName string
67+
content := "​"
6868
submatch := emojiRegex.FindStringSubmatch(strings.Join(msglist, " "))
6969

7070
if len(submatch) < 2 {
@@ -104,26 +104,29 @@ func msgFindEmoji(s *discordgo.Session, m *discordgo.MessageCreate, msglist []st
104104
}
105105

106106
userColor := s.State.UserColor(s.State.User.ID, m.ChannelID)
107-
107+
108108
if len(emojis) == 0 {
109-
s.ChannelMessageSendEmbed(m.ChannelID, &discordgo.MessageEmbed{
110-
Title: "No emojis found!",
109+
edit := newEdit(s, m, userColor)
110+
edit.setTitle("No emojis found!")
111+
edit.send()
111112

112-
Color: userColor,
113-
})
114113
return
115114
}
116115

117-
_, err := s.ChannelMessageSendEmbed(m.ChannelID, &discordgo.MessageEmbed{
118-
Title: "Emojis with the substring `" + emojiName + "`",
116+
_, err := s.ChannelMessageEditComplex(&discordgo.MessageEdit{
117+
ID: m.Message.ID,
118+
Channel: m.ChannelID,
119+
Content: &content,
120+
Embed: &discordgo.MessageEmbed{
121+
Title: "Emojis with the substring `" + emojiName + "`",
119122

120-
Color: userColor,
123+
Color: userColor,
121124

122-
Fields: emojisEmbed,
125+
Fields: emojisEmbed,
126+
},
123127
})
124128
if err != nil {
125129
errorLog.Println(err)
126130
}
127131

128-
s.ChannelMessageDelete(m.ChannelID, m.Message.ID)
129132
}

msgImageRecall.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package main
22

33
import (
4-
"io/ioutil"
4+
"fmt"
55
"github.com/bwmarrin/discordgo"
66
"net/http"
7-
"fmt"
7+
"golang.org/x/crypto/blake2b"
8+
"strings"
9+
"bytes"
10+
"encoding/hex"
811
)
912

1013
func msgImageRecall(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
@@ -16,23 +19,47 @@ func msgImageRecall(s *discordgo.Session, m *discordgo.MessageCreate, msglist []
1619

1720
switch msglist[0] {
1821
case "recall":
19-
fimageRecall(s, m, msglist)
22+
fimageRecall(s, m, msglist[1:])
2023
case "save":
21-
fimageSave(s, m, msglist)
24+
fimageSave(s, m, msglist[1:])
2225
case "list":
23-
fimageList(s, m, msglist)
26+
fimageList(s, m, msglist[1:])
2427
case "delete":
25-
fimageDelete(s, m, msglist)
28+
fimageDelete(s, m, msglist[1:])
2629
case "info":
27-
fimageInfo(s, m, msglist)
30+
fimageInfo(s, m, msglist[1:])
2831
}
2932
}
3033

3134
func fimageRecall(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
35+
prefixedImgName := m.Author.ID + "_" + strings.Join(msglist, " ")
36+
hash := blake2b.Sum256([]byte(prefixedImgName))
37+
imgFileName := hex.EncodeToString(hash[:])
38+
39+
URL := fmt.Sprintf("https://api.2bot.ml/image/%s/recall/%s", m.Author.ID, imgFileName)
40+
resp, err := http.Get(URL)
41+
if err != nil {
42+
errorLog.Println(err)
43+
return
44+
}
45+
defer resp.Body.Close()
46+
47+
if resp.StatusCode != http.StatusOK {
48+
return
49+
}
50+
51+
buf := new(bytes.Buffer)
52+
buf.ReadFrom(resp.Body)
53+
imgURL := buf.String()
54+
55+
edit := newEdit(s, m, s.State.UserColor(s.State.User.ID, m.ChannelID))
56+
edit.setDescription(strings.Join(msglist, " "))
57+
edit.setImage(imgURL)
58+
edit.send()
3259
}
3360

3461
func fimageSave(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
35-
resp, err := http.Get("http://strum355.netsoc.co:8042/inServer?id="+m.Author.ID)
62+
resp, err := http.Get("https://api.2bot.ml/inServer?id="+m.Author.ID)
3663
if err != nil {
3764
errorLog.Println(err)
3865
return

utilities.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ func guildDetails(channelID string, s *discordgo.Session) (*discordgo.Guild, err
1414
return nil, err
1515
}
1616
return guildDetails, nil
17-
}
17+
}
18+

0 commit comments

Comments
 (0)