Add support for relationships (#284)

* Add support for relationships

Adds Support for:
  - Sending friend request.
  - Accepting friend request.
  - Getting all the relationships.
  - Getting all the mutual friends with another user.
  - Blocking a user.

**Note:**
  - Bot accounts are not allowed to access the endpoint.
  - May close bwmarrin/discordgo#150

* Implement requested changes

Changed the uint8 declarations to int.

* Change the missed unint8 declaration to int

Missed one instance of unint8 during previous push.
This commit is contained in:
AI 2016-11-09 06:07:04 +05:00 committed by Chris Rhodes
parent 602885488b
commit ed7a451a31
3 changed files with 87 additions and 3 deletions

View file

@ -87,10 +87,10 @@ var (
EndpointChannelMessagesBulkDelete = func(cID string) string { return EndpointChannel(cID) + "/messages/bulk_delete" } EndpointChannelMessagesBulkDelete = func(cID string) string { return EndpointChannel(cID) + "/messages/bulk_delete" }
EndpointChannelMessagesPins = func(cID string) string { return EndpointChannel(cID) + "/pins" } EndpointChannelMessagesPins = func(cID string) string { return EndpointChannel(cID) + "/pins" }
EndpointChannelMessagePin = func(cID, mID string) string { return EndpointChannel(cID) + "/pins/" + mID } EndpointChannelMessagePin = func(cID, mID string) string { return EndpointChannel(cID) + "/pins/" + mID }
EndpointChannelWebhooks = func(cID string) string { return EndpointChannel(cID) + "/webhooks" }
EndpointWebhook = func(wID string) string { return EndpointWebhooks + wID } EndpointChannelWebhooks = func(cID string) string { return EndpointChannel(cID) + "/webhooks" }
EndpointWebhookToken = func(wID, token string) string { return EndpointWebhooks + wID + "/" + token } EndpointWebhook = func(wID string) string { return EndpointWebhooks + wID }
EndpointWebhookToken = func(wID, token string) string { return EndpointWebhooks + wID + "/" + token }
EndpointMessageReactions = func(cID, mID, eID string) string { EndpointMessageReactions = func(cID, mID, eID string) string {
return EndpointChannelMessage(cID, mID) + "/reactions/" + eID return EndpointChannelMessage(cID, mID) + "/reactions/" + eID
@ -99,6 +99,10 @@ var (
return EndpointMessageReactions(cID, mID, eID) + "/" + uID return EndpointMessageReactions(cID, mID, eID) + "/" + uID
} }
EndpointRelationships = func() string { return EndpointUsers + "@me" + "/relationships" }
EndpointRelationship = func(uID string) string { return EndpointRelationships() + "/" + uID }
EndpointRelationshipsMutual = func(uID string) string { return EndpointUsers + uID + "/relationships" }
EndpointInvite = func(iID string) string { return EndpointAPI + "invite/" + iID } EndpointInvite = func(iID string) string { return EndpointAPI + "invite/" + iID }
EndpointIntegrationsJoin = func(iID string) string { return EndpointAPI + "integrations/" + iID + "/join" } EndpointIntegrationsJoin = func(iID string) string { return EndpointAPI + "integrations/" + iID + "/join" }

View file

@ -40,6 +40,8 @@ var eventToInterface = map[string]interface{}{
"PRESENCE_UPDATE": PresenceUpdate{}, "PRESENCE_UPDATE": PresenceUpdate{},
"PRESENCES_REPLACE": PresencesReplace{}, "PRESENCES_REPLACE": PresencesReplace{},
"READY": Ready{}, "READY": Ready{},
"RELATIONSHIP_ADD": RelationshipAdd{},
"RELATIONSHIP_REMOVE": RelationshipRemove{},
"USER_UPDATE": UserUpdate{}, "USER_UPDATE": UserUpdate{},
"USER_SETTINGS_UPDATE": UserSettingsUpdate{}, "USER_SETTINGS_UPDATE": UserSettingsUpdate{},
"USER_GUILD_SETTINGS_UPDATE": UserGuildSettingsUpdate{}, "USER_GUILD_SETTINGS_UPDATE": UserGuildSettingsUpdate{},
@ -156,6 +158,16 @@ type GuildRoleUpdate struct {
// PresencesReplace is an array of Presences for an event. // PresencesReplace is an array of Presences for an event.
type PresencesReplace []*Presence type PresencesReplace []*Presence
// RelationshipAdd is a wrapper struct for an event.
type RelationshipAdd struct {
*Relationship
}
// RelationshipRemove is a wrapper struct for an event.
type RelationshipRemove struct {
*Relationship
}
// VoiceStateUpdate is a wrapper struct for an event. // VoiceStateUpdate is a wrapper struct for an event.
type VoiceStateUpdate struct { type VoiceStateUpdate struct {
*VoiceState *VoiceState

View file

@ -1736,3 +1736,71 @@ func (s *Session) MessageReactions(channelID, messageID, emojiID string, limit i
err = unmarshal(body, &st) err = unmarshal(body, &st)
return return
} }
// ------------------------------------------------------------------------------------------------
// Functions specific to Discord Relationships (Friends list)
// ------------------------------------------------------------------------------------------------
// RelationshipsGet returns an array of all the relationships of the user.
func (s *Session) RelationshipsGet() (r []*Relationship, err error) {
body, err := s.RequestWithBucketID("GET", EndpointRelationships(), nil, EndpointRelationships())
if err != nil {
return
}
err = unmarshal(body, &r)
return
}
// relationshipCreate creates a new relationship. (I.e. send or accept a friend request, block a user.)
// relationshipType : 1 = friend, 2 = blocked, 3 = incoming friend req, 4 = sent friend req
func (s *Session) relationshipCreate(userID string, relationshipType int) (err error) {
data := struct {
Type int `json:"type"`
}{relationshipType}
fmt.Println("Data: " + fmt.Sprintf("%v", data))
_, err = s.RequestWithBucketID("PUT", EndpointRelationship(userID), data, EndpointRelationships())
return
}
// RelationshipFriendRequestSend sends a friend request to a user.
// userID: ID of the user.
func (s *Session) RelationshipFriendRequestSend(userID string) (err error) {
err = s.relationshipCreate(userID, 4)
return
}
// RelationshipFriendRequestAccept accepts a friend request from a user.
// userID: ID of the user.
func (s *Session) RelationshipFriendRequestAccept(userID string) (err error) {
err = s.relationshipCreate(userID, 1)
return
}
// RelationshipUserBlock blocks a user.
// userID: ID of the user.
func (s *Session) RelationshipUserBlock(userID string) (err error) {
err = s.relationshipCreate(userID, 2)
return
}
// RelationshipDelete removes the relationship with a user.
// userID: ID of the user.
func (s *Session) RelationshipDelete(userID string) (err error) {
_, err = s.RequestWithBucketID("DELETE", EndpointRelationship(userID), nil, EndpointRelationships())
return
}
// RelationshipsMutualGet returns an array of all the users both @me and the given user is friends with.
// userID: ID of the user.
func (s *Session) RelationshipsMutualGet(userID string) (mf []*User, err error) {
body, err := s.RequestWithBucketID("GET", EndpointRelationshipsMutual(userID), nil, EndpointRelationshipsMutual(userID))
if err != nil {
return
}
err = unmarshal(body, &mf)
return
}