hampbot/main.go

141 lines
3.5 KiB
Go
Raw Normal View History

2023-08-18 20:22:20 -07:00
package main
import (
2023-08-18 20:52:26 -07:00
"math/rand"
2023-08-18 20:22:20 -07:00
"os"
"os/signal"
"syscall"
2023-08-18 20:52:26 -07:00
"time"
2023-08-18 20:22:20 -07:00
"github.com/bwmarrin/discordgo"
"github.com/jackmerrill/hampbot/internal/commands/fun"
studentlife "github.com/jackmerrill/hampbot/internal/commands/studentlife"
util "github.com/jackmerrill/hampbot/internal/commands/util"
"github.com/jackmerrill/hampbot/internal/listeners"
2023-08-18 21:01:43 -07:00
"github.com/jackmerrill/hampbot/internal/utils/config"
2023-08-21 15:26:31 -07:00
"github.com/jackmerrill/hampbot/internal/utils/embed"
2023-08-18 20:22:20 -07:00
"github.com/zekroTJA/shireikan"
"github.com/charmbracelet/log"
)
func main() {
token := os.Getenv("TOKEN")
session, err := discordgo.New("Bot " + token)
if err != nil {
panic(err)
}
log.Info("Starting bot...")
err = session.Open()
if err != nil {
panic(err)
}
defer func() {
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
}()
handler := shireikan.New(&shireikan.Config{
2023-08-18 21:01:43 -07:00
GeneralPrefix: config.BotPrefix,
2023-08-18 20:22:20 -07:00
AllowBots: false,
AllowDM: true,
ExecuteOnEdit: true,
InvokeToLower: true,
UseDefaultHelpCommand: true,
OnError: func(ctx shireikan.Context, typ shireikan.ErrorType, err error) {
2023-09-07 10:39:57 -07:00
if typ != shireikan.ErrTypCommandNotFound {
ctx.GetSession().ChannelMessageSendComplex(ctx.GetChannel().ID, &discordgo.MessageSend{
Embed: embed.NewErrorEmbed(ctx).SetTitle("Error").SetDescription(err.Error()).MessageEmbed,
Reference: ctx.GetMessage().Reference(),
})
log.Error(err)
}
2023-08-18 20:22:20 -07:00
},
})
2023-08-18 20:52:26 -07:00
log.Info("Initializing Random")
rand.Seed(time.Now().UnixNano())
2023-08-18 20:22:20 -07:00
log.Info("Registering commands...")
handler.Register(&util.Ping{})
log.Debug("Registered ping command")
handler.Register(&studentlife.Laundry{})
log.Debug("Registered laundry command")
handler.Register(&util.Steal{})
log.Debug("Registered steal command")
handler.Register(&fun.AI{})
log.Debug("Registered ai command")
2023-08-18 20:52:26 -07:00
handler.Register(&fun.XKCD{})
log.Debug("Registered xkcd command")
2023-08-18 21:01:43 -07:00
handler.Register(&fun.Dalle{})
log.Debug("Registered dalle command")
2023-08-18 22:27:57 -07:00
handler.Register(&studentlife.PVTA{})
log.Debug("Registered PVTA command")
2023-08-19 15:36:17 -07:00
handler.Register(&studentlife.Abbreviation{})
log.Debug("Registered abbreviation command")
err = studentlife.InitAll(session)
if err != nil {
log.Error("Failed to initialize laundry notify- skipping.")
} else {
handler.Register(&studentlife.LaundryNotify{})
}
2023-09-07 10:39:57 -07:00
2023-08-19 15:36:17 -07:00
log.Debug("Registered laundry notify command")
2023-09-07 10:39:57 -07:00
handler.Register(&studentlife.Where{})
log.Debug("Registered where command")
2023-10-20 13:08:40 -07:00
handler.Register(&fun.MetricTime{})
log.Debug("Registered metrictime command")
2024-01-16 20:20:43 -08:00
handler.Register(&util.VerifyCommand{})
go util.StartWebserver(session)
log.Debug("Registered verify command")
2023-08-18 20:52:26 -07:00
log.Info("Registered all commands")
2023-08-18 22:27:57 -07:00
log.Info("Setting up activities...")
activities := config.Statuses
go func() {
for {
activity := activities[rand.Intn(len(activities))]
session.UpdateStatusComplex(discordgo.UpdateStatusData{
Activities: []*discordgo.Activity{&activity},
Status: "online",
})
2023-08-19 15:49:57 -07:00
time.Sleep(1 * time.Minute)
2023-08-18 22:27:57 -07:00
}
}()
log.Info("Setting up listeners...")
deleteHandler := &listeners.MessageDeleteListener{}
editHandler := &listeners.MessageEditListener{}
session.AddHandler(func(s *discordgo.Session, e *discordgo.MessageCreate) {
config.MessageLog[e.ID] = *e.Message
})
session.AddHandler(deleteHandler.Exec)
session.AddHandler(editHandler.Exec)
2023-08-18 20:22:20 -07:00
handler.Setup(session)
log.Info("Bot is now running. Press CTRL-C to exit.")
}