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