|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + |
| 10 | + "github.com/bwmarrin/discordgo" |
| 11 | + "github.com/joho/godotenv" |
| 12 | + "golang.org/x/oauth2" |
| 13 | +) |
| 14 | + |
| 15 | +var oauthConfig = oauth2.Config{ |
| 16 | + Endpoint: oauth2.Endpoint{ |
| 17 | + AuthURL: "https://discord.com/oauth2/authorize", |
| 18 | + TokenURL: "https://discord.com/api/oauth2/token", |
| 19 | + }, |
| 20 | + Scopes: []string{"identify", "role_connections.write"}, |
| 21 | +} |
| 22 | + |
| 23 | +var ( |
| 24 | + appID = flag.String("app", "", "Application ID") |
| 25 | + token = flag.String("token", "", "Application token") |
| 26 | + clientSecret = flag.String("secret", "", "OAuth2 secret") |
| 27 | + redirectURL = flag.String("redirect", "", "OAuth2 Redirect URL") |
| 28 | +) |
| 29 | + |
| 30 | +func init() { |
| 31 | + flag.Parse() |
| 32 | + godotenv.Load() |
| 33 | + oauthConfig.ClientID = *appID |
| 34 | + oauthConfig.ClientSecret = *clientSecret |
| 35 | + oauthConfig.RedirectURL, _ = url.JoinPath(*redirectURL, "/linked-roles-callback") |
| 36 | +} |
| 37 | + |
| 38 | +func main() { |
| 39 | + s, _ := discordgo.New("Bot " + *token) |
| 40 | + |
| 41 | + _, err := s.ApplicationRoleConnectionMetadataUpdate(*appID, []*discordgo.ApplicationRoleConnectionMetadata{ |
| 42 | + { |
| 43 | + Type: discordgo.ApplicationRoleConnectionMetadataIntegerGreaterThanOrEqual, |
| 44 | + Key: "loc", |
| 45 | + Name: "Lines of Code", |
| 46 | + NameLocalizations: map[discordgo.Locale]string{}, |
| 47 | + Description: "Total lines of code written", |
| 48 | + DescriptionLocalizations: map[discordgo.Locale]string{}, |
| 49 | + }, |
| 50 | + { |
| 51 | + Type: discordgo.ApplicationRoleConnectionMetadataBooleanEqual, |
| 52 | + Key: "gopher", |
| 53 | + Name: "Gopher", |
| 54 | + NameLocalizations: map[discordgo.Locale]string{}, |
| 55 | + Description: "Writes in Go", |
| 56 | + DescriptionLocalizations: map[discordgo.Locale]string{}, |
| 57 | + }, |
| 58 | + { |
| 59 | + Type: discordgo.ApplicationRoleConnectionMetadataDatetimeGreaterThanOrEqual, |
| 60 | + Key: "first_line", |
| 61 | + Name: "First line written", |
| 62 | + NameLocalizations: map[discordgo.Locale]string{}, |
| 63 | + Description: "Days since the first line of code", |
| 64 | + DescriptionLocalizations: map[discordgo.Locale]string{}, |
| 65 | + }, |
| 66 | + }) |
| 67 | + if err != nil { |
| 68 | + panic(err) |
| 69 | + } |
| 70 | + |
| 71 | + fmt.Println("Updated application metadata") |
| 72 | + http.HandleFunc("/linked-roles", func(w http.ResponseWriter, r *http.Request) { |
| 73 | + w.Header().Set("Cache-Control", "no-cache") |
| 74 | + // Redirect the user to Discord OAuth2 page. |
| 75 | + http.Redirect(w, r, oauthConfig.AuthCodeURL("random-state"), http.StatusMovedPermanently) |
| 76 | + }) |
| 77 | + http.HandleFunc("/linked-roles-callback", func(w http.ResponseWriter, r *http.Request) { |
| 78 | + q := r.URL.Query() |
| 79 | + // A safeguard against CSRF attacks. |
| 80 | + // Usually tied to requesting user or random. |
| 81 | + // NOTE: Hardcoded for the sake of the example. |
| 82 | + if q["state"][0] != "random-state" { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + // Fetch the tokens with code we've received. |
| 87 | + tokens, err := oauthConfig.Exchange(r.Context(), q["code"][0]) |
| 88 | + if err != nil { |
| 89 | + w.Write([]byte(err.Error())) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + // Construct a temporary session with user's OAuth2 access_token. |
| 94 | + ts, _ := discordgo.New("Bearer " + tokens.AccessToken) |
| 95 | + |
| 96 | + // Retrive the user data. |
| 97 | + u, err := ts.User("@me") |
| 98 | + if err != nil { |
| 99 | + w.Write([]byte(err.Error())) |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + // Fetch external metadata... |
| 104 | + // NOTE: Hardcoded for the sake of the example. |
| 105 | + metadata := map[string]string{ |
| 106 | + "gopher": "1", // 1 for true, 0 for false |
| 107 | + "loc": "10000", |
| 108 | + "first_line": "1970-01-01", // YYYY-MM-DD |
| 109 | + } |
| 110 | + |
| 111 | + // And submit it back to discord. |
| 112 | + _, err = ts.UserApplicationRoleConnectionUpdate(*appID, &discordgo.ApplicationRoleConnection{ |
| 113 | + PlatformName: "Discord Gophers", |
| 114 | + PlatformUsername: u.Username, |
| 115 | + Metadata: metadata, |
| 116 | + }) |
| 117 | + if err != nil { |
| 118 | + w.Write([]byte(err.Error())) |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + // Retrieve it to check if everything is ok. |
| 123 | + info, err := ts.UserApplicationRoleConnection(*appID) |
| 124 | + if err != nil { |
| 125 | + w.Write([]byte(err.Error())) |
| 126 | + return |
| 127 | + } |
| 128 | + jsonMetadata, _ := json.Marshal(info.Metadata) |
| 129 | + // And show it to the user. |
| 130 | + w.Write([]byte(fmt.Sprintf("Your updated metadata is: %s", jsonMetadata))) |
| 131 | + }) |
| 132 | + http.ListenAndServe(":8010", nil) |
| 133 | +} |
0 commit comments