discord-buttsbot-go/buttsbot.go

117 lines
2.6 KiB
Go
Raw Normal View History

2019-04-27 02:23:04 +00:00
package main
import (
2019-04-27 04:12:46 +00:00
"flag"
"fmt"
"github.com/bwmarrin/discordgo"
2019-04-27 04:12:46 +00:00
"log"
"math/rand"
2019-04-27 04:12:46 +00:00
"os"
"os/signal"
"strings"
2019-04-27 04:12:46 +00:00
"syscall"
2019-04-27 02:23:04 +00:00
)
2019-08-17 19:28:43 +00:00
//Session for DiscordGo
2019-04-27 04:12:46 +00:00
var Session, _ = discordgo.New()
var testPtr = flag.Bool("test", false, "Test mode")
2019-08-17 19:28:43 +00:00
var botToken string
var botChannel string
2019-04-27 04:12:46 +00:00
func init() {
2019-08-17 19:28:43 +00:00
flag.StringVar(&botToken, "t", "", "Discord Authentication Token")
flag.StringVar(&botChannel, "bc", "", "Bot status channel")
flag.Parse()
2019-04-27 05:56:31 +00:00
if os.Getenv("DISCORD_TOKEN") != "" {
Session.Token = "Bot " + os.Getenv("DISCORD_TOKEN")
} else {
2019-08-17 19:28:43 +00:00
Session.Token = "Bot " + botToken
2019-04-27 04:12:46 +00:00
}
if os.Getenv("DISCORD_BOT_CHANNEL") != "" {
botChannel = os.Getenv("DISCORD_BOT_CHANNEL")
}
2019-04-27 04:12:46 +00:00
}
func main() {
2019-04-27 04:12:46 +00:00
var err error
2019-04-27 03:21:46 +00:00
fmt.Println("butts!")
2019-04-27 04:12:46 +00:00
flag.Parse()
if Session.Token == "" {
log.Println("You must provide a Discord auth token!")
return
}
2019-04-27 06:27:24 +00:00
//Handlers
Session.AddHandler(ready)
Session.AddHandler(messageCreate)
2019-04-27 06:27:24 +00:00
2019-04-27 04:12:46 +00:00
//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()
}
2019-04-27 06:27:24 +00:00
func ready(s *discordgo.Session, event *discordgo.Ready) {
if *testPtr == true {
2019-08-17 19:28:43 +00:00
s.ChannelMessageSend(botChannel, "Test complete: discord-buttsbot-go")
2019-04-27 06:27:24 +00:00
log.Printf("Test successful! Now quiting.")
s.Close()
os.Exit(0)
}
s.UpdateStatus(0, "with butts!")
if os.Getenv("BUTTSBOT_STARTUP_MESSAGE") == "TRUE" {
s.ChannelMessageSend(botChannel, "This isn't a butt...")
}
2019-04-27 06:27:24 +00:00
}
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))
2019-04-27 17:47:29 +00:00
return
}
2019-04-27 17:45:05 +00:00
//Replace words in random messages
if rand.Intn(100) > 85 {
s.ChannelMessageSend(m.ChannelID, textToButt(m, s))
2019-04-27 17:47:29 +00:00
return
2019-04-27 17:45:05 +00:00
}
}
//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)
2019-08-17 19:28:43 +00:00
for i := range words {
if rand.Intn(10) > 7 {
words[i] = "butt"
}
}
return strings.Join(words, " ")
}