-
Notifications
You must be signed in to change notification settings - Fork 92
/
commandHandler.go
134 lines (120 loc) · 4.53 KB
/
commandHandler.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
128
129
130
131
132
133
134
package main
import (
"fmt"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/post04/dr-docso/bot"
)
// New creates an initialized commandhandler
func New(prefix string, ignoreBots bool) *CommandHandler {
return &CommandHandler{
Prefix: prefix,
Commands: make(map[string]*Command),
IgnoreBots: ignoreBots,
}
}
// AddCommand adds a new command to command handler
func (handler *CommandHandler) AddCommand(name string, help string, description string, commandHandler func(s *discordgo.Session, m *discordgo.MessageCreate, prefix string)) {
help = strings.ReplaceAll(help, "{prefix}", handler.Prefix)
handler.Commands[name] = &Command{
Run: commandHandler,
Help: help,
Description: description,
}
}
// GenHelp generates the help command output.
func (handler *CommandHandler) GenHelp() {
handler.TimeStarted = time.Now()
var longestCommand int
for name := range handler.Commands {
if len(name) > longestCommand {
longestCommand = len(name)
}
}
// Build the description
embedDesc := strings.Builder{}
embedDesc.WriteString("```autoit\n")
for name := range handler.Commands {
embedDesc.WriteString(handler.Prefix)
embedDesc.WriteString(name)
embedDesc.WriteString(strings.Repeat(" ", (longestCommand-len(name))+1))
embedDesc.WriteString("#")
embedDesc.WriteString(handler.Commands[name].Description)
embedDesc.WriteRune('\n')
}
embedDesc.WriteString("```")
handler.HelpCommand = &discordgo.MessageEmbed{
Footer: &discordgo.MessageEmbedFooter{
Text: fmt.Sprintf("Use %shelp commandName to get more info about a command", handler.Prefix),
},
Title: "Available commands",
Description: embedDesc.String(),
}
}
// OnMessage handles onmessage event from discordgo for command handler.
func (handler *CommandHandler) OnMessage(session *discordgo.Session, msg *discordgo.MessageCreate) {
parts := strings.Fields(msg.Content)
if handler.OnMessageHandler != nil {
go handler.OnMessageHandler(session, msg)
}
if msg.Author.Bot && handler.IgnoreBots {
return
}
if len(parts) < 1 || !strings.HasPrefix(parts[0], handler.Prefix) {
return
}
if handler.Middleware != nil {
if !handler.Middleware(session, msg) {
return
}
}
cmd := strings.ToLower(strings.TrimPrefix(parts[0], handler.Prefix))
switch cmd {
case "help":
fmt.Println("help command ran by " + msg.Author.Username + "#" + msg.Author.Discriminator + " in " + msg.ChannelID)
if len(parts) == 1 {
session.ChannelMessageSendEmbed(msg.ChannelID, handler.HelpCommand)
} else {
embed := &discordgo.MessageEmbed{}
if command, ok := handler.Commands[strings.ToLower(parts[1])]; ok {
embed.Description = fmt.Sprintf("Name: %s\n"+
"example: %s\n"+
"description: %s",
strings.ToLower(parts[1]),
command.Help,
command.Description)
embed.Title = strings.ToLower(parts[1]) + " Command"
session.ChannelMessageSendEmbed(msg.ChannelID, embed)
} else {
embed.Title = "Unknown command"
embed.Description = fmt.Sprintf("%q is not a valid command; use %shelp to get available commands.", parts[1], handler.Prefix)
session.ChannelMessageSendEmbed(msg.ChannelID, embed)
}
}
case "info":
fmt.Println("info command ran by " + msg.Author.Username + "#" + msg.Author.Discriminator + " in " + msg.ChannelID)
session.ChannelMessageSendEmbed(msg.ChannelID, &discordgo.MessageEmbed{
Title: "dr-docso by post and insomnia",
Description: fmt.Sprintf("Library: [DiscordGo](https://github.com/bwmarrin/discordgo)\nUptime: <t:%d:R>\nPrefix: %s\nGithub Repo: [here](https://github.com/post04/dr-docso)\nInvite: [Click me](https://discord.com/oauth2/authorize?client_id=817416218390560798&permissions=3221613648&scope=bot)", handler.TimeStarted.Unix(), handler.Prefix),
})
default:
if command, ok := handler.Commands[cmd]; ok {
fmt.Println(parts[0][len(handler.Prefix):] + " command ran by " + msg.Author.Username + "#" + msg.Author.Discriminator + " in " + msg.ChannelID)
go command.Run(session, msg, handler.Prefix)
}
}
}
// OnEdit handles onedit event from discordgo for command handler.
func (handler *CommandHandler) OnEdit(session *discordgo.Session, msg *discordgo.MessageUpdate) {
parts := strings.Fields(msg.Content)
if len(parts) < 1 || !strings.HasPrefix(parts[0], handler.Prefix) {
return
}
cmd := strings.ToLower(strings.TrimPrefix(parts[0], handler.Prefix))
if cmd != "docs" {
return
}
fmt.Println(parts[0][len(handler.Prefix):] + " command edit by " + msg.Author.Username + "#" + msg.Author.Discriminator + " in " + msg.ChannelID)
bot.HandleDocUpdate(session, msg)
}