Przeglądaj źródła

Added AppMaker example

Bruce Marriner 8 lat temu
rodzic
commit
6ff8c4b218
2 zmienionych plików z 158 dodań i 0 usunięć
  1. 58 0
      examples/appmaker/README.md
  2. 100 0
      examples/appmaker/main.go

+ 58 - 0
examples/appmaker/README.md

@@ -0,0 +1,58 @@
+<img align="right" src="http://bwmarrin.github.io/discordgo/img/discordgo.png">
+AppMaker Example
+====
+
+This example demonstrates how to utilize DiscordGo to create Bot Applications
+
+You can create a new bot account or convert an existing normal user account into
+a bot account with this tool.  You can also view the list of applications you 
+have on your account and delete applications.
+
+### Build
+
+This assumes you already have a working Go environment setup and that 
+DiscordGo is correctly installed on your system.
+
+```sh
+go build
+```
+
+### Usage
+
+```
+Usage of ./appmaker:
+  -a string
+        App/Bot Name
+  -c string
+        Token of account to convert.
+  -d string
+        Application ID to delete
+  -e string
+        Account Email
+  -l    List Applications Only
+  -p string
+        Account Password
+  -t string
+        Account Token
+```
+
+* Account Email and Password or Token are required.  The account provided with
+these fields will be the "owner" of any bot applications created.
+
+* If you provide the **-l** flag than appmaker will only display a list of 
+applications on provided account.
+
+* If you provide a **-d** flag with a valid application ID then that application
+will be delted.
+
+* If you provide a **-c** flag with a valid user token then than user account
+will be converted into a Bot account instead of creating a new Bot account for
+an application.
+
+
+Below example will create a new Bot Application under the given Email/Password 
+account. The Bot will be named **DiscordGoRocks**
+
+```sh
+./appmaker -e Email -p Password -a DiscordGoRocks
+```

+ 100 - 0
examples/appmaker/main.go

@@ -0,0 +1,100 @@
+package main
+
+import (
+	"flag"
+	"fmt"
+
+	"github.com/bwmarrin/discordgo"
+)
+
+var (
+	Email     string
+	Password  string
+	Token     string
+	AppName   string
+	ConvToken string
+	DeleteID  string
+	ListOnly  bool
+)
+
+func init() {
+
+	flag.StringVar(&Email, "e", "", "Account Email")
+	flag.StringVar(&Password, "p", "", "Account Password")
+	flag.StringVar(&Token, "t", "", "Account Token")
+	flag.StringVar(&DeleteID, "d", "", "Application ID to delete")
+	flag.BoolVar(&ListOnly, "l", false, "List Applications Only")
+	flag.StringVar(&AppName, "a", "", "App/Bot Name")
+	flag.StringVar(&ConvToken, "c", "", "Token of account to convert.")
+	flag.Parse()
+}
+
+func main() {
+
+	// Create a new Discord session using the provided login information.
+	dg, err := discordgo.New(Email, Password, Token)
+	if err != nil {
+		fmt.Println("error creating Discord session,", err)
+		return
+	}
+
+	// If -l set, only display a list of existing applications
+	// for the given account.
+	if ListOnly {
+		aps, err := dg.Applications()
+		if err != nil {
+			fmt.Println("error fetching applications,", err)
+			return
+		}
+
+		for k, v := range aps {
+			fmt.Printf("%d : --------------------------------------\n", k)
+			fmt.Printf("ID: %s\n", v.ID)
+			fmt.Printf("Name: %s\n", v.Name)
+			fmt.Printf("Secret: %s\n", v.Secret)
+			fmt.Printf("Description: %s\n", v.Description)
+		}
+		return
+	}
+
+	// if -d set, delete the given Application
+	if DeleteID != "" {
+		err := dg.ApplicationDelete(DeleteID)
+		if err != nil {
+			fmt.Println("error deleting application,", err)
+		}
+		return
+	}
+
+	// Create a new application.
+	ap := &discordgo.Application{}
+	ap.Name = AppName
+	ap, err = dg.ApplicationCreate(ap)
+	if err != nil {
+		fmt.Println("error creating new applicaiton,", err)
+		return
+	}
+
+	fmt.Printf("Application created successfully:\n")
+	fmt.Printf("ID: %s\n", ap.ID)
+	fmt.Printf("Name: %s\n", ap.Name)
+	fmt.Printf("Secret: %s\n\n", ap.Secret)
+
+	// Create the bot account under the application we just created
+	// If ConvToken is set, then this will convert an existing account
+	// into a bot account under the application we just created.
+	bot, err := dg.ApplicationBotCreate(ap.ID, ConvToken)
+	if err != nil {
+		fmt.Println("error creating bot account,", err)
+		return
+	}
+
+	fmt.Printf("Bot account created successfully.\n")
+	fmt.Printf("ID: %s\n", bot.ID)
+	fmt.Printf("Username: %s\n", bot.Username)
+	fmt.Printf("Token: %s\n\n", bot.Token)
+	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.")
+
+	return
+}