main.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // This file provides a basic "quick start" example of using the Discordgo
  2. // package to connect to Discord using the low level API functions.
  3. package main
  4. import (
  5. "flag"
  6. "fmt"
  7. "time"
  8. "github.com/bwmarrin/discordgo"
  9. )
  10. var (
  11. Email string
  12. Password string
  13. Token string
  14. BotID string
  15. )
  16. func init() {
  17. flag.StringVar(&Email, "e", "", "Account Email")
  18. flag.StringVar(&Password, "p", "", "Account Password")
  19. flag.StringVar(&Token, "t", "", "Account Token")
  20. flag.Parse()
  21. }
  22. func main() {
  23. // Create a new Discord Session struct and set a handler for the
  24. dg := discordgo.Session{}
  25. // Register messageCreate as a callback for the messageCreate events.
  26. dg.AddHandler(messageCreate)
  27. // If no Authentication Token was provided login using the
  28. // provided Email and Password.
  29. if Token == "" {
  30. err := dg.Login(Email, Password)
  31. if err != nil {
  32. fmt.Println("error logging into Discord,", err)
  33. return
  34. }
  35. }
  36. // Open websocket connection to Discord
  37. err := dg.Open()
  38. if err != nil {
  39. fmt.Println(err)
  40. }
  41. fmt.Println("Bot is now running. Press CTRL-C to exit.")
  42. // Simple way to keep program running until CTRL-C is pressed.
  43. <-make(chan struct{})
  44. return
  45. }
  46. // This function will be called (due to AddHandler above) every time a new
  47. // message is created on any channel that the autenticated bot has access to.
  48. func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
  49. // Print message to stdout.
  50. fmt.Printf("%20s %20s %20s > %s\n", m.ChannelID, time.Now().Format(time.Stamp), m.Author.Username, m.Content)
  51. }