Disgord 
Health
| Branch | Build status | Code climate | Go Report Card | Codacy |
|---|---|---|---|---|
| develop |
About
GoLang module for interacting with the Discord API. Supports socketing and REST functionality. Discord object will also have implemented helper functions such as Message.RespondString(session, "hello"), or Session.SaveToDiscord(&Emoji) for simplicity/readability.
Disgord has complete implementation for Discord's documented REST API. It lacks comprehensive testing, although unit-tests have been created for several of the Disgord REST implementations. The socketing is not complete, but does support all event types that are documented (using both channels and callbacks).
Note that caching is yet to be implemented. Versions from v0.5.1 and below, had caching to some degree, but was scrapped once a complete rework of the project structure was done.
Disgord does not utilize reflection, except in unit tests and unmarshalling/marshalling of JSON. But does return custom error messages for some functions which can be type checked in a switch for a more readable error handling as well potentially giving access to more information.
To get started see the examples in docs
Alternative GoLang package for Discord: DiscordGo
Discord channel/server: Discord Gophers#Disgord
Package structure
None of the sub-packages should be used outside the library. If there exists a requirement for that, please create an issue or pull request.
github.com/andersfylling/disgord
βββ.circleci :CircleCI configuration
βββcache :Different cache replacement algorithms
βββconstant :Constants such as version, GitHub URL, etc.
βββdocs :Examples, templates, (documentation)
βββendpoint :All the REST endpoints of Discord
βββevent :All the Discord event identifiers
βββgenerate :All go generate scripts for "generic" code
βββhttd :Deals with rate limits and http calls
βββtestdata :Holds all test data for unit tests (typically JSON files)
βββwebsocket :Discord Websocket logic (reconnect, resume, etc.)Dependencies
github.com/andersfylling/disgord
βββgithub.com/andersfylling/snowflake :The snowflake ID designed for Discord
βββgithub.com/json-iterator/go :For faster JSON decoding/encoding
βββgithub.com/sergi/go-diff :Unit testing for checking JSON encoding/decoding of structs
βββgithub.com/sirupsen/logrus :Logging (will be replaced with a simplified interface for DI)If you do not wish to use json-iterator, you can pass -tags=json-std to switch to "encoding/json".
However, json-iterator is the recommended default for this library.
Contributing
Please see the CONTRIBUTING.md file (Note that it can be useful to read this regardless if you have the time)
Git branching model
The branch:develop holds the most recent changes, as it name implies. There is no master branch as there will never be a "stable latest branch" except the git tags (or releases).
Mental model
Caching
The cache can be either immutable (recommended) or mutable. When the cache is mutable you will share the memory space with the cache, such that if you change your data structure you might also change the cache directly. However, by using the immutable option all incoming data is deep copied to the cache and you will not be able to directly access the memory space, this should keep your code less error-prone and allow for concurrent cache access in case you want to use channels or other long-running tasks/processes.
Requests
For every REST API request the request is rate limited and cached auto-magically by Disgord. This means that when you utilize the Session interface you won't have to worry about rate limits and data is cached to improve performance. See the GoDoc for how to bypass the caching.
Events
The reactor pattern is used. This will always be the default behavior, however channels will ofcourse work more as a pro-actor system as you deal with the data parallel to other functions. Incoming events from the discord servers are parsed into respective structs and dispatched to either a) handlers, or b) through channels. Both are dispatched from the same place, and the arguments share the same memory space. Pick handlers (register them using Session.On method) simplicity as they run in sequence, while channels are executed in a parallel setting (it's expected you understand how channels work so I won't go in-depth here).
Quick example
NOTE: To see more examples go visit the docs/examples folder. See the GoDoc for a in-depth introduction on the various topics (or disgord.go package comment). Below is an example of the traditional ping-pong bot.
// create a Disgord session
session, err := disgord.NewSession(&disgord.Config{
Token: os.Getenv("DISGORD_TOKEN"),
})
if err != nil {
panic(err)
}
// create a handler and bind it to new message events
session.On(disgord.EventMessageCreate, func(session disgord.Session, data *disgord.MessageCreate) {
msg := data.Message
if msg.Content == "ping" {
msg.RespondString(session, "pong")
}
})
// connect to the discord gateway to receive events
err = session.Connect()
if err != nil {
panic(err)
}
// Keep the socket connection alive, until you terminate the application
session.DisconnectOnInterrupt()Q&A
1. Reason for making another Discord lib in GoLang?
I'm trying to take over the world and then become a intergalactic war lord. Have to start somewhere.