interactions

package module
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2021 License: MIT Imports: 11 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")
	app.Run(1323)
}

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

This section is empty.

Types

type App

type App struct {
	Router *fasthttprouter.Router
	// 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) 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) Get

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

Get retrieves a value from the global context

func (*App) ProcessRequest

func (a *App) ProcessRequest(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) Run

func (a *App) Run(port int) error

Run runs the App with a built-in fasthttp web server. It takes a port as its only argument.

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 *logrus.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