Update to api_basic example
This commit is contained in:
parent
5980385f40
commit
1e8d2b9939
1 changed files with 31 additions and 23 deletions
|
@ -3,52 +3,60 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Email string
|
||||||
|
Password string
|
||||||
|
Token string
|
||||||
|
BotID string
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
|
||||||
|
flag.StringVar(&Email, "e", "", "Account Email")
|
||||||
|
flag.StringVar(&Password, "p", "", "Account Password")
|
||||||
|
flag.StringVar(&Token, "t", "", "Account Token")
|
||||||
|
flag.Parse()
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
var err error
|
// Create a new Discord Session struct and set a handler for the
|
||||||
|
|
||||||
// Check for Username and Password CLI arguments.
|
|
||||||
if len(os.Args) != 3 {
|
|
||||||
fmt.Println("You must provide username and password as arguments. See below example.")
|
|
||||||
fmt.Println(os.Args[0], " [username] [password]")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a new Discord Session interface and set a handler for the
|
|
||||||
// OnMessageCreate event that happens for every new message on any channel
|
|
||||||
dg := discordgo.Session{}
|
dg := discordgo.Session{}
|
||||||
|
|
||||||
// Register messageCreate as a callback for the messageCreate events.
|
// Register messageCreate as a callback for the messageCreate events.
|
||||||
dg.AddHandler(messageCreate)
|
dg.AddHandler(messageCreate)
|
||||||
|
|
||||||
// Login to the Discord server and store the authentication token
|
// If no Authentication Token was provided login using the
|
||||||
err = dg.Login(os.Args[1], os.Args[2])
|
// provided Email and Password.
|
||||||
if err != nil {
|
if Token == "" {
|
||||||
fmt.Println(err)
|
err := dg.Login(Email, Password)
|
||||||
return
|
if err != nil {
|
||||||
|
fmt.Println("error logging into Discord,", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open websocket connection
|
// Open websocket connection to Discord
|
||||||
err = dg.Open()
|
err := dg.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simple way to keep program running until any key press.
|
fmt.Println("Bot is now running. Press CTRL-C to exit.")
|
||||||
var input string
|
// Simple way to keep program running until CTRL-C is pressed.
|
||||||
fmt.Scanln(&input)
|
<-make(chan struct{})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function will be called (due to AddHandler above) every time a new
|
// This function will be called (due to AddHandler above) every time a new
|
||||||
// message is created on any channel that the autenticated user has access to.
|
// message is created on any channel that the autenticated bot has access to.
|
||||||
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
|
||||||
// Print message to stdout.
|
// Print message to stdout.
|
||||||
|
|
Loading…
Reference in a new issue