Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/DisgoOrg/disgo/discord"
"github.com/DisgoOrg/disgo/rest"
"github.com/DisgoOrg/disgo/rest/route"
)

type Channel struct {
Expand Down Expand Up @@ -259,6 +260,12 @@ func (c *Channel) DeleteStageInstance(opts ...rest.RequestOpt) rest.Error {
return c.Bot.RestServices.StageInstanceService().DeleteStageInstance(c.ID, opts...)
}

// GetIconURL returns the Icon URL of this channel.
// This will be nil for every discord.ChannelType except discord.ChannelTypeGroupDM
func (c *Channel) GetIconURL(size int) *string {
return discord.FormatAssetURL(route.ChannelIcon, c.ID, c.Icon, size)
}

func (c *Channel) IsModerator(member *Member) bool {
if !c.IsStageChannel() {
unsupportedChannelType(c)
Expand Down
17 changes: 1 addition & 16 deletions core/guild.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package core

import (
"strings"

"github.com/DisgoOrg/disgo/discord"
"github.com/DisgoOrg/disgo/rest"
"github.com/DisgoOrg/disgo/rest/route"
Expand Down Expand Up @@ -189,20 +187,7 @@ func (g *Guild) GetBan(userID discord.Snowflake, opts ...rest.RequestOpt) (*Ban,

// IconURL returns the Icon of a Guild
func (g *Guild) IconURL(size int) *string {
if g.Icon == nil {
return nil
}
animated := strings.HasPrefix(*g.Icon, "a_")
format := route.PNG
if animated {
format = route.GIF
}
compiledRoute, err := route.GuildIcon.Compile(nil, format, size, g.ID.String(), *g.Icon)
if err != nil {
return nil
}
u := compiledRoute.URL()
return &u
return discord.FormatAssetURL(route.GuildIcon, g.ID, g.Icon, size)
}

// GetAuditLogs gets AuditLog(s) for this Guild
Expand Down
15 changes: 1 addition & 14 deletions core/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package core
import (
"fmt"
"strconv"
"strings"

"github.com/DisgoOrg/disgo/discord"
"github.com/DisgoOrg/disgo/rest"
Expand Down Expand Up @@ -52,19 +51,7 @@ func (u *User) Tag() string {
}

func (u *User) getAssetURL(cdnRoute *route.CDNRoute, assetId *string, size int) *string {
if assetId == nil {
return nil
}
format := route.PNG
if strings.HasPrefix(*assetId, "a_") {
format = route.GIF
}
compiledRoute, err := cdnRoute.Compile(nil, format, size, u.ID, *assetId)
if err != nil {
return nil
}
url := compiledRoute.URL()
return &url
return discord.FormatAssetURL(cdnRoute, u.ID, assetId, size)
}

// OpenDMChannel creates a DMChannel between the user and the Disgo client
Expand Down
11 changes: 11 additions & 0 deletions discord/channel.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package discord

import (
"github.com/DisgoOrg/disgo/rest/route"
)

// ChannelType for interacting with discord's channels
type ChannelType int

Expand Down Expand Up @@ -92,4 +96,11 @@ type PartialChannel struct {
ID Snowflake `json:"id"`
Type ChannelType `json:"type"`
Name string `json:"name"`
Icon *string `json:"icon,omitempty"`
}

// GetIconURL returns the Icon URL of this channel.
// This will be nil for every discord.ChannelType except discord.ChannelTypeGroupDM
func (c *PartialChannel) GetIconURL(size int) *string {
return FormatAssetURL(route.ChannelIcon, c.ID, c.Icon, size)
}
12 changes: 0 additions & 12 deletions discord/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,6 @@ var (
ErrStickerTypeGuild = errors.New("sticker type must be of type StickerTypeGuild")
)

func ErrUnexpectedQueryParam(param string) error {
return fmt.Errorf("unexpected query param '%s' received", param)
}

func ErrInvalidArgCount(argCount int, paramCount int) error {
return fmt.Errorf("invalid amount of arguments received. expected: %d, received: %d", argCount, paramCount)
}

func ErrFileExtensionNotSupported(fileExtension string) error {
return fmt.Errorf("provided file extension: %s is not supported by discord on this end", fileExtension)
}

func ErrUnexpectedGatewayOp(wOp GatewayOpcode, rOp int) error {
return fmt.Errorf("expected op: %d, received: %d", wOp, rOp)
}
23 changes: 23 additions & 0 deletions discord/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package discord

import (
"strings"

"github.com/DisgoOrg/disgo/rest/route"
)

func FormatAssetURL(cdnRoute *route.CDNRoute, entityId Snowflake, assetId *string, size int) *string {
if assetId == nil {
return nil
}
format := route.PNG
if strings.HasPrefix(*assetId, "a_") {
format = route.GIF
}
compiledRoute, err := cdnRoute.Compile(nil, format, size, entityId, *assetId)
if err != nil {
return nil
}
url := compiledRoute.URL()
return &url
}
6 changes: 2 additions & 4 deletions rest/route/api_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"net/url"
"strings"

"github.com/DisgoOrg/disgo/discord"
)

// NewAPIRoute generates a new discord api path struct
Expand Down Expand Up @@ -45,7 +43,7 @@ type APIRoute struct {
// Compile returns a CompiledAPIRoute
func (r *APIRoute) Compile(queryValues QueryValues, params ...interface{}) (*CompiledAPIRoute, error) {
if len(params) != r.urlParamCount {
return nil, discord.ErrInvalidArgCount(r.urlParamCount, len(params))
return nil, ErrInvalidArgCount(r.urlParamCount, len(params))
}
path := r.path
var majorParams []string
Expand All @@ -65,7 +63,7 @@ func (r *APIRoute) Compile(queryValues QueryValues, params ...interface{}) (*Com
query := url.Values{}
for param, value := range queryValues {
if _, ok := r.queryParams[param]; !ok {
return nil, discord.ErrUnexpectedQueryParam(param)
return nil, ErrUnexpectedQueryParam(param)
}
query.Add(param, fmt.Sprint(value))
}
Expand Down
6 changes: 2 additions & 4 deletions rest/route/cdn_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"net/url"
"strings"

"github.com/DisgoOrg/disgo/discord"
)

// NewCDNRoute generates a new discord cdn path struct
Expand Down Expand Up @@ -52,7 +50,7 @@ func (r *CDNRoute) Compile(queryValues QueryValues, fileExtension FileExtension,
}
}
if !supported {
return nil, discord.ErrFileExtensionNotSupported(fileExtension.String())
return nil, ErrFileExtensionNotSupported(fileExtension.String())
}
if queryValues == nil {
queryValues = QueryValues{}
Expand All @@ -76,7 +74,7 @@ func (r *CDNRoute) Compile(queryValues QueryValues, fileExtension FileExtension,
query := url.Values{}
for param, value := range queryValues {
if _, ok := r.queryParams[param]; !ok {
return nil, discord.ErrUnexpectedQueryParam(param)
return nil, ErrUnexpectedQueryParam(param)
}
query.Add(param, fmt.Sprint(value))
}
Expand Down
15 changes: 15 additions & 0 deletions rest/route/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package route

import "fmt"

func ErrUnexpectedQueryParam(param string) error {
return fmt.Errorf("unexpected query param '%s' received", param)
}

func ErrInvalidArgCount(argCount int, paramCount int) error {
return fmt.Errorf("invalid amount of arguments received. expected: %d, received: %d", argCount, paramCount)
}

func ErrFileExtensionNotSupported(fileExtension string) error {
return fmt.Errorf("provided file extension: %s is not supported by discord on this end", fileExtension)
}
6 changes: 2 additions & 4 deletions rest/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"net/url"
"strings"

"github.com/DisgoOrg/disgo/discord"
)

// NewRoute generates a new discord path struct
Expand Down Expand Up @@ -43,7 +41,7 @@ type Route struct {
// Compile returns a CompiledRoute
func (r *Route) Compile(queryValues QueryValues, params ...interface{}) (*CompiledRoute, error) {
if len(params) != r.urlParamCount {
return nil, discord.ErrInvalidArgCount(r.urlParamCount, len(params))
return nil, ErrInvalidArgCount(r.urlParamCount, len(params))
}
path := r.path
for _, param := range params {
Expand All @@ -57,7 +55,7 @@ func (r *Route) Compile(queryValues QueryValues, params ...interface{}) (*Compil
query := url.Values{}
for param, value := range queryValues {
if _, ok := r.queryParams[param]; !ok {
return nil, discord.ErrUnexpectedQueryParam(param)
return nil, ErrUnexpectedQueryParam(param)
}
query.Add(param, fmt.Sprint(value))
}
Expand Down
2 changes: 2 additions & 0 deletions rest/route/route_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ var (
UserAvatar = NewCDNRoute("/avatars/{user.id}/{user.avatar.hash}", PNG, JPEG, WebP, GIF)
DefaultUserAvatar = NewCDNRoute("/embed/avatars/{user.discriminator%5}", PNG)

ChannelIcon = NewCDNRoute("/channel-icons/{channel.id}/{channel.icon.hash}", PNG, JPEG, WebP)

MemberAvatar = NewCDNRoute("/guilds/{guild.id}/users/{user.id}/avatars/{member.avatar.hash}", PNG, JPEG, WebP, GIF)

ApplicationIcon = NewCDNRoute("/app-icons/{application.id}/{icon.hash}", PNG, JPEG, WebP)
Expand Down