github.com/meowstercatel/discordgo@v0.0.0-20250615204139-29b43d676735/oauth2.go (about)

     1  // Discordgo - Discord bindings for Go
     2  // Available at https://github.com/bwmarrin/discordgo
     3  
     4  // Copyright 2015-2016 Bruce Marriner <bruce@sqls.net>.  All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  // This file contains functions related to Discord OAuth2 endpoints
     9  
    10  package discordgo
    11  
    12  // ------------------------------------------------------------------------------------------------
    13  // Code specific to Discord OAuth2 Applications
    14  // ------------------------------------------------------------------------------------------------
    15  
    16  // The MembershipState represents whether the user is in the team or has been invited into it
    17  type MembershipState int
    18  
    19  // Constants for the different stages of the MembershipState
    20  const (
    21  	MembershipStateInvited  MembershipState = 1
    22  	MembershipStateAccepted MembershipState = 2
    23  )
    24  
    25  // A TeamMember struct stores values for a single Team Member, extending the normal User data - note that the user field is partial
    26  type TeamMember struct {
    27  	User            *User           `json:"user"`
    28  	TeamID          string          `json:"team_id"`
    29  	MembershipState MembershipState `json:"membership_state"`
    30  	Permissions     []string        `json:"permissions"`
    31  }
    32  
    33  // A Team struct stores the members of a Discord Developer Team as well as some metadata about it
    34  type Team struct {
    35  	ID          string        `json:"id"`
    36  	Name        string        `json:"name"`
    37  	Description string        `json:"description"`
    38  	Icon        string        `json:"icon"`
    39  	OwnerID     string        `json:"owner_user_id"`
    40  	Members     []*TeamMember `json:"members"`
    41  }
    42  
    43  // Application returns an Application structure of a specific Application
    44  //   appID : The ID of an Application
    45  func (s *Session) Application(appID string) (st *Application, err error) {
    46  
    47  	body, err := s.RequestWithBucketID("GET", EndpointOAuth2Application(appID), nil, EndpointOAuth2Application(""))
    48  	if err != nil {
    49  		return
    50  	}
    51  
    52  	err = unmarshal(body, &st)
    53  	return
    54  }
    55  
    56  // Applications returns all applications for the authenticated user
    57  func (s *Session) Applications() (st []*Application, err error) {
    58  
    59  	body, err := s.RequestWithBucketID("GET", EndpointOAuth2Applications, nil, EndpointOAuth2Applications)
    60  	if err != nil {
    61  		return
    62  	}
    63  
    64  	err = unmarshal(body, &st)
    65  	return
    66  }
    67  
    68  // ApplicationCreate creates a new Application
    69  //    name : Name of Application / Bot
    70  //    uris : Redirect URIs (Not required)
    71  func (s *Session) ApplicationCreate(ap *Application) (st *Application, err error) {
    72  
    73  	data := struct {
    74  		Name        string `json:"name"`
    75  		Description string `json:"description"`
    76  	}{ap.Name, ap.Description}
    77  
    78  	body, err := s.RequestWithBucketID("POST", EndpointOAuth2Applications, data, EndpointOAuth2Applications)
    79  	if err != nil {
    80  		return
    81  	}
    82  
    83  	err = unmarshal(body, &st)
    84  	return
    85  }
    86  
    87  // ApplicationUpdate updates an existing Application
    88  //   var : desc
    89  func (s *Session) ApplicationUpdate(appID string, ap *Application) (st *Application, err error) {
    90  
    91  	data := struct {
    92  		Name        string `json:"name"`
    93  		Description string `json:"description"`
    94  	}{ap.Name, ap.Description}
    95  
    96  	body, err := s.RequestWithBucketID("PUT", EndpointOAuth2Application(appID), data, EndpointOAuth2Application(""))
    97  	if err != nil {
    98  		return
    99  	}
   100  
   101  	err = unmarshal(body, &st)
   102  	return
   103  }
   104  
   105  // ApplicationDelete deletes an existing Application
   106  //   appID : The ID of an Application
   107  func (s *Session) ApplicationDelete(appID string) (err error) {
   108  
   109  	_, err = s.RequestWithBucketID("DELETE", EndpointOAuth2Application(appID), nil, EndpointOAuth2Application(""))
   110  	if err != nil {
   111  		return
   112  	}
   113  
   114  	return
   115  }
   116  
   117  // Asset struct stores values for an asset of an application
   118  type Asset struct {
   119  	Type int    `json:"type"`
   120  	ID   string `json:"id"`
   121  	Name string `json:"name"`
   122  }
   123  
   124  // ApplicationAssets returns an application's assets
   125  func (s *Session) ApplicationAssets(appID string) (ass []*Asset, err error) {
   126  
   127  	body, err := s.RequestWithBucketID("GET", EndpointOAuth2ApplicationAssets(appID), nil, EndpointOAuth2ApplicationAssets(""))
   128  	if err != nil {
   129  		return
   130  	}
   131  
   132  	err = unmarshal(body, &ass)
   133  	return
   134  }
   135  
   136  // ------------------------------------------------------------------------------------------------
   137  // Code specific to Discord OAuth2 Application Bots
   138  // ------------------------------------------------------------------------------------------------
   139  
   140  // ApplicationBotCreate creates an Application Bot Account
   141  //
   142  //   appID : The ID of an Application
   143  //
   144  // NOTE: func name may change, if I can think up something better.
   145  func (s *Session) ApplicationBotCreate(appID string) (st *User, err error) {
   146  
   147  	body, err := s.RequestWithBucketID("POST", EndpointOAuth2ApplicationsBot(appID), nil, EndpointOAuth2ApplicationsBot(""))
   148  	if err != nil {
   149  		return
   150  	}
   151  
   152  	err = unmarshal(body, &st)
   153  	return
   154  }