From 80b71a47fbd4155fb4fc4832a46e048a2cafb257 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Sat, 27 Apr 2019 12:41:17 -0400 Subject: [PATCH 1/6] Check if a user was mentioned, but ignore its own messages --- buttsbot.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/buttsbot.go b/buttsbot.go index 17e85eb..01aaff0 100644 --- a/buttsbot.go +++ b/buttsbot.go @@ -40,6 +40,7 @@ func main() { //Handlers Session.AddHandler(ready) + Session.AddHandler(messageCreate) //Open a connection to Discord err = Session.Open() @@ -66,3 +67,26 @@ 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, "butt") + } +} + +//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 +} From 2c726283dca330a6cddc32d4b1decb7c5c120e58 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Sat, 27 Apr 2019 13:38:04 -0400 Subject: [PATCH 2/6] Replace random words with butt when mentioned --- buttsbot.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/buttsbot.go b/buttsbot.go index 01aaff0..34f9975 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" ) @@ -76,7 +78,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { //Check if the bot was mentioned if isUserMentioned(m, s.State.User) == true { - s.ChannelMessageSend(m.ChannelID, "butt") + s.ChannelMessageSend(m.ChannelID, textToButt(m, s)) } } @@ -90,3 +92,15 @@ func isUserMentioned(m *discordgo.MessageCreate, u *discordgo.User) bool { } 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, " ") +} From 2574f84c65f46884820473a3915a005f37bbc8a7 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Sat, 27 Apr 2019 13:45:05 -0400 Subject: [PATCH 3/6] Replace words on random messages --- buttsbot.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/buttsbot.go b/buttsbot.go index 34f9975..84970d0 100644 --- a/buttsbot.go +++ b/buttsbot.go @@ -80,6 +80,11 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { if isUserMentioned(m, s.State.User) == true { s.ChannelMessageSend(m.ChannelID, textToButt(m, s)) } + + //Replace words in random messages + if rand.Intn(100) > 85 { + s.ChannelMessageSend(m.ChannelID, textToButt(m, s)) + } } //Easy way to check if a user was mentioned in a message From 66994e9937421d8e7302f33b69cbf922a1c1c0de Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Sat, 27 Apr 2019 13:47:29 -0400 Subject: [PATCH 4/6] Stop processing after replacing words --- buttsbot.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/buttsbot.go b/buttsbot.go index 84970d0..49a265e 100644 --- a/buttsbot.go +++ b/buttsbot.go @@ -79,11 +79,13 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { //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 } } From a9d231888586bf3c1ef23adf24ae78b0582083d5 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Sat, 27 Apr 2019 14:05:07 -0400 Subject: [PATCH 5/6] Notify Discord when tests pass and fail --- .drone.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.drone.yml b/.drone.yml index a04fdb2..449daf7 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 From e834a7ae53ecd540afe3f0781b0bdcbd1a355c15 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Sat, 27 Apr 2019 14:10:04 -0400 Subject: [PATCH 6/6] Maybe now failures will notify --- .drone.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.drone.yml b/.drone.yml index 449daf7..70eb410 100644 --- a/.drone.yml +++ b/.drone.yml @@ -19,8 +19,8 @@ steps: image: appleboy/drone-discord when: status: - - success - - failure + - success + - failure settings: webhook_id: from_secret: discord_webhook_id