Skip to content

Top-gg-Community/go-sdk

Repository files navigation

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

About

Go API wrapper for Discord Bots

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages