diff --git a/.drone.yml b/.drone.yml index a04fdb2..70eb410 100644 --- a/.drone.yml +++ b/.drone.yml @@ -17,6 +17,10 @@ steps: - ./discord-buttsbot-go -test - name: discord image: appleboy/drone-discord + when: + status: + - success + - failure settings: webhook_id: from_secret: discord_webhook_id diff --git a/buttsbot.go b/buttsbot.go index 17e85eb..49a265e 100644 --- a/buttsbot.go +++ b/buttsbot.go @@ -5,8 +5,10 @@ import ( "fmt" "github.com/bwmarrin/discordgo" "log" + "math/rand" "os" "os/signal" + "strings" "syscall" ) @@ -40,6 +42,7 @@ func main() { //Handlers Session.AddHandler(ready) + Session.AddHandler(messageCreate) //Open a connection to Discord err = Session.Open() @@ -66,3 +69,45 @@ func ready(s *discordgo.Session, event *discordgo.Ready) { 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, " ") +}