interactions

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2022 License: MIT Imports: 9 Imported by: 2

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/treeder/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 ButtonStyle

type ButtonStyle int
const (
	Primary ButtonStyle
	Secondary
	Success
	Dange
	Link
)

type Component

type Component struct {
	Type       ComponentType `json:"type"`
	Style      ButtonStyle   `json:"style"` // required for button type
	Label      string        `json:"label"`
	CustomID   string        `json:"custom_id"` // required for anything but action row
	Components []Component   `json:"components,omitempty"`
}

type ComponentType

type ComponentType int
const (
	ActionRow ComponentType
	Button
	SelectMenu
)

type Data

type Data struct {
	Type   InteractionType `json:"type"`
	Token  string          `json:"token"`
	User   User            `json:"user"`
	Member struct {
		User         User      `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"`
		CustomID string                                    `json:"custom_id"`
	} `json:"data"`
	ChannelID string `json:"channel_id"`
}

func (*Data) ResponseURL

func (data *Data) ResponseURL() string

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"`
	Components      []Component      `json:"components,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
	MessageComponent // user clicks a button
	ApplicationCommandAutoComplete
)

type User

type User struct {
	ID            string `json:"id"`
	Username      string `json:"username"`
	Avatar        string `json:"avatar"`
	Discriminator string `json:"discriminator"`
	PublicFlags   int64  `json:"public_flags"`
}

Jump to

Keyboard shortcuts

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