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
/
Copy pathcommands.go
127 lines (109 loc) · 3.44 KB
/
commands.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
package main
import "github.com/bwmarrin/discordgo"
import "strings"
type command struct {
Name string
Help string
Exec func(*discordgo.Session, *discordgo.MessageCreate, []string)
}
var (
commMap = make(map[string]command)
)
func prepareCommands() {
command{
Name: "git",
Help: "Args: none\n\nLinks 2Bots github page.\n\nExample:\n`" + conf.Prefix + "git`",
Exec: msgGit,
}.add()
command{
Name: "emoji",
Help: "Args: [emoji]\n\nSends a large image of the given emoji.\n\nExample:\n`" + conf.Prefix + ":smile:`",
Exec: msgEmoji,
}.add()
command{
Name: "setGame",
Help: "Args: [game]\n\nSets your current game to 'game'",
Exec: msgGame,
}.add()
command{
Name: "findEmoji",
Help: "Args: [emoji | name]\n\nReturns all the emojis that match the given emoji or emoji name in all the servers you are in",
Exec: msgFindEmoji,
}.add()
command{
Name: "image",
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." +
"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" +
"Example:\n`!owo image save 2B Happy`\n2Bot downloads the image and sends it off for reviewing\n\n" +
"`" + conf.Prefix + "image recall 2B Happy`\nIf your image was confirmed, 2Bot will send the image named `2B Happy`\n\n" +
"`" + conf.Prefix + "image delete 2B Happy`\nThis will delete the image you saved called `2B Happy`\n\n" +
"`" + conf.Prefix + "image list`\nThis will list your saved images along with a preview!\n\n" +
"`" + conf.Prefix + "image status`\nShows some details on your saved images and quota",
Exec: msgImageRecall,
}.add()
command{"help",
"", msgHelp,
}.add()
}
//Small wrapper function to reduce clutter
func l(s string) (r string) {
return strings.ToLower(s)
}
func parseCommand(s *discordgo.Session, m *discordgo.MessageCreate, message string) {
msglist := strings.Fields(message)
command := func() string {
if strings.HasPrefix(message, " ") {
return " " + msglist[0]
}
return msglist[0]
}()
if command == "emoji" {
return
}
if command == l(commMap[command].Name) {
commMap[command].Exec(s, m, msglist[1:])
return
}
//if data passed as command isnt a valid command,
//check if its an emoji
commMap["emoji"].Exec(s, m, msglist)
}
func listCommands(s *discordgo.Session, m *discordgo.MessageCreate) {
var commands []string
for _, val := range commMap {
if val.Name == "" {
continue
}
commands = append(commands, "`"+val.Name+"`")
}
userColor := s.State.UserColor(s.State.User.ID, m.ChannelID)
edit := newEdit(s, m, userColor)
edit.setFields([]*discordgo.MessageEmbedField{
{Name: "List", Value: strings.Join(commands, ", ")},
{Name: "Info", Value: "\n\nUse `" + conf.Prefix + "help [command]` for detailed info about a command."},
})
edit.send()
}
func msgHelp(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
if len(msglist) == 0 {
listCommands(s, m)
return
}
command := msglist[0]
val, ok := commMap[command]
if !ok {
return
}
userColor := s.State.UserColor(s.State.User.ID, m.ChannelID)
edit := newEdit(s, m, userColor)
edit.setFields([]*discordgo.MessageEmbedField{
{Name: "Details", Value: val.Help},
})
edit.setTitle("Help " + val.Name)
edit.send()
return
}
func (c command) add() command {
commMap[l(c.Name)] = c
return c
}