himbot/main.go

135 lines
3.3 KiB
Go
Raw Normal View History

2023-12-26 18:39:21 -07:00
package main
import (
"himbot/command"
2024-10-22 17:07:53 -06:00
"himbot/lib"
2023-12-26 18:39:21 -07:00
"log"
"os"
"os/signal"
2024-10-21 23:50:17 -06:00
"syscall"
2024-10-22 17:07:53 -06:00
"time"
2023-12-26 18:39:21 -07:00
2024-10-21 23:50:17 -06:00
"github.com/bwmarrin/discordgo"
2023-12-26 18:39:21 -07:00
"github.com/joho/godotenv"
)
2024-10-21 23:50:17 -06:00
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,
},
2023-12-26 18:39:21 -07:00
},
},
2024-10-22 00:01:19 -06:00
{
Name: "markov",
2024-10-22 00:10:38 -06:00
Description: "Why did the Markov chain break up with its partner? Because it couldn't handle the past!",
2024-10-22 09:49:52 -06:00
Options: []*discordgo.ApplicationCommandOption{
2024-10-22 12:24:02 -06:00
{
Type: discordgo.ApplicationCommandOptionInteger,
Name: "messages",
Description: "Number of messages to use (default: 100, max: 1000)",
Required: false,
},
},
2024-10-22 00:01:19 -06:00
},
2024-11-05 00:19:36 -06:00
{
Name: "balance",
Description: "Check your himbucks balance",
},
{
Name: "leaderboard",
Description: "View the himbucks leaderboard",
},
2024-10-21 23:50:17 -06:00
}
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
2024-11-05 00:19:36 -06:00
"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),
2024-10-21 23:50:17 -06:00
}
)
2023-12-26 18:39:21 -07:00
func main() {
2024-10-20 22:45:16 -06:00
godotenv.Load(".env")
2023-12-26 18:39:21 -07:00
2024-11-04 01:23:57 -06:00
err := lib.InitDB()
if err != nil {
log.Fatalf("Failed to initialize database: %v", err)
}
2023-12-26 18:39:21 -07:00
token := os.Getenv("DISCORD_TOKEN")
2024-03-31 13:44:49 -06:00
2024-10-20 22:45:16 -06:00
if token == "" {
log.Fatalln("No $DISCORD_TOKEN given.")
2023-12-26 18:39:21 -07:00
}
2024-10-21 23:50:17 -06:00
dg, err := discordgo.New("Bot " + token)
if err != nil {
log.Fatalf("Error creating Discord session: %v", err)
2023-12-26 18:39:21 -07:00
}
2024-10-21 23:50:17 -06:00
dg.AddHandler(ready)
dg.AddHandler(interactionCreate)
2024-11-05 00:19:36 -06:00
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
2023-12-26 18:39:21 -07:00
2024-10-21 23:50:17 -06:00
err = dg.Open()
if err != nil {
log.Fatalf("Error opening connection: %v", err)
2023-12-26 18:39:21 -07:00
}
2024-10-21 23:50:17 -06:00
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
2024-11-04 01:23:57 -06:00
if lib.DBClient != nil {
lib.DBClient.Close()
}
2024-10-21 23:50:17 -06:00
dg.Close()
2023-12-26 18:39:21 -07:00
}
2024-10-21 23:50:17 -06:00
func ready(s *discordgo.Session, event *discordgo.Ready) {
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
}
2023-12-26 18:39:21 -07:00
2024-10-21 23:50:17 -06:00
func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
}
2023-12-26 18:39:21 -07:00
2024-10-21 23:50:17 -06:00
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
}
2023-12-26 18:39:21 -07:00
}