2023-12-26 18:39:21 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-01-21 00:27:52 -07:00
|
|
|
"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
|
|
|
{
|
2024-11-05 13:03:06 -06:00
|
|
|
Name: "himbucks",
|
2024-11-05 00:19:36 -06:00
|
|
|
Description: "Check your himbucks balance",
|
|
|
|
},
|
|
|
|
{
|
2024-11-05 13:03:06 -06:00
|
|
|
Name: "himboard",
|
2024-11-05 00:19:36 -06:00
|
|
|
Description: "View the himbucks leaderboard",
|
|
|
|
},
|
2024-11-05 13:03:06 -06:00
|
|
|
{
|
|
|
|
Name: "syncbucks",
|
|
|
|
Description: "Sync your himbucks balance with the database",
|
|
|
|
},
|
2024-11-07 02:56:56 -06:00
|
|
|
{
|
|
|
|
Name: "sendbucks",
|
|
|
|
Description: "Send himbucks to another user",
|
|
|
|
Options: []*discordgo.ApplicationCommandOption{
|
|
|
|
{
|
|
|
|
Type: discordgo.ApplicationCommandOptionUser,
|
|
|
|
Name: "user",
|
|
|
|
Description: "The user to send himbucks to",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: discordgo.ApplicationCommandOptionInteger,
|
|
|
|
Name: "amount",
|
|
|
|
Description: "Amount of himbucks to send",
|
|
|
|
Required: true,
|
|
|
|
MinValue: &[]float64{1}[0],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-10-21 23:50:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
|
2024-11-05 16:36:50 -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),
|
2024-11-07 02:56:56 -06:00
|
|
|
"himbucks": lib.HandleCommand("himbucks", 5*time.Second, command.BalanceGetCommand),
|
2024-11-05 16:37:43 -06:00
|
|
|
"himboard": lib.HandleCommand("himboard", 5*time.Second, command.LeaderboardCommand),
|
2024-11-05 16:36:50 -06:00
|
|
|
"syncbucks": lib.HandleCommand("syncbucks", 1800*time.Second, command.BalanceSyncCommand),
|
2024-11-07 02:56:56 -06:00
|
|
|
"sendbucks": lib.HandleCommand("sendbucks", 1800*time.Second, command.BalanceSendCommand),
|
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 18:01:58 -06:00
|
|
|
processorManager := lib.NewMessageProcessorManager()
|
|
|
|
|
|
|
|
// Register processors
|
|
|
|
processorManager.RegisterProcessor(lib.ProcessHimbucks)
|
|
|
|
|
2024-11-05 00:19:36 -06:00
|
|
|
dg.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
2024-11-05 18:01:58 -06:00
|
|
|
processorManager.ProcessMessage(s, m)
|
2024-11-05 00:19:36 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
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) {
|
2024-11-05 16:42:44 -06:00
|
|
|
// First, delete all existing commands
|
|
|
|
log.Println("Deleting existing commands...")
|
|
|
|
|
|
|
|
existingCommands, err := s.ApplicationCommands(s.State.User.ID, "")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error fetching existing commands: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cmd := range existingCommands {
|
|
|
|
err := s.ApplicationCommandDelete(s.State.User.ID, "", cmd.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error deleting command %s: %v", cmd.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then register the new commands
|
|
|
|
log.Println("Registering new commands...")
|
2024-10-21 23:50:17 -06:00
|
|
|
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
|
|
|
}
|