Skip to content

Commit

Permalink
added WithDefaultShardManager and WithDefaultGateway and tweaked clie…
Browse files Browse the repository at this point in the history
…nt building logic
  • Loading branch information
topi314 committed Jul 11, 2022
1 parent 036c102 commit 88af671
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
3 changes: 1 addition & 2 deletions _examples/application_commands/gateway/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/gateway"
"github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2"
)
Expand Down Expand Up @@ -45,7 +44,7 @@ func main() {
log.Info("disgo version: ", disgo.Version)

client, err := disgo.New(token,
bot.WithGatewayConfigOpts(gateway.WithIntents(gateway.IntentsNone)),
bot.WithDefaultGateway(),
bot.WithEventListenerFunc(commandListener),
)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions _examples/application_commands/localization/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/gateway"
"github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2"
)
Expand Down Expand Up @@ -69,7 +68,7 @@ func main() {
log.Infof("disgo version: %s", disgo.Version)

client, err := disgo.New(token,
bot.WithGatewayConfigOpts(gateway.WithIntents(gateway.IntentsNone)),
bot.WithDefaultGateway(),
bot.WithEventListenerFunc(commandListener),
)
if err != nil {
Expand Down
20 changes: 17 additions & 3 deletions bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ func WithGateway(gateway gateway.Gateway) ConfigOpt {
}
}

// WithDefaultGateway creates a gateway.Gateway with sensible defaults.
func WithDefaultGateway() ConfigOpt {
return func(config *Config) {
config.GatewayConfigOpts = append(config.GatewayConfigOpts, func(_ *gateway.Config) {})
}
}

// WithGatewayConfigOpts lets you configure the default gateway.Gateway.
func WithGatewayConfigOpts(opts ...gateway.ConfigOpt) ConfigOpt {
return func(config *Config) {
Expand All @@ -135,6 +142,13 @@ func WithShardManager(shardManager sharding.ShardManager) ConfigOpt {
}
}

// WithDefaultShardManager creates a sharding.ShardManager with sensible defaults.
func WithDefaultShardManager() ConfigOpt {
return func(config *Config) {
config.ShardManagerConfigOpts = append(config.ShardManagerConfigOpts, func(_ *sharding.Config) {})
}
}

// WithShardManagerConfigOpts lets you configure the default sharding.ShardManager.
func WithShardManagerConfigOpts(opts ...sharding.ConfigOpt) ConfigOpt {
return func(config *Config) {
Expand Down Expand Up @@ -224,7 +238,7 @@ func BuildClient(token string, config Config, gatewayEventHandlerFunc func(clien
}
client.eventManager = config.EventManager

if config.Gateway == nil && config.GatewayConfigOpts != nil {
if config.Gateway == nil && len(config.GatewayConfigOpts) > 0 {
var gatewayRs *discord.Gateway
gatewayRs, err = client.restServices.GetGateway()
if err != nil {
Expand All @@ -246,7 +260,7 @@ func BuildClient(token string, config Config, gatewayEventHandlerFunc func(clien
}
client.gateway = config.Gateway

if config.ShardManager == nil && config.ShardManagerConfigOpts != nil {
if config.ShardManager == nil && len(config.ShardManagerConfigOpts) > 0 {
var gatewayBotRs *discord.GatewayBot
gatewayBotRs, err = client.restServices.GetGatewayBot()
if err != nil {
Expand Down Expand Up @@ -281,7 +295,7 @@ func BuildClient(token string, config Config, gatewayEventHandlerFunc func(clien
}
client.shardManager = config.ShardManager

if config.HTTPServer == nil && config.PublicKey != "" && config.HTTPServerConfigOpts != nil {
if config.HTTPServer == nil && config.PublicKey != "" {
config.HTTPServerConfigOpts = append([]httpserver.ConfigOpt{
httpserver.WithLogger(client.logger),
}, config.HTTPServerConfigOpts...)
Expand Down

0 comments on commit 88af671

Please sign in to comment.