-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslashcommand.go
108 lines (87 loc) · 3.03 KB
/
slashcommand.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
package harmonia
import (
"log"
"regexp"
"github.com/bwmarrin/discordgo"
)
var slashCommandNameRegex = regexp.MustCompile(`^[-_\p{L}\p{N}]{1,32}$`)
// A SlashCommand describes a slash command or CHAT_INPUT application command.
type SlashCommand struct {
name string
description string
guildID string
dmPermission bool
defaultPermissions *int64
commandFunc CommandFunc
options []*Option
registration *discordgo.ApplicationCommand
}
// NewSlashCommand returns a SlashCommand with a given name
func NewSlashCommand(name string) *SlashCommand {
if name == "" {
log.Panic("empty command name")
}
if !slashCommandNameRegex.MatchString(name) {
log.Panic("slash command name does not match with the CHAT_INPUT regex.")
}
return &SlashCommand{
name: name,
}
}
// WithDescription changes the description of the SlashCommand and returns itself, so that it can be chained.
func (s *SlashCommand) WithDescription(description string) *SlashCommand {
s.description = description
return s
}
// WithGuildID changes the guildID of the SlashCommand and returns itself, so that it can be chained.
func (s *SlashCommand) WithGuildID(guildID string) *SlashCommand {
s.guildID = guildID
return s
}
// WithDMPermission changes the DM Permission of the SlashCommand and returns itself, so that it can be chained.
func (s *SlashCommand) WithDMPermission(isAllowed bool) *SlashCommand {
s.dmPermission = isAllowed
return s
}
// WithDefaultPermissions changes the DefaultPermissions of the SlashCommand and returns itself, so that it can be chained.
func (s *SlashCommand) WithDefaultPermissions(defaultPermissions int64) *SlashCommand {
s.defaultPermissions = &defaultPermissions
return s
}
// WithCommand changes the CommandFunc that is called when the SlashCommand is executed and returns itself, so that it can be chained.
func (s *SlashCommand) WithCommand(commandFunc CommandFunc) *SlashCommand {
s.commandFunc = commandFunc
return s
}
// WithOptions changes the options in the SlashCommand and returns itself, so that it can be chained.
func (s *SlashCommand) WithOptions(options ...*Option) *SlashCommand {
s.options = options
return s
}
func (s *SlashCommand) GetName() string {
return s.name
}
func (s *SlashCommand) Do(h *Harmonia, i *Invocation) {
go s.commandFunc(h, i)
}
func (s *SlashCommand) getRegistration() *discordgo.ApplicationCommand {
if s.registration != nil {
return s.registration
}
options := make([]*discordgo.ApplicationCommandOption, len(s.options))
for i, v := range s.options {
options[i] = v.ApplicationCommandOption
}
return &discordgo.ApplicationCommand{
Name: s.name,
Description: s.description,
GuildID: s.guildID,
Options: options,
DMPermission: &s.dmPermission,
DefaultMemberPermissions: s.defaultPermissions,
Type: discordgo.ChatApplicationCommand,
}
}
func (s *SlashCommand) setRegistration(registration *discordgo.ApplicationCommand) {
s.registration = registration
}