Skip to content

Commit 4f55d76

Browse files
authored
update Activity struct and add emoji functions (bwmarrin#895)
* 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 <61803796+postrequest69@users.noreply.github.com>
1 parent a5f1f30 commit 4f55d76

File tree

3 files changed

+104
-3
lines changed

3 files changed

+104
-3
lines changed

message.go

+18
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,24 @@ type Message struct {
125125
Flags MessageFlags `json:"flags"`
126126
}
127127

128+
// GetCustomEmojis pulls out all the custom (Non-unicode) emojis from a message and returns a Slice of the Emoji struct.
129+
func (m *Message) GetCustomEmojis() []*Emoji {
130+
var toReturn []*Emoji
131+
emojis := EmojiRegex.FindAllString(m.Content, -1)
132+
if len(emojis) < 1 {
133+
return toReturn
134+
}
135+
for _, em := range emojis {
136+
parts := strings.Split(em, ":")
137+
toReturn = append(toReturn, &Emoji{
138+
ID: parts[2][:len(parts[2])-1],
139+
Name: parts[1],
140+
Animated: strings.HasPrefix(em, "<a:"),
141+
})
142+
}
143+
return toReturn
144+
}
145+
128146
// MessageFlags is the flags of "message" (see MessageFlags* consts)
129147
// https://discord.com/developers/docs/resources/channel#message-object-message-flags
130148
type MessageFlags int

message_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,15 @@ func TestContentWithMoreMentionsReplaced(t *testing.T) {
3838
t.Error(result)
3939
}
4040
}
41+
func TestGettingEmojisFromMessage(t *testing.T) {
42+
msg := "test test <:kitty14:811736565172011058> <:kitty4:811736468812595260>"
43+
m := &Message{
44+
Content: msg,
45+
}
46+
emojis := m.GetCustomEmojis()
47+
if len(emojis) < 1 {
48+
t.Error("No emojis found.")
49+
return
50+
}
51+
52+
}

structs.go

+74-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"fmt"
1717
"math"
1818
"net/http"
19+
"regexp"
1920
"strings"
2021
"sync"
2122
"time"
@@ -353,6 +354,11 @@ type Emoji struct {
353354
Available bool `json:"available"`
354355
}
355356

357+
// EmojiRegex is the regex used to find and identify emojis in messages
358+
var (
359+
EmojiRegex = regexp.MustCompile(`<(a|):[A-z0-9_~]+:[0-9]{18}>`)
360+
)
361+
356362
// MessageFormat returns a correctly formatted Emoji for use in Message content and embeds
357363
func (e *Emoji) MessageFormat() string {
358364
if e.ID != "" && e.Name != "" {
@@ -1114,9 +1120,74 @@ type GatewayStatusUpdate struct {
11141120
// Activity defines the Activity sent with GatewayStatusUpdate
11151121
// https://discord.com/developers/docs/topics/gateway#activity-object
11161122
type Activity struct {
1117-
Name string `json:"name"`
1118-
Type ActivityType `json:"type"`
1119-
URL string `json:"url,omitempty"`
1123+
Name string `json:"name"`
1124+
Type ActivityType `json:"type"`
1125+
URL string `json:"url,omitempty"`
1126+
CreatedAt time.Time `json:"created_at"`
1127+
ApplicationID string `json:"application_id,omitempty"`
1128+
State string `json:"state,omitempty"`
1129+
Details string `json:"details,omitempty"`
1130+
Timestamps TimeStamps `json:"timestamps,omitempty"`
1131+
Emoji Emoji `json:"emoji,omitempty"`
1132+
Party Party `json:"party,omitempty"`
1133+
Assets Assets `json:"assets,omitempty"`
1134+
Secrets Secrets `json:"secrets,omitempty"`
1135+
Instance bool `json:"instance,omitempty"`
1136+
Flags int `json:"flags,omitempty"`
1137+
}
1138+
1139+
// UnmarshalJSON is a custom unmarshaljson to make CreatedAt a time.Time instead of an int
1140+
func (activity *Activity) UnmarshalJSON(b []byte) error {
1141+
temp := struct {
1142+
Name string `json:"name"`
1143+
Type ActivityType `json:"type"`
1144+
URL string `json:"url,omitempty"`
1145+
CreatedAt int64 `json:"created_at"`
1146+
ApplicationID string `json:"application_id,omitempty"`
1147+
State string `json:"state,omitempty"`
1148+
Details string `json:"details,omitempty"`
1149+
Timestamps TimeStamps `json:"timestamps,omitempty"`
1150+
Emoji Emoji `json:"emoji,omitempty"`
1151+
Party Party `json:"party,omitempty"`
1152+
Assets Assets `json:"assets,omitempty"`
1153+
Secrets Secrets `json:"secrets,omitempty"`
1154+
Instance bool `json:"instance,omitempty"`
1155+
Flags int `json:"flags,omitempty"`
1156+
}{}
1157+
err := json.Unmarshal(b, &temp)
1158+
if err != nil {
1159+
return err
1160+
}
1161+
activity.CreatedAt = time.Unix(0, temp.CreatedAt*1000000)
1162+
activity.ApplicationID = temp.ApplicationID
1163+
activity.Assets = temp.Assets
1164+
activity.Details = temp.Details
1165+
activity.Emoji = temp.Emoji
1166+
activity.Flags = temp.Flags
1167+
activity.Instance = temp.Instance
1168+
activity.Name = temp.Name
1169+
activity.Party = temp.Party
1170+
activity.Secrets = temp.Secrets
1171+
activity.State = temp.State
1172+
activity.Timestamps = temp.Timestamps
1173+
activity.Type = temp.Type
1174+
activity.URL = temp.URL
1175+
return nil
1176+
}
1177+
1178+
// Party defines the Party field in the Activity struct
1179+
// https://discord.com/developers/docs/topics/gateway#activity-object
1180+
type Party struct {
1181+
ID string `json:"id,omitempty"`
1182+
Size []int `json:"size,omitempty"`
1183+
}
1184+
1185+
// Secrets defines the Secrets field for the Activity struct
1186+
// https://discord.com/developers/docs/topics/gateway#activity-object
1187+
type Secrets struct {
1188+
Join string `json:"join,omitempty"`
1189+
Spectate string `json:"spectate,omitempty"`
1190+
Match string `json:"match,omitempty"`
11201191
}
11211192

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

0 commit comments

Comments
 (0)