Clean up example.

Remove the email/pass login option.  Change display to show all fields
of the Bot/App struct.
This commit is contained in:
Bruce 2017-04-10 21:05:33 +00:00
parent abc85a2de0
commit d1c23ecb09

View file

@ -1,38 +1,42 @@
package main package main
import ( import (
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"os"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
// Variables used for command line options // Variables used for command line options
var ( var (
Email string
Password string
Token string Token string
AppName string Name string
DeleteID string DeleteID string
ListOnly bool ListOnly bool
) )
func init() { func init() {
flag.StringVar(&Email, "e", "", "Account Email") flag.StringVar(&Token, "t", "", "Owner Account Token")
flag.StringVar(&Password, "p", "", "Account Password") flag.StringVar(&Name, "n", "", "Name to give App/Bot")
flag.StringVar(&Token, "t", "", "Account Token")
flag.StringVar(&DeleteID, "d", "", "Application ID to delete") flag.StringVar(&DeleteID, "d", "", "Application ID to delete")
flag.BoolVar(&ListOnly, "l", false, "List Applications Only") flag.BoolVar(&ListOnly, "l", false, "List Applications Only")
flag.StringVar(&AppName, "a", "", "App/Bot Name")
flag.Parse() flag.Parse()
if Token == "" {
flag.Usage()
os.Exit(1)
}
} }
func main() { func main() {
var err error var err error
// Create a new Discord session using the provided login information. // Create a new Discord session using the provided login information.
dg, err := discordgo.New(Email, Password, Token) dg, err := discordgo.New(Token)
if err != nil { if err != nil {
fmt.Println("error creating Discord session,", err) fmt.Println("error creating Discord session,", err)
return return
@ -41,18 +45,17 @@ func main() {
// If -l set, only display a list of existing applications // If -l set, only display a list of existing applications
// for the given account. // for the given account.
if ListOnly { if ListOnly {
aps, err2 := dg.Applications()
if err2 != nil { aps, err := dg.Applications()
if err != nil {
fmt.Println("error fetching applications,", err) fmt.Println("error fetching applications,", err)
return return
} }
for k, v := range aps { for _, v := range aps {
fmt.Printf("%d : --------------------------------------\n", k) fmt.Println("-----------------------------------------------------")
fmt.Printf("ID: %s\n", v.ID) b, _ := json.MarshalIndent(v, "", " ")
fmt.Printf("Name: %s\n", v.Name) fmt.Println(string(b))
fmt.Printf("Secret: %s\n", v.Secret)
fmt.Printf("Description: %s\n", v.Description)
} }
return return
} }
@ -66,9 +69,14 @@ func main() {
return return
} }
if Name == "" {
flag.Usage()
os.Exit(1)
}
// Create a new application. // Create a new application.
ap := &discordgo.Application{} ap := &discordgo.Application{}
ap.Name = AppName ap.Name = Name
ap, err = dg.ApplicationCreate(ap) ap, err = dg.ApplicationCreate(ap)
if err != nil { if err != nil {
fmt.Println("error creating new applicaiton,", err) fmt.Println("error creating new applicaiton,", err)
@ -76,9 +84,8 @@ func main() {
} }
fmt.Printf("Application created successfully:\n") fmt.Printf("Application created successfully:\n")
fmt.Printf("ID: %s\n", ap.ID) b, _ := json.MarshalIndent(ap, "", " ")
fmt.Printf("Name: %s\n", ap.Name) fmt.Println(string(b))
fmt.Printf("Secret: %s\n\n", ap.Secret)
// Create the bot account under the application we just created // Create the bot account under the application we just created
bot, err := dg.ApplicationBotCreate(ap.ID) bot, err := dg.ApplicationBotCreate(ap.ID)
@ -88,11 +95,9 @@ func main() {
} }
fmt.Printf("Bot account created successfully.\n") fmt.Printf("Bot account created successfully.\n")
fmt.Printf("ID: %s\n", bot.ID) b, _ = json.MarshalIndent(bot, "", " ")
fmt.Printf("Username: %s\n", bot.Username) fmt.Println(string(b))
fmt.Printf("Token: %s\n\n", bot.Token)
fmt.Println("Please save the above posted info in a secure place.") fmt.Println("Please save the above posted info in a secure place.")
fmt.Println("You will need that information to login with your bot account.") fmt.Println("You will need that information to login with your bot account.")
return
} }