Skip to content

Latest commit

 

History

History
134 lines (96 loc) · 2.95 KB

README.md

File metadata and controls

134 lines (96 loc) · 2.95 KB

Go DBL

Build Status Go Report Card GoDoc

An API wrapper for Discord Bots

Godoc is available here: https://godoc.org/github.com/DiscordBotList/go-dbl

Table of Contents

Guides

Installing

go get -u github.com/top-gg/go-dbl

Posting Stats

package main

import (
	"log"

	"github.com/top-gg/go-dbl"
)

func main() {
	dblClient, err := dbl.NewClient("token")
	if err != nil {
		log.Fatalf("Error creating new Discord Bot List client: %s", err)
	}

	err = dblClient.PostBotStats("botID", &dbl.BotStatsPayload{
		Shards: []int{2500}, // If non-sharded, just pass total server count as the only integer element
	})
	if err != nil {
		log.Printf("Error sending bot stats to Discord Bot List: %s", err)
	}

	// ...
}

Setting options

package main

import (
	"log"
	"net/http"
	"time"

	"github.com/top-gg/go-dbl"
)

const clientTimeout = 5 * time.Second

func main() {
	httpClient := &http.Client{}

	dblClient, err := dbl.NewClient(
		"token",
		dbl.HTTPClientOption(httpClient), // Setting a custom HTTP client. Default is *http.Client with default timeout.
		dbl.TimeoutOption(clientTimeout), // Setting timeout option. Default is 3 seconds
	)
	if err != nil {
		log.Fatalf("Error creating new Discord Bot List client: %s", err)
	}

	// ...
}

Ratelimits

There's a local token bucket rate limiter, allowing for 60 requests a minute (single/burst)

Upon reaching the local rate limit, ErrLocalRatelimit error will be returned

If remote rate limit is exceeded, ErrRemoteRatelimit error will be returned and RetryAfter in client fields will be updated with the retry time

Webhook

package main

import (
	"errors"
	"log"
	"net/http"

	"github.com/top-gg/go-dbl"
)

const listenerPort = ":9090"

func main() {
	listener := dbl.NewListener("token", handleVote)

	// Serve is a blocking call
	err := listener.Serve(listenerPort)
	if !errors.Is(err, http.ErrServerClosed) {
		log.Fatalf("HTTP server error: %s", err)
	}
}

func handleVote(payload *dbl.WebhookPayload) {
	// perform on payload
}

More details

For more details, Godoc and tests are available