Skip to content

Commit cc173bb

Browse files
authored
Feature/update application model and add endpoints (#24)
* Refactor Application struct and improve documentation The Application struct in api/application.go has been thoroughly revised for improved clarity and effectiveness. Changes encompass refining documentation, adjusting optional properties based on their functionality, and introducing new fields including 'guilds', 'redirect_uris', and 'interactions_endpoint_url' * Add Discord application API endpoints Two key API endpoints have been added to the application, specifically serving Discord. These comprise fetching the current application and editing the current application. The routes for these endpoints have also been introduced.
1 parent a858d53 commit cc173bb

File tree

3 files changed

+121
-23
lines changed

3 files changed

+121
-23
lines changed

api/application.go

+28-23
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,32 @@ import "github.com/veteran-software/discord-api-wrapper/v10/oauth2"
2020
//
2121
//goland:noinspection SpellCheckingInspection
2222
type Application struct {
23-
ID Snowflake `json:"id"` // the id of the app
24-
Name string `json:"name"` // the name of the app
25-
Icon *string `json:"icon"` // the icon hash of the app
26-
Description string `json:"description"` // the description of the app
27-
RpcOrigins []string `json:"rpc_origins"` // an array of rpc origin urls, if rpc is enabled
28-
BotPublic bool `json:"bot_public"` // when false only app owner can join the app's bot to guilds
29-
BotRequireCodeGrant bool `json:"bot_require_code_grant"` // when true the app's bot will only join upon completion of the full oauth2 code grant flow
30-
TermsOfServiceURL string `json:"terms_of_service_url,omitempty"` // the url of the app's terms of service
31-
PrivacyPolicyURL string `json:"privacy_policy_url,omitempty"` // the url of the app's privacy policy
32-
Owner User `json:"owner,omitempty"` // partial user object containing info on the owner of the application
33-
VerifyKey string `json:"verify_key"` // the hex encoded key for verification in interactions and the GameSDK's GetTicket
34-
Team *Team `json:"team"` // if the application belongs to a team, this will be a list of the members of that team
35-
GuildID Snowflake `json:"guild_id,omitempty"` // if this application is a game sold on Discord, this field will be the guild to which it has been linked
36-
PrimarySkuID Snowflake `json:"primary_sku_id"` // if this application is a game sold on Discord, this field will be the id of the "Game SKU" that is created, if exists
37-
Slug string `json:"slug,omitempty"` // if this application is a game sold on Discord, this field will be the URL slug that links to the store page
38-
CoverImage string `json:"cover_image,omitempty"` // the application's default rich presence invite cover image hash
39-
Flags ApplicationFlags `json:"flags,omitempty"` // the application's public ApplicationFlags
40-
Tags []string `json:"tags,omitempty"` // up to 5 tags describing the content and functionality of the application
41-
InstallParams InstallParams `json:"install_params,omitempty"` // settings for the application's default in-app authorization link, if enabled
42-
CustomInstallURL string `json:"custom_install_url,omitempty"` // the application's default custom authorization link, if enabled
43-
RoleConnectionsVerificationURL string `json:"role_connections_verification_url"` // the application's role connection verification entry point, which when configured will render the app as a verification method in the guild role verification configuration
23+
ID Snowflake `json:"id"` // ID of the app
24+
Name string `json:"name"` // Name of the app
25+
Icon *string `json:"icon"` // Icon hash of the app
26+
Description string `json:"description"` // Description of the app
27+
RpcOrigins []string `json:"rpc_origins,omitempty"` // List of RPC origin URLs, if RPC is enabled
28+
BotPublic bool `json:"bot_public"` // When false, only the app owner can add the app to guilds
29+
BotRequireCodeGrant bool `json:"bot_require_code_grant"` // When true, the app's bot will only join upon completion of the full OAuth2 code grant flow
30+
Bot User `json:"bot,omitempty"` // Partial user object for the bot user associated with the app
31+
TermsOfServiceURL string `json:"terms_of_service_url,omitempty"` // URL of the app's Terms of Service
32+
PrivacyPolicyURL string `json:"privacy_policy_url,omitempty"` // URL of the app's Privacy Policy
33+
Owner User `json:"owner,omitempty"` // Partial user object for the owner of the app
34+
VerifyKey string `json:"verify_key"` // Hex encoded key for verification in interactions and the GameSDK's GetTicket
35+
Team *Team `json:"team"` // If the app belongs to a team, this will be a list of the members of that team
36+
GuildID Snowflake `json:"guild_id,omitempty"` // Guild associated with the app. For example, a developer support server.
37+
Guild Guild `json:"guild,omitempty"` // Partial object of the associated guild
38+
PrimarySkuID Snowflake `json:"primary_sku_id,omitempty"` // If this app is a game sold on Discord, this field will be the id of the "Game SKU" that is created, if exists
39+
Slug string `json:"slug,omitempty"` // If this app is a game sold on Discord, this field will be the URL slug that links to the store page
40+
CoverImage string `json:"cover_image,omitempty"` // App's default rich presence invite cover image hash
41+
Flags ApplicationFlags `json:"flags,omitempty"` // App's public flags
42+
ApproximateGuildCount int64 `json:"approximate_guild_count,omitempty"` // Approximate count of guilds the app has been added to
43+
RedirectUris []string `json:"redirect_uris,omitempty"` // Array of redirect URIs for the app
44+
InteractionsEndpointUrl string `json:"interactions_endpoint_url,omitempty"` // Interactions endpoint URL for the app
45+
RoleConnectionsVerificationURL string `json:"role_connections_verification_url,omitempty"` // Role connection verification URL for the app
46+
Tags []string `json:"tags,omitempty"` // List of tags describing the content and functionality of the app. Max of 5 tags.
47+
InstallParams InstallParams `json:"install_params,omitempty"` // Settings for the app's default in-app authorization link, if enabled
48+
CustomInstallURL string `json:"custom_install_url,omitempty"` // Default custom authorization URL for the app, if enabled
4449
}
4550

4651
// ApplicationFlags - the application's public ApplicationFlags
@@ -62,6 +67,6 @@ const (
6267

6368
// InstallParams - settings for the application's default in-app authorization link, if enabled
6469
type InstallParams struct {
65-
Scopes []*oauth2.Scopes `json:"scopes"` // the scopes to add the application to the server with
66-
Permissions string `json:"permissions"` // the permissions to request for the bot role
70+
Scopes []*oauth2.Scopes `json:"scopes"` // Scopes to add the application to the server with
71+
Permissions string `json:"permissions"` // Permissions to request for the bot role
6772
}

api/application_endpoints.go

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2022-2024. Veteran Software
3+
*
4+
* Discord API Wrapper - A custom wrapper for the Discord REST API developed for a proprietary project.
5+
*
6+
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
7+
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
10+
*
11+
* You should have received a copy of the GNU General Public License along with this program.
12+
* If not, see <http://www.gnu.org/licenses/>.
13+
*/
14+
15+
package api
16+
17+
import (
18+
"encoding/json"
19+
"errors"
20+
"fmt"
21+
"strconv"
22+
"strings"
23+
24+
log "github.com/veteran-software/nowlive-logging"
25+
)
26+
27+
//goland:noinspection GoUnusedExportedFunction
28+
func GetCurrentApplication() (*Application, error) {
29+
u := parseRoute(fmt.Sprintf(getCurrentApplication, api))
30+
31+
var application *Application
32+
responseBytes, err := fireGetRequest(u, nil, nil)
33+
if err != nil {
34+
log.Errorln(log.Discord, log.FuncName(), err)
35+
return nil, err
36+
}
37+
38+
err = json.Unmarshal(responseBytes, &application)
39+
40+
return application, err
41+
}
42+
43+
//goland:noinspection GoUnusedExportedFunction
44+
func EditCurrentApplication(customInstallUrl, description, roleConnectionsVerificationUrl, interactionsEndpointUrl *string, flags *int, tags *[]string) (*Application, error) {
45+
u := parseRoute(fmt.Sprintf(editCurrentApplication, api))
46+
47+
// Set the optional qsp
48+
q := u.Query()
49+
if customInstallUrl != nil {
50+
q.Set("custom_install_url", *customInstallUrl)
51+
}
52+
if description != nil {
53+
q.Set("description", *description)
54+
}
55+
if roleConnectionsVerificationUrl != nil {
56+
q.Set("role_connections_verification_url", *roleConnectionsVerificationUrl)
57+
}
58+
if interactionsEndpointUrl != nil {
59+
q.Set("interactions_endpoint_url", *interactionsEndpointUrl)
60+
}
61+
if flags != nil {
62+
q.Set("flags", strconv.Itoa(*flags))
63+
}
64+
if tags != nil {
65+
if len(*tags) > 5 {
66+
return nil, errors.New("you cannot have more than 5 tags")
67+
}
68+
for _, tag := range *tags {
69+
if len(tag) > 20 {
70+
return nil, errors.New("tag cannot be longer than 20 characters long")
71+
}
72+
}
73+
74+
q.Set("flags", fmt.Sprintf("[%v]", strings.Join(*tags, ",")))
75+
}
76+
// If there's any of the optional qsp present, encode and add to the URL
77+
if len(q) != 0 {
78+
u.RawQuery = q.Encode()
79+
}
80+
81+
var application *Application
82+
responseBytes, err := fireGetRequest(u, nil, nil)
83+
if err != nil {
84+
log.Errorln(log.Discord, log.FuncName(), err)
85+
return nil, err
86+
}
87+
88+
err = json.Unmarshal(responseBytes, &application)
89+
90+
return application, err
91+
}

api/routes.go

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package api
1616

1717
//goland:noinspection SpellCheckingInspection,GoUnusedConst
1818
const (
19+
getCurrentApplication = "%s/applications/@me"
20+
editCurrentApplication = "%s/applications/@me"
1921
getGlobalApplicationCommands = "%s/applications/%s/commands"
2022
createGlobalApplicationCommand = getGlobalApplicationCommands
2123
bulkOverwriteGlobalApplicationCommands = getGlobalApplicationCommands

0 commit comments

Comments
 (0)