receiver

package module
v0.0.0-...-dacfb77 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2021 License: MIT Imports: 6 Imported by: 0

README

Discord Event Receiver

A library for receiving Discord (and custom) events from NATS

Example

package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"github.com/chatorbot/receiver"
	"github.com/Postcord/objects"
	"github.com/sirupsen/logrus"
)

// Message represents a Discord message
type Message map[string]interface{}

func main() {
	logger := logrus.New()
	logger.SetLevel(logrus.InfoLevel)
	logger.SetFormatter(&logrus.TextFormatter{
		ForceColors:      true,
		DisableTimestamp: true,
		FullTimestamp:    true,
		TimestampFormat:  "",
	})
	r, err := receiver.New(&receiver.Config{
		NatsAddr: "nats://127.0.0.1:4444",
		Token:    os.Getenv("DISCORD_TOKEN"),
		Logger:   logger,
	})
	if err != nil {
		panic("failed to create receiver")
	}
	defer r.Close()

	r.On("discord.ready", ready)
	r.On("discord.message_create", message)
	r.Start()

	// Wait here until CTRL-C or other term signal is received.
	fmt.Println("Bot is now running.  Press CTRL-C to exit.")
	sc := make(chan os.Signal, 1)
	signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
	<-sc
}

func ready(s *rest.Client, m *objects.Ready) error {
	fmt.Printf("Bot ready: %s\n", m.User.Username)
	return nil
}

func message(s *rest.Client, m *objects.MessageCreate) error {
	fmt.Printf("%s: %s\n", m.Message.Author.Username, m.Message.Content)
	return nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	NatsAddr    string
	Token       string
	Logger      *logrus.Logger
	Client      *rest.Client
	ServiceName string
}

Config contains the configuration options for the Receiver

type EventHandler

type EventHandler interface{}

type Receiver

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

Receiver is a client to interface with the Chator NATS Discord interface

func New

func New(conf *Config) (*Receiver, error)

NewReceiver Creates a new Discord NATS receiver

func (*Receiver) Close

func (r *Receiver) Close()

func (*Receiver) On

func (r *Receiver) On(sub string, handler EventHandler)

On registers an event listener

func (*Receiver) Start

func (r *Receiver) Start()

Jump to

Keyboard shortcuts

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