main.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "time"
  6. "github.com/bwmarrin/discordgo"
  7. )
  8. // Variables used for command line parameters
  9. var (
  10. Token string
  11. )
  12. func init() {
  13. flag.StringVar(&Token, "t", "", "Bot Token")
  14. flag.Parse()
  15. }
  16. func main() {
  17. // Create a new Discord session using the provided bot token.
  18. dg, err := discordgo.New("Bot " + Token)
  19. if err != nil {
  20. fmt.Println("error creating Discord session,", err)
  21. return
  22. }
  23. // Register messageCreate as a callback for the messageCreate events.
  24. dg.AddHandler(messageCreate)
  25. // Open the websocket and begin listening.
  26. err = dg.Open()
  27. if err != nil {
  28. fmt.Println("error opening connection,", err)
  29. return
  30. }
  31. fmt.Println("Bot is now running. Press CTRL-C to exit.")
  32. // Simple way to keep program running until CTRL-C is pressed.
  33. <-make(chan struct{})
  34. return
  35. }
  36. // This function will be called (due to AddHandler above) every time a new
  37. // message is created on any channel that the autenticated bot has access to.
  38. func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
  39. // Print message to stdout.
  40. fmt.Printf("%20s %20s %20s > %s\n", m.ChannelID, time.Now().Format(time.Stamp), m.Author.Username, m.Content)
  41. }