Skip to content

Commit

Permalink
add activity opts
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Aug 10, 2023
1 parent 76d3e4d commit aab01e1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 36 deletions.
2 changes: 1 addition & 1 deletion _examples/test/examplebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
client, err := disgo.New(token,
bot.WithGatewayConfigOpts(
gateway.WithIntents(gateway.IntentsNonPrivileged, gateway.IntentMessageContent),
gateway.WithPresenceOpts(gateway.WithListeningActivity("your bullshit"), gateway.WithOnlineStatus(discord.OnlineStatusDND)),
gateway.WithPresenceOpts(gateway.WithListeningActivity("your bullshit", discord.WithActivityState("lol")), gateway.WithOnlineStatus(discord.OnlineStatusDND)),
),
bot.WithCacheConfigOpts(
cache.WithCaches(cache.FlagsAll),
Expand Down
64 changes: 64 additions & 0 deletions discord/activity_opts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package discord

// DefaultActivityConfig returns a ActivityConfig with sensible defaults.
func DefaultActivityConfig(activityType ActivityType, name string, url *string) *ActivityConfig {
return &ActivityConfig{
Name: name,
Type: activityType,
URL: url,
}
}

// ActivityConfig lets you configure an Activity.
type ActivityConfig struct {
Name string
Type ActivityType
URL *string
State *string
}

// ActivityConfigOpt is a type alias for a function that takes a ActivityConfig and is used to configure an Activity.
type ActivityConfigOpt func(config *ActivityConfig)

// Apply applies the given ActivityConfigOpt(s) to the ActivityConfig
func (c *ActivityConfig) Apply(opts []ActivityConfigOpt) {
for _, opt := range opts {
opt(c)
}
}

// WithActivityURL sets the URL of the Activity.
func WithActivityURL(url string) ActivityConfigOpt {
return func(config *ActivityConfig) {
if url == "" {
config.URL = nil
return
}
config.URL = &url
}
}

// WithActivityState sets the State of the Activity.
func WithActivityState(state string) ActivityConfigOpt {
return func(config *ActivityConfig) {
if state == "" {
config.State = nil
return
}
config.State = &state
}
}

// WithActivityName sets the Name of the Activity.
func WithActivityName(name string) ActivityConfigOpt {
return func(config *ActivityConfig) {
config.Name = name
}
}

// WithActivityType sets the Type of the Activity.
func WithActivityType(activityType ActivityType) ActivityConfigOpt {
return func(config *ActivityConfig) {
config.Type = activityType
}
}
59 changes: 24 additions & 35 deletions gateway/gateway_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,61 +461,50 @@ func (MessageDataPresenceUpdate) messageData() {}
type PresenceOpt func(presenceUpdate *MessageDataPresenceUpdate)

// WithPlayingActivity creates a new "Playing ..." activity of type discord.ActivityTypeGame
func WithPlayingActivity(name string) PresenceOpt {
return withActivity(discord.Activity{
Name: name,
Type: discord.ActivityTypeGame,
})
func WithPlayingActivity(name string, opts ...discord.ActivityConfigOpt) PresenceOpt {
return withActivity(discord.ActivityTypeGame, name, nil, nil, opts...)
}

// WithStreamingActivity creates a new "Streaming ..." activity of type discord.ActivityTypeStreaming
func WithStreamingActivity(name string, url string) PresenceOpt {
activity := discord.Activity{
Name: name,
Type: discord.ActivityTypeStreaming,
}
func WithStreamingActivity(name string, url string, opts ...discord.ActivityConfigOpt) PresenceOpt {
var finalURL *string
if url != "" {
activity.URL = &url
finalURL = &url
}
return withActivity(activity)
return withActivity(discord.ActivityTypeStreaming, name, finalURL, nil, opts...)
}

// WithListeningActivity creates a new "Listening to ..." activity of type discord.ActivityTypeListening
func WithListeningActivity(name string) PresenceOpt {
return withActivity(discord.Activity{
Name: name,
Type: discord.ActivityTypeListening,
})
func WithListeningActivity(name string, opts ...discord.ActivityConfigOpt) PresenceOpt {
return withActivity(discord.ActivityTypeListening, name, nil, nil, opts...)
}

// WithWatchingActivity creates a new "Watching ..." activity of type discord.ActivityTypeWatching
func WithWatchingActivity(name string) PresenceOpt {
return withActivity(discord.Activity{
Name: name,
Type: discord.ActivityTypeWatching,
})
func WithWatchingActivity(name string, opts ...discord.ActivityConfigOpt) PresenceOpt {
return withActivity(discord.ActivityTypeWatching, name, nil, nil, opts...)
}

// WithCustomActivity creates a new activity of type discord.ActivityTypeCustom
func WithCustomActivity(status string) PresenceOpt {
return withActivity(discord.Activity{
Name: "Custom Status",
Type: discord.ActivityTypeCustom,
State: &status,
})
func WithCustomActivity(state string, opts ...discord.ActivityConfigOpt) PresenceOpt {
return withActivity(discord.ActivityTypeCustom, "Custom Status", nil, nil, opts...)
}

// WithCompetingActivity creates a new "Competing in ..." activity of type discord.ActivityTypeCompeting
func WithCompetingActivity(name string) PresenceOpt {
return withActivity(discord.Activity{
Name: name,
Type: discord.ActivityTypeCompeting,
})
func WithCompetingActivity(name string, opts ...discord.ActivityConfigOpt) PresenceOpt {
return withActivity(discord.ActivityTypeCompeting, name, nil, nil, opts...)
}

func withActivity(activity discord.Activity) PresenceOpt {
func withActivity(activityType discord.ActivityType, name string, url *string, state *string, opts ...discord.ActivityConfigOpt) PresenceOpt {
cfg := discord.DefaultActivityConfig(activityType, name, url)
cfg.Apply(opts)

return func(presence *MessageDataPresenceUpdate) {
presence.Activities = []discord.Activity{activity}
presence.Activities = append(presence.Activities, discord.Activity{
Name: cfg.Name,
Type: cfg.Type,
URL: cfg.URL,
State: cfg.State,
})
}
}

Expand Down

0 comments on commit aab01e1

Please sign in to comment.