main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "github.com/bwmarrin/discordgo"
  9. )
  10. // Variables used for command line parameters
  11. var (
  12. Token string
  13. )
  14. func init() {
  15. flag.StringVar(&Token, "t", "", "Bot Token")
  16. flag.Parse()
  17. }
  18. func main() {
  19. // Create a new Discord session using the provided bot token.
  20. dg, err := discordgo.New("Bot " + Token)
  21. if err != nil {
  22. fmt.Println("error creating Discord session,", err)
  23. return
  24. }
  25. // Register the messageCreate func as a callback for MessageCreate events.
  26. dg.AddHandler(messageCreate)
  27. // Just like the ping pong example, we only care about receiving message
  28. // events in this example.
  29. dg.Identify.Intents = discordgo.IntentsGuildMessages
  30. // Open a websocket connection to Discord and begin listening.
  31. err = dg.Open()
  32. if err != nil {
  33. fmt.Println("error opening connection,", err)
  34. return
  35. }
  36. // Wait here until CTRL-C or other term signal is received.
  37. fmt.Println("Bot is now running. Press CTRL-C to exit.")
  38. sc := make(chan os.Signal, 1)
  39. signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
  40. <-sc
  41. // Cleanly close down the Discord session.
  42. dg.Close()
  43. }
  44. // This function will be called (due to AddHandler above) every time a new
  45. // message is created on any channel that the authenticated bot has access to.
  46. //
  47. // It is called whenever a message is created but only when it's sent through a
  48. // server as we did not request IntentsDirectMessages.
  49. func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
  50. // Ignore all messages created by the bot itself
  51. // This isn't required in this specific example but it's a good practice.
  52. if m.Author.ID == s.State.User.ID {
  53. return
  54. }
  55. // In this example, we only care about messages that are "ping".
  56. if m.Content != "ping" {
  57. return
  58. }
  59. // We create the private channel with the user who sent the message.
  60. channel, err := s.UserChannelCreate(m.Author.ID)
  61. if err != nil {
  62. // If an error occurred, we failed to create the channel.
  63. //
  64. // Some common causes are:
  65. // 1. We don't share a server with the user (not possible here).
  66. // 2. We opened enough DM channels quickly enough for Discord to
  67. // label us as abusing the endpoint, blocking us from opening
  68. // new ones.
  69. fmt.Println("error creating channel:", err)
  70. s.ChannelMessageSend(
  71. m.ChannelID,
  72. "Something went wrong while sending the DM!",
  73. )
  74. return
  75. }
  76. // Then we send the message through the channel we created.
  77. _, err = s.ChannelMessageSend(channel.ID, "Pong!")
  78. if err != nil {
  79. // If an error occurred, we failed to send the message.
  80. //
  81. // It may occur either when we do not share a server with the
  82. // user (highly unlikely as we just received a message) or
  83. // the user disabled DM in their settings (more likely).
  84. fmt.Println("error sending DM message:", err)
  85. s.ChannelMessageSend(
  86. m.ChannelID,
  87. "Failed to send you a DM. "+
  88. "Did you disable DM in your privacy settings?",
  89. )
  90. }
  91. }