Skip to content

Commit

Permalink
update Activity struct and add emoji functions (bwmarrin#895)
Browse files Browse the repository at this point in the history
* update Activity struct and add emoji functions

* fix the emoji regex

* Remove inline type definitions

* Change function name

* fix message_test function name

* make custom unmarshaljson and change `CreatedAt` to `time.Time`

* fix

Co-authored-by: post <[email protected]>
  • Loading branch information
post04 and post04 authored Apr 10, 2021
1 parent a5f1f30 commit 4f55d76
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
18 changes: 18 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ type Message struct {
Flags MessageFlags `json:"flags"`
}

// GetCustomEmojis pulls out all the custom (Non-unicode) emojis from a message and returns a Slice of the Emoji struct.
func (m *Message) GetCustomEmojis() []*Emoji {
var toReturn []*Emoji
emojis := EmojiRegex.FindAllString(m.Content, -1)
if len(emojis) < 1 {
return toReturn
}
for _, em := range emojis {
parts := strings.Split(em, ":")
toReturn = append(toReturn, &Emoji{
ID: parts[2][:len(parts[2])-1],
Name: parts[1],
Animated: strings.HasPrefix(em, "<a:"),
})
}
return toReturn
}

// MessageFlags is the flags of "message" (see MessageFlags* consts)
// https://discord.com/developers/docs/resources/channel#message-object-message-flags
type MessageFlags int
Expand Down
12 changes: 12 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,15 @@ func TestContentWithMoreMentionsReplaced(t *testing.T) {
t.Error(result)
}
}
func TestGettingEmojisFromMessage(t *testing.T) {
msg := "test test <:kitty14:811736565172011058> <:kitty4:811736468812595260>"
m := &Message{
Content: msg,
}
emojis := m.GetCustomEmojis()
if len(emojis) < 1 {
t.Error("No emojis found.")
return
}

}
77 changes: 74 additions & 3 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"fmt"
"math"
"net/http"
"regexp"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -353,6 +354,11 @@ type Emoji struct {
Available bool `json:"available"`
}

// EmojiRegex is the regex used to find and identify emojis in messages
var (
EmojiRegex = regexp.MustCompile(`<(a|):[A-z0-9_~]+:[0-9]{18}>`)
)

// MessageFormat returns a correctly formatted Emoji for use in Message content and embeds
func (e *Emoji) MessageFormat() string {
if e.ID != "" && e.Name != "" {
Expand Down Expand Up @@ -1114,9 +1120,74 @@ type GatewayStatusUpdate struct {
// Activity defines the Activity sent with GatewayStatusUpdate
// https://discord.com/developers/docs/topics/gateway#activity-object
type Activity struct {
Name string `json:"name"`
Type ActivityType `json:"type"`
URL string `json:"url,omitempty"`
Name string `json:"name"`
Type ActivityType `json:"type"`
URL string `json:"url,omitempty"`
CreatedAt time.Time `json:"created_at"`
ApplicationID string `json:"application_id,omitempty"`
State string `json:"state,omitempty"`
Details string `json:"details,omitempty"`
Timestamps TimeStamps `json:"timestamps,omitempty"`
Emoji Emoji `json:"emoji,omitempty"`
Party Party `json:"party,omitempty"`
Assets Assets `json:"assets,omitempty"`
Secrets Secrets `json:"secrets,omitempty"`
Instance bool `json:"instance,omitempty"`
Flags int `json:"flags,omitempty"`
}

// UnmarshalJSON is a custom unmarshaljson to make CreatedAt a time.Time instead of an int
func (activity *Activity) UnmarshalJSON(b []byte) error {
temp := struct {
Name string `json:"name"`
Type ActivityType `json:"type"`
URL string `json:"url,omitempty"`
CreatedAt int64 `json:"created_at"`
ApplicationID string `json:"application_id,omitempty"`
State string `json:"state,omitempty"`
Details string `json:"details,omitempty"`
Timestamps TimeStamps `json:"timestamps,omitempty"`
Emoji Emoji `json:"emoji,omitempty"`
Party Party `json:"party,omitempty"`
Assets Assets `json:"assets,omitempty"`
Secrets Secrets `json:"secrets,omitempty"`
Instance bool `json:"instance,omitempty"`
Flags int `json:"flags,omitempty"`
}{}
err := json.Unmarshal(b, &temp)
if err != nil {
return err
}
activity.CreatedAt = time.Unix(0, temp.CreatedAt*1000000)
activity.ApplicationID = temp.ApplicationID
activity.Assets = temp.Assets
activity.Details = temp.Details
activity.Emoji = temp.Emoji
activity.Flags = temp.Flags
activity.Instance = temp.Instance
activity.Name = temp.Name
activity.Party = temp.Party
activity.Secrets = temp.Secrets
activity.State = temp.State
activity.Timestamps = temp.Timestamps
activity.Type = temp.Type
activity.URL = temp.URL
return nil
}

// Party defines the Party field in the Activity struct
// https://discord.com/developers/docs/topics/gateway#activity-object
type Party struct {
ID string `json:"id,omitempty"`
Size []int `json:"size,omitempty"`
}

// Secrets defines the Secrets field for the Activity struct
// https://discord.com/developers/docs/topics/gateway#activity-object
type Secrets struct {
Join string `json:"join,omitempty"`
Spectate string `json:"spectate,omitempty"`
Match string `json:"match,omitempty"`
}

// ActivityType is the type of Activity (see ActivityType* consts) in the Activity struct
Expand Down

0 comments on commit 4f55d76

Please sign in to comment.