-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
shards.go
152 lines (120 loc) · 3.76 KB
/
shards.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package shards
import (
"fmt"
"sync"
"time"
"github.com/bwmarrin/discordgo"
)
const (
// TIMELIMIT specifies how long to pause between connecting shards.
TIMELIMIT = time.Second * 5
// VERSION specifies the shards module version. Follows semantic versioning (semver.org).
VERSION = "2.5.0"
)
// A Shard represents a shard.
type Shard struct {
sync.RWMutex
// The Discord session handling this Shard.
Session *discordgo.Session
// This Shard's ID.
ID int
// Total Shard count.
ShardCount int
// Event handlers.
handlers []interface{}
}
// AddHandler registers an event handler for a Shard.
//
// Shouldn't be called after Init or results in undefined behavior.
func (s *Shard) AddHandler(handler interface{}) {
s.Lock()
defer s.Unlock()
s.handlers = append(s.handlers, handler)
}
// ApplicationCommandCreate registers an application command for a Shard.
//
// Shouldn't be called before Initialization.
func (s *Shard) ApplicationCommandCreate(guildID string, cmd *discordgo.ApplicationCommand) error {
s.Lock()
defer s.Unlock()
// Referencing s.Session before Initialization will result in a nil pointer dereference panic.
if s.Session == nil {
return fmt.Errorf("error: shard.ApplicationCommandCreate must not be called before shard.Init")
}
_, err := s.Session.ApplicationCommandCreate(s.Session.State.User.ID, guildID, cmd)
return err
}
// ApplicationCommandBulkOverwrite registers a series of application commands for a Shard,
// overwriting existing commands.
//
// Shouldn't be called before Initialization.
func (s *Shard) ApplicationCommandBulkOverwrite(guildID string, cmds []*discordgo.ApplicationCommand) error {
s.Lock()
defer s.Unlock()
// Referencing s.Session before Initialization will result in a nil pointer dereference panic.
if s.Session == nil {
return fmt.Errorf("error: shard.ApplicationCommandCreate must not be called before shard.Init")
}
_, err := s.Session.ApplicationCommandBulkOverwrite(s.Session.State.User.ID, guildID, cmds)
return err
}
// ApplicationCommandDelete deregisters an application command for a Shard.
//
// Shouldn't be called before Initialization.
func (s *Shard) ApplicationCommandDelete(guildID string, cmd *discordgo.ApplicationCommand) error {
s.Lock()
defer s.Unlock()
// Referencing s.Session before Initialization will result in a nil pointer dereference panic.
if s.Session == nil {
return fmt.Errorf("error: shard.ApplicationCommandCreate must not be called before shard.Init")
}
return s.Session.ApplicationCommandDelete(s.Session.State.User.ID, guildID, cmd.ID)
}
// GuildCount returns the amount of guilds that a Shard is handling.
func (s *Shard) GuildCount() int {
s.RLock()
defer s.RUnlock()
if s.Session != nil {
s.Session.State.RLock()
defer s.Session.State.RUnlock()
return len(s.Session.State.Guilds)
}
return 0
}
// Init initializes a shard with a bot token, its Shard ID, the total
// amount of shards, and a Discord intent.
func (s *Shard) Init(token string, ID, ShardCount int, intent discordgo.Intent, stateEnabled bool) (err error) {
s.Lock()
defer s.Unlock()
// Apply sharding configuration.
s.ID = ID
s.ShardCount = ShardCount
// Create the session.
s.Session, err = discordgo.New(token)
if err != nil {
return
}
// Shard the session.
s.Session.ShardCount = s.ShardCount
s.Session.ShardID = s.ID
// Configure state tracking.
s.Session.StateEnabled = stateEnabled
// Identify our intent.
s.Session.Identify.Intents = intent
// Add handlers to the session.
for _, handler := range s.handlers {
s.Session.AddHandler(handler)
}
// Connect the shard.
err = s.Session.Open()
return
}
// Stop stops a shard.
func (s *Shard) Stop() error {
s.Lock()
defer s.Unlock()
if s.Session == nil {
return fmt.Errorf("error: shard not initialized")
}
return s.Session.Close()
}