Merge branch 'dev' of pocketjawa/discord-buttsbot-go into master

Version 2.0
master
pocketjawa 2019-04-27 18:12:50 +00:00 committed by Gogs
commit 3dd71c9445
2 changed files with 49 additions and 0 deletions

View File

@ -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

View File

@ -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, " ")
}