Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Verify ¶
Verify implements the verification side of the discord interactions api signing algorithm, as documented here: https://discord.com/developers/docs/interactions/slash-commands#security-and-authorization
Example ¶
package main import ( "bytes" "crypto/ed25519" "encoding/hex" "encoding/json" "net/http" "github.com/bsdlp/discord-interactions-go/interactions" ) func main() { hexEncodedDiscordPubkey := "a43f74054d052d43c3ed90e07c8e3270690826d1fd38eda3a42e91ff38c2482b" discordPubkey, err := hex.DecodeString(hexEncodedDiscordPubkey) if err != nil { // handle error } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { verified := interactions.Verify(r, ed25519.PublicKey(discordPubkey)) if !verified { http.Error(w, "signature mismatch", http.StatusUnauthorized) return } defer r.Body.Close() var data interactions.Data err := json.NewDecoder(r.Body).Decode(&data) if err != nil { // handle error } // respond to ping if data.Type == interactions.Ping { _, err := w.Write([]byte(`{"type":1}`)) if err != nil { // handle error } return } // handle command response := &interactions.InteractionResponse{ Type: interactions.ChannelMessage, Data: &interactions.InteractionApplicationCommandCallbackData{ Content: "got your message kid", }, } var responsePayload bytes.Buffer err = json.NewEncoder(&responsePayload).Encode(response) if err != nil { // handle error } _, err = http.Post(data.ResponseURL(), "application/json", &responsePayload) if err != nil { // handle err } }) }
Output:
Types ¶
type ApplicationCommandInteractionDataOption ¶
type ApplicationCommandInteractionDataOption struct { Name string `json:"name"` Value interface{} `json:"value,omitempty"` Options []ApplicationCommandInteractionDataOption `json:"options,omitempty"` }
type Data ¶
type Data struct { Type InteractionType `json:"type"` Token string `json:"token"` Member struct { User struct { ID string `json:"id"` Username string `json:"username"` Avatar string `json:"avatar"` Discriminator string `json:"discriminator"` PublicFlags int64 `json:"public_flags"` } `json:"user"` Roles []string `json:"roles"` PremiumSince time.Time `json:"premium_since"` Permissions string `json:"permissions"` Pending bool `json:"pending"` Nick string `json:"nick"` Mute bool `json:"mute"` JoinedAt time.Time `json:"joined_at"` IsPending bool `json:"is_pending"` Deaf bool `json:"deaf"` } `json:"member"` ID string `json:"id"` GuildID string `json:"guild_id"` Data struct { Options []ApplicationCommandInteractionDataOption `json:"options"` Name string `json:"name"` ID string `json:"id"` } `json:"data"` ChannelID string `json:"channel_id"` }
func (*Data) ResponseURL ¶
type InteractionApplicationCommandCallbackData ¶
type InteractionApplicationCommandCallbackData struct { TTS *bool `json:"tts,omitempty"` Content string `json:"content"` Embeds json.Unmarshaler `json:"embeds,omitempty"` AllowedMentions json.Unmarshaler `json:"allowed_mentions,omitempty"` }
type InteractionResponse ¶
type InteractionResponse struct { Type InteractionResponseType `json:"type"` Data *InteractionApplicationCommandCallbackData `json:"data,omitempty"` }
type InteractionResponseFlags ¶
type InteractionResponseFlags int64
const Ephemeral InteractionResponseFlags = 1 << 6
type InteractionResponseType ¶
type InteractionResponseType int
const ( Pong InteractionResponseType Acknowledge ChannelMessage ChannelMessageWithSource AcknowledgeWithSource )
Click to show internal directories.
Click to hide internal directories.