main.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/bwmarrin/discordgo"
  6. )
  7. var (
  8. Email string
  9. Password string
  10. Token string
  11. BotID string
  12. )
  13. func init() {
  14. flag.StringVar(&Email, "e", "", "Account Email")
  15. flag.StringVar(&Password, "p", "", "Account Password")
  16. flag.StringVar(&Token, "t", "", "Account Token")
  17. flag.Parse()
  18. }
  19. func main() {
  20. // Create a new Discord session using the provided login information.
  21. dg, err := discordgo.New(Email, Password, Token)
  22. if err != nil {
  23. fmt.Println("error creating Discord session,", err)
  24. return
  25. }
  26. // Get the account information.
  27. u, err := dg.User("@me")
  28. if err != nil {
  29. fmt.Println("error obtaining account details,", err)
  30. }
  31. // Store the account ID for later use.
  32. BotID = u.ID
  33. // Register messageCreate as a callback for the messageCreate events.
  34. dg.AddHandler(messageCreate)
  35. // Open the websocket and begin listening.
  36. dg.Open()
  37. fmt.Println("Bot is now running. Press CTRL-C to exit.")
  38. // Simple way to keep program running until CTRL-C is pressed.
  39. <-make(chan struct{})
  40. return
  41. }
  42. // This function will be called (due to AddHandler above) every time a new
  43. // message is created on any channel that the autenticated bot has access to.
  44. func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
  45. // Ignore all messages created by the bot itself
  46. if m.Author.ID == BotID {
  47. return
  48. }
  49. // If the message is "ping" reply with "Pong!"
  50. if m.Content == "ping" {
  51. s.ChannelMessageSend(m.ChannelID, "Pong!")
  52. }
  53. // If the message is "pong" reply with "Ping!"
  54. if m.Content == "pong" {
  55. s.ChannelMessageSend(m.ChannelID, "Ping!")
  56. }
  57. }