Skip to content

Commit

Permalink
Handle group DMs (channels) (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian authored May 23, 2024
1 parent d934f63 commit b81270a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
90 changes: 90 additions & 0 deletions discord/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ func (u *UnmarshalChannel) UnmarshalJSON(data []byte) error {
err = json.Unmarshal(data, &v)
channel = v

case ChannelTypeGroupDM:
var v GroupDMChannel
err = json.Unmarshal(data, &v)
channel = v

case ChannelTypeGuildCategory:
var v GuildCategoryChannel
err = json.Unmarshal(data, &v)
Expand Down Expand Up @@ -423,6 +428,91 @@ func (c DMChannel) CreatedAt() time.Time {
func (DMChannel) channel() {}
func (DMChannel) messageChannel() {}

var (
_ Channel = (*GroupDMChannel)(nil)
_ MessageChannel = (*GroupDMChannel)(nil)
)

type GroupDMChannel struct {
id snowflake.ID
ownerID *snowflake.ID
name string
lastPinTimestamp *time.Time
lastMessageID *snowflake.ID
icon *string
}

func (c *GroupDMChannel) UnmarshalJSON(data []byte) error {
var v groupDMChannel
if err := json.Unmarshal(data, &v); err != nil {
return err
}

c.id = v.ID
c.ownerID = v.OwnerID
c.name = v.Name
c.lastPinTimestamp = v.LastPinTimestamp
c.lastMessageID = v.LastMessageID
c.icon = v.Icon
return nil
}

func (c GroupDMChannel) MarshalJSON() ([]byte, error) {
return json.Marshal(groupDMChannel{
ID: c.id,
Type: c.Type(),
OwnerID: c.ownerID,
Name: c.name,
LastPinTimestamp: c.lastPinTimestamp,
LastMessageID: c.lastMessageID,
Icon: c.icon,
})
}

func (c GroupDMChannel) String() string {
return channelString(c)
}

func (c GroupDMChannel) ID() snowflake.ID {
return c.id
}

func (GroupDMChannel) Type() ChannelType {
return ChannelTypeGroupDM
}

func (c GroupDMChannel) OwnerID() *snowflake.ID {
return c.ownerID
}

func (c GroupDMChannel) Name() string {
return c.name
}

func (c GroupDMChannel) LastPinTimestamp() *time.Time {
return c.lastPinTimestamp
}

func (c GroupDMChannel) LastMessageID() *snowflake.ID {
return c.lastMessageID
}

func (c GroupDMChannel) CreatedAt() time.Time {
return c.id.Time()
}

// IconURL returns the icon URL of this group DM or nil if not set
func (c GroupDMChannel) IconURL(opts ...CDNOpt) *string {
if c.icon == nil {
return nil
}
url := formatAssetURL(ChannelIcon, opts, c.id, *c.icon)
return &url
}

func (GroupDMChannel) channel() {}
func (GroupDMChannel) messageChannel() {}

var (
_ Channel = (*GuildVoiceChannel)(nil)
_ GuildChannel = (*GuildVoiceChannel)(nil)
Expand Down
10 changes: 10 additions & 0 deletions discord/channels_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ type dmChannel struct {
LastPinTimestamp *time.Time `json:"last_pin_timestamp"`
}

type groupDMChannel struct {
ID snowflake.ID `json:"id"`
Type ChannelType `json:"type"`
OwnerID *snowflake.ID `json:"owner_id"`
Name string `json:"name"`
LastPinTimestamp *time.Time `json:"last_pin_timestamp"`
LastMessageID *snowflake.ID `json:"last_message_id"`
Icon *string `json:"icon"`
}

type guildTextChannel struct {
ID snowflake.ID `json:"id"`
Type ChannelType `json:"type"`
Expand Down

0 comments on commit b81270a

Please sign in to comment.