gambot/main.go

80 lines
1.5 KiB
Go
Raw Permalink Normal View History

2025-01-20 22:39:01 -08:00
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
_ "github.com/mattn/go-sqlite3"
)
func main() {
if err := initDB(); err != nil {
panic(err)
}
defer db.Close()
var tablePath string
flag.StringVar(&token, "t", "", "Bot Token")
flag.StringVar(&appID, "a", "", "App ID")
flag.StringVar(&guildID, "g", "", "Guild ID")
flag.StringVar(&tablePath, "p", "", "Path to table")
flag.Parse()
if err := readTable(tablePath); err != nil {
panic(err)
}
if err := runBot(token); err != nil {
panic(err)
}
}
func runBot(token string) error {
bot, err := discordgo.New("Bot " + token)
if err != nil {
return fmt.Errorf("failed to create Discord session: %w\n", err)
}
bot.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Type != discordgo.InteractionApplicationCommand {
return
}
data := i.ApplicationCommandData()
if data.Name != "gambo" {
return
}
handleMessage(s, i)
})
bot.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Printf("Logged in as %s", r.User.String())
})
_, err = bot.ApplicationCommandBulkOverwrite(appID, guildID, commands)
if err != nil {
log.Fatalf("could not register commands: %s", err)
}
err = bot.Open()
if err != nil {
return fmt.Errorf("failed to open bot session: %w\n", err)
}
defer bot.Close()
fmt.Println("Starting bot...")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
return nil
}