10 "github.com/bwmarrin/discordgo"
13 // Variables used for command line parameters
20 flag.StringVar(&Token, "t", "", "Bot Token")
26 // Create a new Discord session using the provided bot token.
27 dg, err := discordgo.New("Bot " + Token)
29 fmt.Println("error creating Discord session,", err)
33 // Register the messageCreate func as a callback for MessageCreate events.
34 dg.AddHandler(messageCreate)
36 // Open a websocket connection to Discord and begin listening.
39 fmt.Println("error opening connection,", err)
43 // Wait here until CTRL-C or other term signal is received.
44 fmt.Println("Bot is now running. Press CTRL-C to exit.")
45 sc := make(chan os.Signal, 1)
46 signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
49 // Cleanly close down the Discord session.
53 // This function will be called (due to AddHandler above) every time a new
54 // message is created on any channel that the autenticated bot has access to.
55 func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
57 // Ignore all messages created by the bot itself
58 // This isn't required in this specific example but it's a good practice.
59 if m.Author.ID == s.State.User.ID {
62 // If the message is "ping" reply with "Pong!"
63 if m.Content == "bad bot" {
64 s.ChannelMessageSend(m.ChannelID, "the worst bot")
67 // If the message is "pong" reply with "Ping!"
68 if m.Content == "pong" {
69 s.ChannelMessageSend(m.ChannelID, "Ping!")