diff --git a/oauth2.go b/oauth2.go new file mode 100644 index 0000000..9cb094a --- /dev/null +++ b/oauth2.go @@ -0,0 +1,134 @@ +// Discordgo - Discord bindings for Go +// Available at https://github.com/bwmarrin/discordgo + +// Copyright 2015-2016 Bruce Marriner . All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file contains functions related to Discord OAuth2 applications + +package discordgo + +import ( + "fmt" +) + +// An Application struct stores values for a Discord OAuth2 Application +type Application struct { + ID string `json:"id,omitempty"` + Name string `json:"name"` + Description string `json:"description,omitempty"` + Icon string `json:"icon,omitempty"` + Secret string `json:"secret,omitempty"` + RedirectURIs *[]string `json:"redirect_uris,omitempty"` + + // Concept.. almost guarenteed to be removed. + // Imagine that it's just not even here at all. + ses *Session +} + +// Application returns an Application structure of a specific Application +// appID : The ID of an Application +func (s *Session) Application(appID string) (st *Application, err error) { + + body, err := s.Request("GET", APPLICATION(appID), nil) + if err != nil { + return + } + + err = unmarshal(body, &st) + st.ses = s + return +} + +// Applications returns all applications for the authenticated user +func (s *Session) Applications() (st []*Application, err error) { + + body, err := s.Request("GET", APPLICATIONS, nil) + if err != nil { + return + } + + err = unmarshal(body, &st) + for k, _ := range st { + st[k].ses = s + } + return + // TODO .. +} + +// ApplicationCreate creates a new Application +// name : Name of Application / Bot +// uris : Redirect URIs (Not required) +func (s *Session) ApplicationCreate(ap *Application) (st *Application, err error) { + + data := struct { + Name string `json:"name"` + Description string `json:"description"` + RedirectURIs *[]string `json:"redirect_uris,omitempty"` + }{ap.Name, ap.Description, ap.RedirectURIs} + + body, err := s.Request("POST", APPLICATIONS, data) + if err != nil { + return + } + + err = unmarshal(body, &st) + st.ses = s + return +} + +// ApplicationEdit edits an existing Application +// var : desc +func (s *Session) ApplicationUpdate(appID string, ap *Application) (st *Application, err error) { + + data := struct { + Name string `json:"name"` + Description string `json:"description"` + RedirectURIs *[]string `json:"redirect_uris,omitempty"` + }{ap.Name, ap.Description, ap.RedirectURIs} + + body, err := s.Request("PUT", APPLICATION(appID), data) + if err != nil { + return + } + + err = unmarshal(body, &st) + st.ses = s + return +} + +// ApplicationDelete deletes an existing Application +// appID : The ID of an Application +func (s *Session) ApplicationDelete(appID string) (err error) { + + _, err = s.Request("DELETE", APPLICATION(appID), nil) + if err != nil { + return + } + + return +} + +////////////////////////////////////////////////////////////////////////////// +// Below two functions are experimental ideas, they will absolutely change +// one way or another and may be deleted entirely. + +// Delete is a concept helper function, may be removed. +// this func depends on the Application.ses pointer +// pointing to the Discord session that the application +// came from. This "magic" makes some very very nice helper +// functions possible. +func (a *Application) Delete() (err error) { + if a.ses == nil { + return fmt.Errorf("ses is nil.") + } + return a.ses.ApplicationDelete(a.ID) +} + +// Delete is a concept helper function, may be removed. +// this one doesn't depend on the "magic" of adding the ses +// pointer to each Application +func (a *Application) DeleteB(s *Session) (err error) { + return s.ApplicationDelete(a.ID) +} diff --git a/oauth2_test.go b/oauth2_test.go new file mode 100644 index 0000000..4d2899a --- /dev/null +++ b/oauth2_test.go @@ -0,0 +1,53 @@ +package discordgo_test + +import ( + "fmt" + "os" + + "github.com/bwmarrin/discordgo" +) + +func ExampleApplication() { + + // Authentication Token pulled from environment variable DG_TOKEN + Token := os.Getenv("DG_TOKEN") + if Token == "" { + return + } + + // Create a new Discordgo session + dg, err := discordgo.New(Token) + if err != nil { + fmt.Println(err) + return + } + + // Create an new Application + ap := &discordgo.Application{} + ap.Name = "TestApp" + ap.Description = "TestDesc" + ap, err = dg.ApplicationCreate(ap) + fmt.Printf("ApplicationCreate: err: %+v, app: %+v\n", err, ap) + + // Get a specific Application by it's ID + ap, err = dg.Application(ap.ID) + fmt.Printf("Application: err: %+v, app: %+v\n", err, ap) + + // Update an existing Application with new values + ap.Description = "Whooooa" + ap, err = dg.ApplicationUpdate(ap.ID, ap) + fmt.Printf("ApplicationUpdate: err: %+v, app: %+v\n", err, ap) + + // Get a list of all applications for the authenticated user + apps, err := dg.Applications() + fmt.Printf("Applications: err: %+v, apps : %+v\n", err, apps) + for k, v := range apps { + fmt.Printf("Applications: %d : %+v\n", k, v) + } + + // Delete the application we created. + err = ap.Delete() + fmt.Printf("Delete: err: %+v\n", err) + + return +}