diff --git a/bunnybot.go b/bunnybot.go index 6c2bda1..f3bb276 100644 --- a/bunnybot.go +++ b/bunnybot.go @@ -1,15 +1,70 @@ package main import ( - //"flag" + "flag" "fmt" - //"os" - //"os/signal" - //"syscall" + "os" + "os/signal" + "syscall" - //"github.com/bwmarrin/discordgo" + "github.com/bwmarrin/discordgo" ) -func main() { - fmt.Println(get_redditbooru("awwnime")) +var ( + Token string +) + +func init() { + flag.StringVar(&Token, "t", "", "Bot Token") + flag.Parse() +} + +func main() { + // create Discord session + dg, err := discordgo.New("Bot " + Token) + if err != nil { + fmt.Println("Error creating Discord session, ", err) + return + } + + // register the message_create func as a callback for MessageCreate events + dg.AddHandler(message_create) + + // open a websocket connection to Discord and begin listening + err = dg.Open() + if err != nil { + fmt.Println("Error opening connection, ", err) + return + } + + // wait here until term signal + fmt.Println("Bot is now running. Press Ctrl+C to exit.") + sc := make(chan os.Signal, 1) + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) + <-sc + + // cleanly close + dg.Close() + +} + +func message_create (s *discordgo.Session, m *discordgo.MessageCreate) { + + // ignore messages by the bot + if m.Author.ID == s.State.User.ID { + return + } + + // ping + if m.Content == "ping" { + s.ChannelMessageSend(m.ChannelID, "Pong!") + } + + if m.Content == "pong" { + s.ChannelMessageSend(m.ChannelID, "Ping!") + } + + + //get_redditbooru("awwnime") + }