OnReady event will be handled if no callback is set by user. README updated.
This commit is contained in:
parent
d9a9a765d8
commit
f402265fd7
2 changed files with 68 additions and 42 deletions
97
README.md
97
README.md
|
@ -3,9 +3,13 @@
|
||||||
This package provides low level bindings for the [Discord](https://discordapp.com/)
|
This package provides low level bindings for the [Discord](https://discordapp.com/)
|
||||||
REST & Websocket API in the [Go](https://golang.org/) Programming Language (Golang).
|
REST & Websocket API in the [Go](https://golang.org/) Programming Language (Golang).
|
||||||
|
|
||||||
Check out [dgVoice](https://github.com/bwmarrin/dgvoice) for **experimental**
|
* See out [dgVoice](https://github.com/bwmarrin/dgvoice) for **experimental**
|
||||||
Discord voice support.
|
Discord voice support.
|
||||||
|
|
||||||
|
* See out [dgTest](https://github.com/bwmarrin/dgTest) for more examples and test code.
|
||||||
|
|
||||||
|
----
|
||||||
|
|
||||||
[](https://godoc.org/github.com/bwmarrin/discordgo)
|
[](https://godoc.org/github.com/bwmarrin/discordgo)
|
||||||
[](https://gowalker.org/github.com/bwmarrin/discordgo)
|
[](https://gowalker.org/github.com/bwmarrin/discordgo)
|
||||||
[](http://goreportcard.com/report/bwmarrin/discordgo)
|
[](http://goreportcard.com/report/bwmarrin/discordgo)
|
||||||
|
@ -17,6 +21,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
|
@ -25,20 +30,33 @@ import (
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
var username, password, token string
|
||||||
|
|
||||||
|
// Check for Username and Password arguments
|
||||||
|
if len(os.Args) < 2 || len(os.Args) > 3 {
|
||||||
|
fmt.Println("You must provide a username and password as arguments. See below example.")
|
||||||
|
fmt.Println(os.Args[0], " [username] [password]")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set username and password from command line arguments.
|
||||||
|
username = os.Args[1]
|
||||||
|
password = os.Args[2]
|
||||||
|
|
||||||
// Create a new Discord Session and set a handler for the OnMessageCreate
|
// Create a new Discord Session and set a handler for the OnMessageCreate
|
||||||
// event that happens for every new message on any channel
|
// event that happens for every new message on any channel
|
||||||
Session := discordgo.Session{
|
Session := discordgo.Session{
|
||||||
OnMessageCreate: messageCreate,
|
OnMessageCreate: messageCreate,
|
||||||
OnReady: ready,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Login to the Discord server and store the authentication token
|
// Login to the Discord server and store the authentication token
|
||||||
// inside the Session
|
// inside the Session, unless the token was already provided.
|
||||||
Session.Token, err = Session.Login("coolusername", "cleverpassword")
|
if token == "" {
|
||||||
if err != nil {
|
Session.Token, err = Session.Login(username, password)
|
||||||
fmt.Println(err)
|
if err != nil {
|
||||||
return
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open websocket connection
|
// Open websocket connection
|
||||||
|
@ -58,14 +76,8 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func ready(s *discordgo.Session, r discordgo.Ready) {
|
|
||||||
// start the Heartbeat. This is required
|
|
||||||
// to keep the websocket connection open
|
|
||||||
go s.Heartbeat(r.HeartbeatInterval)
|
|
||||||
}
|
|
||||||
|
|
||||||
func messageCreate(s *discordgo.Session, m discordgo.Message) {
|
func messageCreate(s *discordgo.Session, m discordgo.Message) {
|
||||||
fmt.Printf("%25d %s %20s > %s\n", m.ChannelID, time.Now().Format(time.Stamp), m.Author.Username, m.Content)
|
fmt.Printf("%20s %20s %20s > %s\n", m.ChannelID, time.Now().Format(time.Stamp), m.Author.Username, m.Content)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -75,8 +87,9 @@ func messageCreate(s *discordgo.Session, m discordgo.Message) {
|
||||||
Because of that there may be major changes to library functions, constants,
|
Because of that there may be major changes to library functions, constants,
|
||||||
and structures.
|
and structures.
|
||||||
|
|
||||||
- [GoDoc](https://godoc.org/github.com/bwmarrin/discordgo)
|
- [](https://godoc.org/github.com/bwmarrin/discordgo)
|
||||||
- Hand crafted documentation coming soon.
|
- [](https://gowalker.org/github.com/bwmarrin/discordgo)
|
||||||
|
- Hand crafted documentation coming eventually.
|
||||||
|
|
||||||
# What Works
|
# What Works
|
||||||
|
|
||||||
|
@ -99,27 +112,39 @@ REST and Websock API.
|
||||||
* Editing User Profile settings
|
* Editing User Profile settings
|
||||||
* Permissions related functions.
|
* Permissions related functions.
|
||||||
* Functions for Maintenance Status
|
* Functions for Maintenance Status
|
||||||
* Voice Channel support.
|
* Finish Voice support.
|
||||||
* Add a higher level interface with user friendly helper functions.
|
* Add a higher level interface with user friendly helper functions.
|
||||||
|
|
||||||
# Credits
|
|
||||||
|
|
||||||
Special thanks goes to both the below projects who helped me get started with
|
|
||||||
this project. If you're looking for alternative Golang interfaces to Discord
|
|
||||||
please check both of these out.
|
|
||||||
|
|
||||||
* https://github.com/gdraynz/go-discord
|
|
||||||
* https://github.com/Xackery/discord
|
|
||||||
|
|
||||||
|
|
||||||
# Other Discord APIs
|
# Other Discord APIs
|
||||||
|
|
||||||
- [go-discord](https://github.com/gdraynz/go-discord)
|
**Go**:
|
||||||
- [discord](https://github.com/Xackery/discord)
|
[gdraynz/**go-discord**](https://github.com/gdraynz/go-discord),
|
||||||
- [discord.py](https://github.com/Rapptz/discord.py)
|
[Xackery/**discord**](https://github.com/Xackery/discord)
|
||||||
- [discord.js](https://github.com/discord-js/discord.js)
|
|
||||||
- [discord.io](https://github.com/izy521/discord.io)
|
**.NET**:
|
||||||
- [Discord.NET](https://github.com/RogueException/Discord.Net)
|
[RogueException/**Discord.Net**](https://github.com/RogueException/Discord.Net),
|
||||||
- [DiscordSharp](https://github.com/Luigifan/DiscordSharp)
|
[Luigifan/**DiscordSharp**](https://github.com/Luigifan/DiscordSharp)
|
||||||
- [Discord4J](https://github.com/knobody/Discord4J)
|
|
||||||
- [discordrb](https://github.com/meew0/discordrb)
|
**Java**:
|
||||||
|
[nerd/**Discord4J**](https://github.com/nerd/Discord4J)
|
||||||
|
|
||||||
|
**Node.js**:
|
||||||
|
[izy521/**discord.io**](https://github.com/izy521/discord.io),
|
||||||
|
[hydrabolt/**discord.js**](https://github.com/hydrabolt/discord.js),
|
||||||
|
[qeled/**discordie**](https://github.com/qeled/discordie),
|
||||||
|
|
||||||
|
**PHP**:
|
||||||
|
[Cleanse/**discord-hypertext**](https://github.com/Cleanse/discord-hypertext),
|
||||||
|
[teamreflex/**DiscordPHP**](https://github.com/teamreflex/DiscordPHP)
|
||||||
|
|
||||||
|
**Python**:
|
||||||
|
[Rapptz/**discord.py**](https://github.com/Rapptz/discord.py)
|
||||||
|
|
||||||
|
**Ruby**:
|
||||||
|
[meew0/**discordrb**](https://github.com/meew0/discordrb)
|
||||||
|
|
||||||
|
**Scala**:
|
||||||
|
[eaceaser/**discord-akka**](https://github.com/eaceaser/discord-akka)
|
||||||
|
|
||||||
|
**Rust**:
|
||||||
|
[SpaceManiac/**discord-rs**](https://github.com/SpaceManiac/discord-rs)
|
||||||
|
|
13
wsapi.go
13
wsapi.go
|
@ -121,16 +121,17 @@ func (s *Session) event(messageType int, message []byte) (err error) {
|
||||||
switch e.Type {
|
switch e.Type {
|
||||||
|
|
||||||
case "READY":
|
case "READY":
|
||||||
|
var st Ready
|
||||||
|
if err := json.Unmarshal(e.RawData, &st); err != nil {
|
||||||
|
fmt.Println(e.Type, err)
|
||||||
|
printJSON(e.RawData) // TODO: Better error logging
|
||||||
|
return err
|
||||||
|
}
|
||||||
if s.OnReady != nil {
|
if s.OnReady != nil {
|
||||||
var st Ready
|
|
||||||
if err := json.Unmarshal(e.RawData, &st); err != nil {
|
|
||||||
fmt.Println(e.Type, err)
|
|
||||||
printJSON(e.RawData) // TODO: Better error logging
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.OnReady(s, st)
|
s.OnReady(s, st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
go s.Heartbeat(st.HeartbeatInterval)
|
||||||
case "VOICE_SERVER_UPDATE":
|
case "VOICE_SERVER_UPDATE":
|
||||||
// TEMP CODE FOR TESTING VOICE
|
// TEMP CODE FOR TESTING VOICE
|
||||||
var st VoiceServerUpdate
|
var st VoiceServerUpdate
|
||||||
|
|
Loading…
Reference in a new issue