Skip to content

Commit

Permalink
Add methods for grabbing guild images. Clean up some docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
iopred committed Jan 12, 2016
1 parent 1c00077 commit b4dfce0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
68 changes: 64 additions & 4 deletions restapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,9 @@ func (s *Session) User(userID string) (st *User, err error) {
return
}

// UserAvatar returns an image.Image of a users Avatar
// UserAvatar returns an image.Image of a users Avatar.
// userID : A user ID or "@me" which is a shortcut of current user ID
func (s *Session) UserAvatar(userID string) (img image.Image, err error) {

u, err := s.User(userID)
if err != nil {
return
Expand Down Expand Up @@ -322,6 +321,11 @@ func (s *Session) UserGuilds() (st []*Guild, err error) {
// Guild returns a Guild structure of a specific Guild.
// guildID : The ID of a Guild
func (s *Session) Guild(guildID string) (st *Guild, err error) {
// Attempt to grab the guild from State first.
st, err = s.State.Guild(guildID)
if err == nil {
return
}

body, err := s.Request("GET", GUILD(guildID), nil)
if err != nil {
Expand Down Expand Up @@ -499,6 +503,7 @@ func (s *Session) GuildInviteCreate(guildID string, i *Invite) (st *Invite, err
}

// GuildRoles returns all roles for a given guild.
// guildID : The ID of a Guild.
func (s *Session) GuildRoles(guildID string) (st []*Role, err error) {

body, err := s.Request("GET", GUILD_ROLES(guildID), nil)
Expand All @@ -511,7 +516,8 @@ func (s *Session) GuildRoles(guildID string) (st []*Role, err error) {
return // TODO return pointer
}

// GuildRoleCreate returns a new Guild Role
// GuildRoleCreate returns a new Guild Role.
// guildID: The ID of a Guild.
func (s *Session) GuildRoleCreate(guildID string) (st *Role, err error) {

body, err := s.Request("POST", GUILD_ROLES(guildID), nil)
Expand All @@ -525,6 +531,12 @@ func (s *Session) GuildRoleCreate(guildID string) (st *Role, err error) {
}

// GuildRoleEdit updates an existing Guild Role with new values
// guildID : The ID of a Guild.
// roleID : The ID of a Role.
// name : The name of the Role.
// color : The color of the role (decimal, not hex).
// hoist : Whether to display the role's users separately.
// perm : The permissions for the role.
func (s *Session) GuildRoleEdit(guildID, roleID, name string, color int, hoist bool, perm int) (st *Role, err error) {

data := struct {
Expand All @@ -545,7 +557,9 @@ func (s *Session) GuildRoleEdit(guildID, roleID, name string, color int, hoist b
}

// GuildRoleReorder reoders guild roles
func (s *Session) GuildRoleReorder(guildID string, roles []Role) (st []*Role, err error) {
// guildID : The ID of a Guild.
// roles : A list of ordered roles.
func (s *Session) GuildRoleReorder(guildID string, roles []*Role) (st []*Role, err error) {

body, err := s.Request("PATCH", GUILD_ROLES(guildID), roles)
if err != nil {
Expand All @@ -558,13 +572,59 @@ func (s *Session) GuildRoleReorder(guildID string, roles []Role) (st []*Role, er
}

// GuildRoleDelete deletes an existing role.
// guildID : The ID of a Guild.
// roleID : The ID of a Role.
func (s *Session) GuildRoleDelete(guildID, roleID string) (err error) {

_, err = s.Request("DELETE", GUILD_ROLE(guildID, roleID), nil)

return
}

// GuildIcon returns an image.Image of a guild icon.
// guildID : The ID of a Guild.
func (s *Session) GuildIcon(guildID string) (img image.Image, err error) {
g, err := s.Guild(guildID)
if err != nil {
return
}

if g.Icon == "" {
err = errors.New("Guild does not have an icon set.")
return
}

body, err := s.Request("GET", GUILD_ICON(guildID, g.Icon), nil)
if err != nil {
return
}

img, _, err = image.Decode(bytes.NewReader(body))
return
}

// GuildSplash returns an image.Image of a guild splash image.
// guildID : The ID of a Guild.
func (s *Session) GuildSplash(guildID string) (img image.Image, err error) {
g, err := s.Guild(guildID)
if err != nil {
return
}

if g.Splash == "" {
err = errors.New("Guild does not have a splash set.")
return
}

body, err := s.Request("GET", GUILD_SPLASH(guildID, g.Splash), nil)
if err != nil {
return
}

img, _, err = image.Decode(bytes.NewReader(body))
return
}

// ------------------------------------------------------------------------------------------------
// Functions specific to Discord Channels
// ------------------------------------------------------------------------------------------------
Expand Down
7 changes: 4 additions & 3 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ type Guild struct {
OwnerID string `json:"owner_id"`
Large bool `json:"large"` // ??
JoinedAt string `json:"joined_at"` // make this a timestamp
Splash string `json:"splash"`
Roles []*Role `json:"roles"`
Emojis []*Emoji `json:"emojis"`
Members []*Member `json:"members"`
Expand Down Expand Up @@ -269,9 +270,9 @@ type Ready struct {
SessionID string `json:"session_id"`
HeartbeatInterval time.Duration `json:"heartbeat_interval"`
User *User `json:"user"`
ReadState []*ReadState
PrivateChannels []*Channel `json:"private_channels"`
Guilds []*Guild `json:"guilds"`
ReadState []*ReadState `json:"read_state"`
PrivateChannels []*Channel `json:"private_channels"`
Guilds []*Guild `json:"guilds"`
}
type RateLimit struct {
Bucket string `json:"bucket"`
Expand Down

0 comments on commit b4dfce0

Please sign in to comment.