interactions

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: MIT Imports: 18 Imported by: 1

README

Postcord Interactions

Go Reference

Interactions is a simple, batteries included HTTP interactions library for Discord. It is designed to make is fast and easy to create a new Discord server integration using Discords new interactions system.

Getting Started

Add Interactions to your project

go get -u github.com/Postcord/interactions
Example
package main

import (
    "log"
    "os"

    "github.com/Postcord/interactions"
    "github.com/Postcord/objects"
)

func main() {
	app, err := interactions.New(&interactions.Config{
		PublicKey: os.Getenv("DISCORD_PUBLIC_KEY"),
		Token:     "Bot " + os.Getenv("DISCORD_TOKEN"),
	})
	if err != nil {
		panic("failed to create interactions client")
	}

	app.CommandHandler(testHandler)

	log.Println("test-bot is now running")
	http.ListenAndServe(":8080", app.HTTPHandler())
}

func testHandler(ctx *objects.Interaction) *objects.InteractionResponse {
	return &objects.InteractionResponse{
		Type: objects.ResponseChannelMessageWithSource,
		Data: &objects.InteractionApplicationCommandCallbackData{
			Content: "Hello world!",
			Flags:   objects.ResponseFlagEphemeral,
		},
	}
}

Command Routing

If you're looking for a more batteries included solution that includes command and interaction routing. Check out our router package.

Documentation

Documentation is still a work in progress.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FailUnknownError added in v0.1.3

func FailUnknownError(w http.ResponseWriter, jr *json.Encoder)

Types

type App

type App struct {
	// contains filtered or unexported fields
}

App is the primary interactions server

func New

func New(config *Config) (*App, error)

Create a new interactions server instance

func (*App) AutocompleteHandler added in v0.0.15

func (a *App) AutocompleteHandler(handler HandlerFunc)

func (*App) CommandHandler added in v0.0.13

func (a *App) CommandHandler(handler HandlerFunc)

CommandHandler sets the function to handle slash command events

func (*App) ComponentHandler

func (a *App) ComponentHandler(handler HandlerFunc)

ComponentHandler sets the function to handle Component events.

func (*App) FastHTTPHandler added in v0.1.0

func (a *App) FastHTTPHandler() fasthttp.RequestHandler

FastHTTPHandler exposes a fasthttp handler to process incoming interactions

func (*App) Get

func (a *App) Get(key string) (interface{}, bool)

Get retrieves a value from the global context

func (*App) HTTPHandler added in v0.0.17

func (a *App) HTTPHandler() http.HandlerFunc

HTTPHandler exposes a net/http handler to process incoming interactions

func (*App) LambdaHandler added in v0.0.15

func (a *App) LambdaHandler() LambdaHandler

LambdaHandler exposes an AWS APi Gateway Lambda handler to process incoming interactions

func (*App) ModalHandler added in v0.1.0

func (a *App) ModalHandler(handler HandlerFunc)

func (*App) ProcessRequest

func (a *App) ProcessRequest(ctx context.Context, data []byte) (resp *objects.InteractionResponse, err error)

ProcessRequest is used internally to process a validated request. It is exposed to allow users to tied Postcord in with any web framework of their choosing. Ensure you only pass validated requests.

func (*App) Rest

func (a *App) Rest() *rest.Client

Rest exposes the internal Rest client so you can make calls to the Discord API

func (*App) ServeHTTP added in v0.1.2

func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP makes App implement the http.Handler interface

func (*App) Set

func (a *App) Set(key string, obj interface{})

Set stores a value in the global context. This is suitable for things like database connections.

type Config

type Config struct {
	// PublicKey is your interactions public key provided on the Discord developers site
	PublicKey string
	// Logger allows you to specify a custom logrus Logger for the App to use
	Logger *zerolog.Logger
	// Token (optional) is your Discord token that will be passed to the internal REST client
	Token string
	// RESTClient (optional) is the REST client you are overriding with. Useful for proxies.
	RESTClient *rest.Client
}

Config contains the configuration values for the interactions App

Jump to

Keyboard shortcuts

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