-
Notifications
You must be signed in to change notification settings - Fork 0
/
sevcord.go
119 lines (101 loc) · 3.04 KB
/
sevcord.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
package sevcord
// TODO: Locale support (note that it needs to be in User info too)
import (
"log"
"sync"
"github.com/bwmarrin/discordgo"
)
var Logger = log.Default()
type componentHandler struct {
handlers map[string]any // map[componentid]handler
followup *string
}
type modalHandler struct {
handler ModalHandler
followup *string
}
type Client struct {
dg *discordgo.Session
commands map[string]SlashCommandObject
contextMenus map[string]*ContextMenuCommand
mHandler *MessageHandler
componentHandlers map[string]componentHandler // map[interactionid]handler
modalHandlers map[string]modalHandler // map[interactionid]handler
lock *sync.RWMutex
}
type ClientParams struct {
Messages bool // Whether to listen for messages
Reactions bool // Whether to listen for reactions
}
func NewClient(token string, params *ClientParams) (*Client, error) {
dg, err := discordgo.New("Bot " + token)
if err != nil {
return nil, err
}
dg.Identify.Intents = discordgo.IntentsNone // TODO: Detect for message content intent needed/reaction intents needed
if params != nil {
if params.Messages {
dg.Identify.Intents |= discordgo.IntentGuildMessages | discordgo.IntentMessageContent
}
if params.Reactions {
dg.Identify.Intents |= discordgo.IntentGuildMessageReactions
}
}
err = dg.Open()
if err != nil {
return nil, err
}
return &Client{
dg: dg,
commands: make(map[string]SlashCommandObject),
contextMenus: make(map[string]*ContextMenuCommand),
componentHandlers: make(map[string]componentHandler),
modalHandlers: make(map[string]modalHandler),
lock: &sync.RWMutex{},
}, nil
}
type SlashCommandObject interface {
name() string
build() *discordgo.ApplicationCommandOption
isGroup() bool
permissions() *int64
}
func (c *Client) HandleSlashCommand(cmd SlashCommandObject) {
c.commands[cmd.name()] = cmd
}
func (c *Client) HandleContextMenuCommand(cmd *ContextMenuCommand) {
c.contextMenus[cmd.Name] = cmd
}
// HandleMessage updates the internal message handler. NOTE: This will replace the previous handler if there was one
func (c *Client) HandleMessage(m MessageHandler) {
c.mHandler = &m
}
func (c *Client) Start() error {
c.dg.AddHandler(c.interactionHandler)
if c.mHandler != nil {
c.dg.AddHandler(c.messageHandler)
}
// Build commands
cmds := make([]*discordgo.ApplicationCommand, 0, len(c.commands))
for _, cmd := range c.commands {
v := cmd.build()
cmds = append(cmds, &discordgo.ApplicationCommand{
Name: v.Name,
Description: v.Description,
Options: v.Options,
Type: discordgo.ChatApplicationCommand,
DefaultMemberPermissions: cmd.permissions(),
})
}
for _, cmd := range c.contextMenus {
cmds = append(cmds, &discordgo.ApplicationCommand{
Name: cmd.Name,
Type: discordgo.ApplicationCommandType(cmd.Kind),
})
}
_, err := c.dg.ApplicationCommandBulkOverwrite(c.dg.State.User.ID, "", cmds)
return err
}
func (c *Client) Close() error {
return c.dg.Close()
}