Skip to content

Commit b81270a

Browse files
author
Sebastian
authored
Handle group DMs (channels) (#360)
1 parent d934f63 commit b81270a

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

discord/channel.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ func (u *UnmarshalChannel) UnmarshalJSON(data []byte) error {
185185
err = json.Unmarshal(data, &v)
186186
channel = v
187187

188+
case ChannelTypeGroupDM:
189+
var v GroupDMChannel
190+
err = json.Unmarshal(data, &v)
191+
channel = v
192+
188193
case ChannelTypeGuildCategory:
189194
var v GuildCategoryChannel
190195
err = json.Unmarshal(data, &v)
@@ -423,6 +428,91 @@ func (c DMChannel) CreatedAt() time.Time {
423428
func (DMChannel) channel() {}
424429
func (DMChannel) messageChannel() {}
425430

431+
var (
432+
_ Channel = (*GroupDMChannel)(nil)
433+
_ MessageChannel = (*GroupDMChannel)(nil)
434+
)
435+
436+
type GroupDMChannel struct {
437+
id snowflake.ID
438+
ownerID *snowflake.ID
439+
name string
440+
lastPinTimestamp *time.Time
441+
lastMessageID *snowflake.ID
442+
icon *string
443+
}
444+
445+
func (c *GroupDMChannel) UnmarshalJSON(data []byte) error {
446+
var v groupDMChannel
447+
if err := json.Unmarshal(data, &v); err != nil {
448+
return err
449+
}
450+
451+
c.id = v.ID
452+
c.ownerID = v.OwnerID
453+
c.name = v.Name
454+
c.lastPinTimestamp = v.LastPinTimestamp
455+
c.lastMessageID = v.LastMessageID
456+
c.icon = v.Icon
457+
return nil
458+
}
459+
460+
func (c GroupDMChannel) MarshalJSON() ([]byte, error) {
461+
return json.Marshal(groupDMChannel{
462+
ID: c.id,
463+
Type: c.Type(),
464+
OwnerID: c.ownerID,
465+
Name: c.name,
466+
LastPinTimestamp: c.lastPinTimestamp,
467+
LastMessageID: c.lastMessageID,
468+
Icon: c.icon,
469+
})
470+
}
471+
472+
func (c GroupDMChannel) String() string {
473+
return channelString(c)
474+
}
475+
476+
func (c GroupDMChannel) ID() snowflake.ID {
477+
return c.id
478+
}
479+
480+
func (GroupDMChannel) Type() ChannelType {
481+
return ChannelTypeGroupDM
482+
}
483+
484+
func (c GroupDMChannel) OwnerID() *snowflake.ID {
485+
return c.ownerID
486+
}
487+
488+
func (c GroupDMChannel) Name() string {
489+
return c.name
490+
}
491+
492+
func (c GroupDMChannel) LastPinTimestamp() *time.Time {
493+
return c.lastPinTimestamp
494+
}
495+
496+
func (c GroupDMChannel) LastMessageID() *snowflake.ID {
497+
return c.lastMessageID
498+
}
499+
500+
func (c GroupDMChannel) CreatedAt() time.Time {
501+
return c.id.Time()
502+
}
503+
504+
// IconURL returns the icon URL of this group DM or nil if not set
505+
func (c GroupDMChannel) IconURL(opts ...CDNOpt) *string {
506+
if c.icon == nil {
507+
return nil
508+
}
509+
url := formatAssetURL(ChannelIcon, opts, c.id, *c.icon)
510+
return &url
511+
}
512+
513+
func (GroupDMChannel) channel() {}
514+
func (GroupDMChannel) messageChannel() {}
515+
426516
var (
427517
_ Channel = (*GuildVoiceChannel)(nil)
428518
_ GuildChannel = (*GuildVoiceChannel)(nil)

discord/channels_raw.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ type dmChannel struct {
1515
LastPinTimestamp *time.Time `json:"last_pin_timestamp"`
1616
}
1717

18+
type groupDMChannel struct {
19+
ID snowflake.ID `json:"id"`
20+
Type ChannelType `json:"type"`
21+
OwnerID *snowflake.ID `json:"owner_id"`
22+
Name string `json:"name"`
23+
LastPinTimestamp *time.Time `json:"last_pin_timestamp"`
24+
LastMessageID *snowflake.ID `json:"last_message_id"`
25+
Icon *string `json:"icon"`
26+
}
27+
1828
type guildTextChannel struct {
1929
ID snowflake.ID `json:"id"`
2030
Type ChannelType `json:"type"`

0 commit comments

Comments
 (0)