package main import ( "flag" "fmt" "github.com/bwmarrin/discordgo" "log" "math/rand" "os" "os/signal" "strings" "syscall" ) var Session, _ = discordgo.New() var testPtr = flag.Bool("test", false, "Test mode") var bot_token string var bot_channel string func init() { flag.StringVar(&bot_token, "t", "", "Discord Authentication Token") flag.StringVar(&bot_channel, "bc", "", "Bot status channel") flag.Parse() if os.Getenv("DISCORD_TOKEN") != "" { Session.Token = "Bot " + os.Getenv("DISCORD_TOKEN") } else { Session.Token = "Bot " + bot_token } if os.Getenv("DISCORD_BOT_CHANNEL") != "" { bot_channel = os.Getenv("DISCORD_BOT_CHANNEL") } } func main() { var err error fmt.Println("butts!") flag.Parse() if Session.Token == "" { log.Println("You must provide a Discord auth token!") return } //Handlers Session.AddHandler(ready) Session.AddHandler(messageCreate) //Open a connection to Discord err = Session.Open() if err != nil { log.Printf("Error opening connection to Discord, %s\n", err) os.Exit(1) } //Wait for a CTRL-C log.Printf("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 Session.Close() } func ready(s *discordgo.Session, event *discordgo.Ready) { if *testPtr == true { s.ChannelMessageSend(bot_channel, "Test complete: discord-buttsbot-go") log.Printf("Test successful! Now quiting.") s.Close() os.Exit(0) } s.UpdateStatus(0, "with butts!") s.ChannelMessageSend(bot_channel, "This isn't a butt...") } func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { //Ignore messages by itself if m.Author.ID == s.State.User.ID { return } //Check if the bot was mentioned if isUserMentioned(m, s.State.User) == true { s.ChannelMessageSend(m.ChannelID, textToButt(m, s)) return } //Replace words in random messages if rand.Intn(100) > 85 { s.ChannelMessageSend(m.ChannelID, textToButt(m, s)) return } } //Easy way to check if a user was mentioned in a message func isUserMentioned(m *discordgo.MessageCreate, u *discordgo.User) bool { //mentioned := false for _, user := range m.Mentions { if user.ID == u.ID { return true } } return false } //Replace words with butt func textToButt(m *discordgo.MessageCreate, s *discordgo.Session) string { cleanmessage, _ := m.ContentWithMoreMentionsReplaced(s) words := strings.Fields(cleanmessage) for i, _ := range words { if rand.Intn(10) > 7 { words[i] = "butt" } } return strings.Join(words, " ") }