interactions

package
v0.0.0-...-09247b9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Verify

func Verify(r *http.Request, key ed25519.PublicKey) bool

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

func (data *Data) ResponseURL() string

type Embed

type Embed struct {
	Title       string    `json:"title"`
	Description string    `json:"description"`
	Color       int       `json:"color"`
	Footer      Footer    `json:"footer"`
	Thumbnail   Thumbnail `json:"thumbnail"`
	Fields      []Field   `json:"fields"`
}

type Field

type Field struct {
	Name   string `json:"name"`
	Value  string `json:"value"`
	Inline bool   `json:"inline,omitempty"`
}
type Footer struct {
	IconURL string `json:"icon_url"`
	Text    string `json:"text"`
}

type InteractionApplicationCommandCallbackData

type InteractionApplicationCommandCallbackData struct {
	TTS             *bool            `json:"tts,omitempty"`
	Content         string           `json:"content"`
	Flags           int              `json:"flags,omitempty"`
	Embeds          []Embed          `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
)

type InteractionType

type InteractionType int
const (
	Ping InteractionType
	ApplicationCommand
)

type Thumbnail

type Thumbnail struct {
	URL string `json:"url"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL