Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve presence UX #196

Merged
merged 9 commits into from Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion _examples/test/examplebot.go
Expand Up @@ -33,7 +33,7 @@ func main() {
client, err := disgo.New(token,
bot.WithGatewayConfigOpts(
gateway.WithIntents(gateway.IntentsNonPrivileged, gateway.IntentMessageContent),
gateway.WithPresence(gateway.NewListeningPresence("your bullshit", discord.OnlineStatusOnline, false)),
gateway.WithPresenceOpts(gateway.WithListeningActivity("your bullshit"), gateway.WithOnlineStatus(discord.OnlineStatusDND)),
),
bot.WithCacheConfigOpts(
cache.WithCacheFlags(cache.FlagsAll),
Expand Down
12 changes: 9 additions & 3 deletions gateway/gateway_config.go
Expand Up @@ -60,6 +60,8 @@ func (c *Config) Apply(opts []ConfigOpt) {
}
}

type PresenceOpt func(presenceUpdate *MessageDataPresenceUpdate)
mlnrDev marked this conversation as resolved.
Show resolved Hide resolved

// WithLogger sets the Logger for the Gateway.
func WithLogger(logger log.Logger) ConfigOpt {
return func(config *Config) {
Expand Down Expand Up @@ -179,10 +181,14 @@ func WithRateRateLimiterConfigOpts(opts ...RateLimiterConfigOpt) ConfigOpt {
}
}

// WithPresence sets the initial presence the bot should display.
func WithPresence(presence MessageDataPresenceUpdate) ConfigOpt {
// WithPresenceOpts allows to pass initial presence data the bot should display.
func WithPresenceOpts(opts ...PresenceOpt) ConfigOpt {
return func(config *Config) {
config.Presence = &presence
presence := &MessageDataPresenceUpdate{}
for _, opt := range opts {
opt(presence)
}
config.Presence = presence
}
}

Expand Down
86 changes: 48 additions & 38 deletions gateway/gateway_messages.go
Expand Up @@ -437,57 +437,67 @@ type IdentifyCommandDataProperties struct {
Device string `json:"device"` // library name
}

// NewPresence creates a new Presence with the provided properties
func NewPresence(activityType discord.ActivityType, name string, url string, status discord.OnlineStatus, afk bool) MessageDataPresenceUpdate {
var since *int64
if status == discord.OnlineStatusIdle {
unix := time.Now().Unix()
since = &unix
}
func WithPlayingActivity(name string) PresenceOpt {
topi314 marked this conversation as resolved.
Show resolved Hide resolved
return withActivity(discord.Activity{
Name: name,
Type: discord.ActivityTypeGame,
})
}

var activities []discord.Activity
if name != "" {
activity := discord.Activity{
Name: name,
Type: activityType,
}
if activityType == discord.ActivityTypeStreaming && url != "" {
activity.URL = &url
}
activities = append(activities, activity)
func WithStreamingActivity(name string, url string) PresenceOpt {
activity := discord.Activity{
Name: name,
Type: discord.ActivityTypeStreaming,
}

return MessageDataPresenceUpdate{
Since: since,
Activities: activities,
Status: status,
AFK: afk,
if url != "" {
activity.URL = &url
}
return withActivity(activity)
}

// NewGamePresence creates a new Presence of type ActivityTypeGame
func NewGamePresence(name string, status discord.OnlineStatus, afk bool) MessageDataPresenceUpdate {
return NewPresence(discord.ActivityTypeGame, name, "", status, afk)
func WithListeningActivity(name string) PresenceOpt {
return withActivity(discord.Activity{
Name: name,
Type: discord.ActivityTypeListening,
})
}

// NewStreamingPresence creates a new Presence of type ActivityTypeStreaming
func NewStreamingPresence(name string, url string, status discord.OnlineStatus, afk bool) MessageDataPresenceUpdate {
return NewPresence(discord.ActivityTypeStreaming, name, url, status, afk)
func WithWatchingActivity(name string) PresenceOpt {
return withActivity(discord.Activity{
Name: name,
Type: discord.ActivityTypeWatching,
})
}

// NewListeningPresence creates a new Presence of type ActivityTypeListening
func NewListeningPresence(name string, status discord.OnlineStatus, afk bool) MessageDataPresenceUpdate {
return NewPresence(discord.ActivityTypeListening, name, "", status, afk)
func WithCompetingActivity(name string) PresenceOpt {
return withActivity(discord.Activity{
Name: name,
Type: discord.ActivityTypeCompeting,
})
}

// NewWatchingPresence creates a new Presence of type ActivityTypeWatching
func NewWatchingPresence(name string, status discord.OnlineStatus, afk bool) MessageDataPresenceUpdate {
return NewPresence(discord.ActivityTypeWatching, name, "", status, afk)
func withActivity(activity discord.Activity) PresenceOpt {
return func(presence *MessageDataPresenceUpdate) {
presence.Activities = append(presence.Activities, activity)
}
}

// NewCompetingPresence creates a new Presence of type ActivityTypeCompeting
func NewCompetingPresence(name string, status discord.OnlineStatus, afk bool) MessageDataPresenceUpdate {
return NewPresence(discord.ActivityTypeCompeting, name, "", status, afk)
func WithOnlineStatus(status discord.OnlineStatus) PresenceOpt {
return func(presence *MessageDataPresenceUpdate) {
presence.Status = status
if status == discord.OnlineStatusIdle {
var since *int64
unix := time.Now().Unix()
since = &unix
presence.Since = since
mlnrDev marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

func WithAfk(afk bool) PresenceOpt {
return func(presence *MessageDataPresenceUpdate) {
presence.AFK = afk
}
}

// MessageDataPresenceUpdate is used for updating Client's presence
Expand Down