main.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package main
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "github.com/bwmarrin/discordgo"
  8. )
  9. // Variables used for command line options
  10. var (
  11. Token string
  12. Name string
  13. DeleteID string
  14. ListOnly bool
  15. )
  16. func init() {
  17. flag.StringVar(&Token, "t", "", "Owner Account Token")
  18. flag.StringVar(&Name, "n", "", "Name to give App/Bot")
  19. flag.StringVar(&DeleteID, "d", "", "Application ID to delete")
  20. flag.BoolVar(&ListOnly, "l", false, "List Applications Only")
  21. flag.Parse()
  22. if Token == "" {
  23. flag.Usage()
  24. os.Exit(1)
  25. }
  26. }
  27. func main() {
  28. var err error
  29. // Create a new Discord session using the provided login information.
  30. dg, err := discordgo.New(Token)
  31. if err != nil {
  32. fmt.Println("error creating Discord session,", err)
  33. return
  34. }
  35. // If -l set, only display a list of existing applications
  36. // for the given account.
  37. if ListOnly {
  38. aps, err := dg.Applications()
  39. if err != nil {
  40. fmt.Println("error fetching applications,", err)
  41. return
  42. }
  43. for _, v := range aps {
  44. fmt.Println("-----------------------------------------------------")
  45. b, _ := json.MarshalIndent(v, "", " ")
  46. fmt.Println(string(b))
  47. }
  48. return
  49. }
  50. // if -d set, delete the given Application
  51. if DeleteID != "" {
  52. err = dg.ApplicationDelete(DeleteID)
  53. if err != nil {
  54. fmt.Println("error deleting application,", err)
  55. }
  56. return
  57. }
  58. if Name == "" {
  59. flag.Usage()
  60. os.Exit(1)
  61. }
  62. // Create a new application.
  63. ap := &discordgo.Application{}
  64. ap.Name = Name
  65. ap, err = dg.ApplicationCreate(ap)
  66. if err != nil {
  67. fmt.Println("error creating new applicaiton,", err)
  68. return
  69. }
  70. fmt.Printf("Application created successfully:\n")
  71. b, _ := json.MarshalIndent(ap, "", " ")
  72. fmt.Println(string(b))
  73. // Create the bot account under the application we just created
  74. bot, err := dg.ApplicationBotCreate(ap.ID)
  75. if err != nil {
  76. fmt.Println("error creating bot account,", err)
  77. return
  78. }
  79. fmt.Printf("Bot account created successfully.\n")
  80. b, _ = json.MarshalIndent(bot, "", " ")
  81. fmt.Println(string(b))
  82. fmt.Println("Please save the above posted info in a secure place.")
  83. fmt.Println("You will need that information to login with your bot account.")
  84. }