Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/rpc #170

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
72 changes: 72 additions & 0 deletions _examples/rpc/rpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"os"
"os/signal"
"syscall"

"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/rest"
"github.com/disgoorg/disgo/rpc"
"github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2"
)

var (
clientID = snowflake.GetEnv("disgo_client_id")
clientSecret = os.Getenv("disgo_client_secret")
channelID = snowflake.GetEnv("disgo_channel_id")
)

func main() {
log.SetLevel(log.LevelDebug)
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.Info("example is starting...")

oauth2Client := rest.NewOAuth2(rest.NewClient(""))

client, err := rpc.NewClient(clientID)
if err != nil {
log.Fatal(err)
return
}
defer client.Close()

var tokenRs *discord.AccessTokenResponse
if err = client.Send(rpc.Message{
Cmd: rpc.CmdAuthorize,
Args: rpc.CmdArgsAuthorize{
ClientID: clientID,
Scopes: []discord.OAuth2Scope{discord.OAuth2ScopeRPC, discord.OAuth2ScopeMessagesRead},
},
}, rpc.NewHandler(func(data rpc.CmdRsAuthorize) {
tokenRs, err = oauth2Client.GetAccessToken(clientID, clientSecret, data.Code, "http://localhost")
if err != nil {
log.Fatal(err)
}
})); err != nil {
log.Fatal(err)
}

if err = client.Send(rpc.Message{
Cmd: rpc.CmdAuthenticate,
Args: rpc.CmdArgsAuthenticate{
AccessToken: tokenRs.AccessToken,
},
}, nil); err != nil {
log.Fatal(err)
}

if err = client.Subscribe(rpc.EventMessageCreate, rpc.CmdArgsSubscribeMessage{
ChannelID: channelID,
}, rpc.NewHandler(func(data rpc.EventDataMessageCreate) {
log.Info("message: ", data.Message.Content)
})); err != nil {
log.Fatal(err)
}

log.Info("example is now running. Press CTRL-C to exit.")
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-s
}
10 changes: 5 additions & 5 deletions discord/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ const (

// Activity represents the fields of a user's presence
type Activity struct {
ID string `json:"id"`
Name string `json:"name"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type ActivityType `json:"type"`
URL *string `json:"url"`
CreatedAt int64 `json:"created_at"`
URL *string `json:"url,omitempty"`
CreatedAt int64 `json:"created_at,omitempty"`
Timestamps *ActivityTimestamps `json:"timestamps,omitempty"`
ApplicationID snowflake.ID `json:"application_id,omitempty"`
Details *string `json:"details,omitempty"`
Expand All @@ -32,7 +32,7 @@ type Activity struct {
Secrets *ActivitySecrets `json:"secrets,omitempty"`
Instance *bool `json:"instance,omitempty"`
Flags ActivityFlags `json:"flags,omitempty"`
Buttons []string `json:"buttons"`
Buttons []string `json:"buttons,omitempty"`
}

// ActivityFlags add additional information to an activity
Expand Down
8 changes: 8 additions & 0 deletions discord/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ func (a Application) CoverURL(opts ...CDNOpt) *string {
return formatAssetURL(route.ApplicationCover, opts, a.ID, *a.Cover)
}

type OAuth2Application struct {
ID snowflake.ID `json:"id"`
Name string `json:"name"`
Icon *string `json:"icon,omitempty"`
Description string `json:"description"`
RPCOrigins []string `json:"rpc_origins"`
}

type PartialApplication struct {
ID snowflake.ID `json:"id"`
Flags ApplicationFlags `json:"flags"`
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/disgoorg/disgo
go 1.18

require (
github.com/Microsoft/go-winio v0.5.2
topi314 marked this conversation as resolved.
Show resolved Hide resolved
github.com/disgoorg/log v1.2.0
github.com/disgoorg/snowflake/v2 v2.0.0
github.com/gorilla/websocket v1.5.0
Expand All @@ -12,7 +13,8 @@ require (
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
11 changes: 10 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VMrpA=
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disgoorg/log v1.2.0 h1:sqlXnu/ZKAlIlHV9IO+dbMto7/hCQ474vlIdMWk8QKo=
github.com/disgoorg/log v1.2.0/go.mod h1:3x1KDG6DI1CE2pDwi3qlwT3wlXpeHW/5rVay+1qDqOo=
github.com/disgoorg/snowflake/v2 v2.0.0 h1:+xvyyDddXmXLHmiG8SZiQ3sdZdZPbUR22fSHoqwkrOA=
Expand All @@ -10,11 +13,17 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b h1:qYTY2tN72LhgDj2rtWG+LI6TXFl2ygFQQ4YezfVaGQE=
github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b/go.mod h1:/pA7k3zsXKdjjAiUhB5CjuKib9KJGCaLvZwtxGC8U0s=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/exp v0.0.0-20220325121720-054d8573a5d8 h1:Xt4/LzbTwfocTk9ZLEu4onjeFucl88iW+v4j4PWbQuE=
golang.org/x/exp v0.0.0-20220325121720-054d8573a5d8/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
8 changes: 5 additions & 3 deletions rest/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ func (s *oAuth2Impl) SetGuildCommandPermissions(bearerToken string, applicationI

func (s *oAuth2Impl) exchangeAccessToken(clientID snowflake.ID, clientSecret string, grantType discord.GrantType, codeOrRefreshToken string, redirectURI string, opts ...RequestOpt) (exchange *discord.AccessTokenResponse, err error) {
values := url.Values{
"client_id": []string{clientID.String()},
"client_secret": []string{clientSecret},
"grant_type": []string{grantType.String()},
"client_id": []string{clientID.String()},
"grant_type": []string{grantType.String()},
}
if clientSecret != "" {
values["client_secret"] = []string{clientSecret}
}
switch grantType {
case discord.GrantTypeAuthorizationCode:
Expand Down