the-farting-whale/main.go

104 lines
2.3 KiB
Go
Raw Permalink Normal View History

2023-05-03 22:11:03 +00:00
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"strings"
2023-12-08 12:39:52 +00:00
"time"
"math/rand"
2023-05-03 22:11:03 +00:00
"github.com/bwmarrin/discordgo"
"github.com/spf13/viper"
)
func main() {
vpr := viper.GetViper()
vpr.SetConfigFile(".env")
var token string = ""
if err := vpr.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Fatalln("Config file (.env) not found!")
} else {
log.Println("Failed to parse config!")
}
}
if vpr.GetString("DISCORD_BOT_TOKEN") == "" {
token = os.Getenv("DISCORD_BOT_TOKEN")
if token == "" {
log.Fatalln("Couldn't fetch bot token!")
}
} else {
token = vpr.GetString("DISCORD_BOT_TOKEN")
}
dg, err := discordgo.New("Bot " + token)
if err != nil {
log.Fatalf("Error creating Discord session: %s\n", err)
}
dg.AddHandler(messageCreate)
dg.Identify.Intents = discordgo.IntentsGuildMessages
// connect to discord
err = dg.Open()
if err != nil {
log.Fatalf("Couldn't connect to DIscord: %s\n", err)
}
// wait until ctrl+c or other term signal is received
fmt.Println("The Farting Whale is online!")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
// graceful shutdown
dg.Close()
}
// incoming message handler
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// ignore messages sent by the bot.
if m.Author.ID == s.State.User.ID {
return
}
2023-12-08 12:39:52 +00:00
if strings.HasPrefix(m.Content, "!ask") {
possibleAnswers := []string{
"Better not tell you now.",
"Signs point to yes.",
"My reply is no.",
"Yes.",
"Without a doubt.",
"I don't think so.",
"I doubt it.",
"NO!",
"Well... can't say for sure but maybe.",
"I think you need sleep.",
"Depends.",
"Skibidi dop dop dop yes yes yes gang gang!!!111",
"I don't think this is a good idea, honestly.",
"SHUT. THE. FUCK. UP",
"Hmm, let me think about that for a bit.",
}
rand.Seed(time.Now().Unix())
reply := possibleAnswers[rand.Intn(len(possibleAnswers))]
s.ChannelMessageSend(m.ChannelID, reply)
return
}
2023-05-03 22:11:03 +00:00
if strings.HasPrefix(m.Content, "!fart") {
c := strings.Fields(m.Content)
if len(c) == 1 {
s.ChannelMessageSend(m.ChannelID, ":whale2: :dash:")
} else if len(c) == 2 {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf(":whale2: :dash: %s", c[1]))
}
return
}
2023-05-04 11:36:48 +00:00
}