main.go 1.7 KB

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