main.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. } else {
  36. dg.Token = Token
  37. }
  38. // Open websocket connection to Discord
  39. err := dg.Open()
  40. if err != nil {
  41. fmt.Println(err)
  42. }
  43. fmt.Println("Bot is now running. Press CTRL-C to exit.")
  44. // Simple way to keep program running until CTRL-C is pressed.
  45. <-make(chan struct{})
  46. return
  47. }
  48. // This function will be called (due to AddHandler above) every time a new
  49. // message is created on any channel that the autenticated bot has access to.
  50. func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
  51. // Print message to stdout.
  52. fmt.Printf("%20s %20s %20s > %s\n", m.ChannelID, time.Now().Format(time.Stamp), m.Author.Username, m.Content)
  53. }