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 3
/
commands.go
97 lines (78 loc) · 2.15 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
package main
import (
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
)
var (
activeCommands = make(map[string]command)
disabledCommands = make(map[string]command)
)
type command struct {
Name string
Help string
OwnerOnly bool
RequiresPerms bool
PermsRequired int
Exec func(*discordgo.Session, *discordgo.MessageCreate, []string)
}
/*
go-chi style command adding
- aliases
- nicer help text
- uh...MORE
also more here!
*/
func parseCommand(s *discordgo.Session, m *discordgo.MessageCreate, guildDetails *discordgo.Guild, message string) {
msglist := strings.Fields(message)
if len(msglist) == 0 {
return
}
log.Trace(fmt.Sprintf("%s %s#%s, %s %s: %s", m.Author.ID, m.Author.Username, m.Author.Discriminator, guildDetails.ID, guildDetails.Name, m.Content))
commandName := strings.ToLower(func() string {
if strings.HasPrefix(message, " ") {
return " " + msglist[0]
}
return msglist[0]
}())
if command, ok := activeCommands[commandName]; ok && commandName == strings.ToLower(command.Name) {
userPerms, err := permissionDetails(m.Author.ID, m.ChannelID, s)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Error verifying permissions :(")
return
}
isOwner := m.Author.ID == conf.OwnerID
hasPerms := userPerms&command.PermsRequired > 0
if (!command.OwnerOnly && !command.RequiresPerms) || (command.RequiresPerms && hasPerms) || isOwner {
command.Exec(s, m, msglist)
return
}
s.ChannelMessageSend(m.ChannelID, "You don't have the correct permissions to run this!")
return
}
activeCommands["bigmoji"].Exec(s, m, msglist)
}
func (c command) add() command {
activeCommands[strings.ToLower(c.Name)] = c
return c
}
func newCommand(name string, permissions int, needsPerms bool, f func(*discordgo.Session, *discordgo.MessageCreate, []string)) command {
return command{
Name: name,
PermsRequired: permissions,
RequiresPerms: needsPerms,
Exec: f,
}
}
func (c command) alias(a string) command {
activeCommands[strings.ToLower(a)] = c
return c
}
func (c command) setHelp(help string) command {
c.Help = help
return c
}
func (c command) ownerOnly() command {
c.OwnerOnly = true
return c
}