/
help.go
118 lines (99 loc) · 2.77 KB
/
help.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
package handlers
import (
"database/sql"
"fmt"
"github.com/MattiasBerlin/outbot/commands"
"github.com/bwmarrin/discordgo"
"github.com/pkg/errors"
"strings"
)
const (
infoColor = 0x4286f4
)
// HelpCommand for getting help descriptions.
func HelpCommand() commands.Command {
return commands.Command{
CallPhrase: "help",
Permission: commands.All,
HelpDescription: "Get descriptions of the available commands",
Handler: HandleHelp,
Help: commands.Help{
Summary: "Get descriptions of the available commands",
DetailedDescription: "Get descriptions of all the available commands.",
Syntax: "help [command]",
Example: "help ping",
},
}
}
// HandleHelp handles the help command.
func HandleHelp(msg string, s *discordgo.Session, m *discordgo.MessageCreate, db *sql.DB, guildID string, cmds []commands.Command) {
splitMsg := strings.Split(msg, " ")
if len(splitMsg) == 0 {
sendHelpMessage(s, m, cmds)
return
}
for _, cmd := range cmds {
if cmd.CallPhrase == splitMsg[0] {
response := discordgo.MessageEmbed{
Title: cmd.CallPhrase,
Color: infoColor,
}
var content strings.Builder
var desc string
if cmd.Help.DetailedDescription != "" {
desc = cmd.Help.DetailedDescription
} else {
desc = cmd.Help.Summary
}
content.WriteString(desc)
hasSyntax := cmd.Help.Syntax != ""
hasExample := cmd.Help.Example != ""
if hasSyntax || hasExample {
content.WriteString("\n\n")
if hasSyntax {
content.WriteString(fmt.Sprintf("Syntax: %v\n", cmd.Help.Syntax))
}
if hasExample {
content.WriteString(fmt.Sprintf("Example: %v\n", cmd.Help.Example))
}
}
response.Description = content.String()
_, err := s.ChannelMessageSendEmbed(m.ChannelID, &response)
if err != nil {
fmt.Println("Failed to send message:", err)
return
}
return
}
}
// If it gets here no command was found matching the request
response := discordgo.MessageEmbed{
Color: failColor,
Description: fmt.Sprintf("Command %q was not found", msg[1]),
}
_, err := s.ChannelMessageSendEmbed(m.ChannelID, &response)
if err != nil {
fmt.Println("Failed to send message:", err)
return
}
}
func sendHelpMessage(s *discordgo.Session, m *discordgo.MessageCreate, cmds []commands.Command) error {
msg := discordgo.MessageEmbed{
Title: "Command list",
Color: infoColor,
}
var content string
for _, cmd := range cmds {
description := cmd.Help.Summary
if description == "" {
description = "*No description available*"
}
content += fmt.Sprintf("%v - %v\n", cmd.CallPhrase, description)
}
msg.Description = content
_, err := s.ChannelMessageSendEmbed(m.ChannelID, &msg)
if err != nil {
return errors.Wrap(err, "failed to send message")
}
return nil
}