himbot/main.go
2024-11-05 00:19:36 -06:00

134 lines
3.3 KiB
Go

package main
import (
"himbot/command"
"himbot/lib"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
var (
commands = []*discordgo.ApplicationCommand{
{
Name: "ping",
Description: "ping pong!",
},
{
Name: "hs",
Description: "This command was your nickname in highschool!",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "nickname",
Description: "Your nickname in highschool.",
Required: true,
},
},
},
{
Name: "markov",
Description: "Why did the Markov chain break up with its partner? Because it couldn't handle the past!",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionInteger,
Name: "messages",
Description: "Number of messages to use (default: 100, max: 1000)",
Required: false,
},
},
},
{
Name: "balance",
Description: "Check your himbucks balance",
},
{
Name: "leaderboard",
Description: "View the himbucks leaderboard",
},
}
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"ping": lib.HandleCommand("ping", 5*time.Second, command.PingCommand),
"hs": lib.HandleCommand("hs", 10*time.Second, command.HsCommand),
"markov": lib.HandleCommand("markov", 30*time.Second, command.MarkovCommand),
"balance": lib.HandleCommand("balance", 5*time.Second, command.BalanceCommand),
"leaderboard": lib.HandleCommand("leaderboard", 5*time.Second, command.LeaderboardCommand),
}
)
func main() {
godotenv.Load(".env")
err := lib.InitDB()
if err != nil {
log.Fatalf("Failed to initialize database: %v", err)
}
token := os.Getenv("DISCORD_TOKEN")
if token == "" {
log.Fatalln("No $DISCORD_TOKEN given.")
}
dg, err := discordgo.New("Bot " + token)
if err != nil {
log.Fatalf("Error creating Discord session: %v", err)
}
dg.AddHandler(ready)
dg.AddHandler(interactionCreate)
dg.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
if err := lib.ProcessMessage(s, m); err != nil {
log.Printf("Error processing message for himbucks: %v", err)
}
})
dg.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages
err = dg.Open()
if err != nil {
log.Fatalf("Error opening connection: %v", err)
}
log.Println("Bot is now running. Press CTRL-C to exit.")
registerCommands(dg)
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
if lib.DBClient != nil {
lib.DBClient.Close()
}
dg.Close()
}
func ready(s *discordgo.Session, event *discordgo.Ready) {
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
}
func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
}
func registerCommands(s *discordgo.Session) {
log.Println("Registering commands...")
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
for i, v := range commands {
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, "", v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}
registeredCommands[i] = cmd
}
}