prun-trade-bot-go/cmd/bot/main.go

63 lines
1.4 KiB
Go
Raw Normal View History

2024-07-17 14:56:53 +02:00
package main
import (
"code.lowsec.club/okawari/go-discord-bot/lib/bot"
"code.lowsec.club/okawari/go-discord-bot/lib/commands"
"code.lowsec.club/okawari/go-discord-bot/lib/helpers"
"code.lowsec.club/okawari/go-discord-bot/lib/logging"
"context"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
"os"
"os/signal"
)
var discord *discordgo.Session
func main() {
_ = logging.InitLogging()
err := godotenv.Load()
if err != nil {
log.Err(err).Msg("Unable to load dot env files")
}
log.Info().Msg("Starting discord bot")
ctx := context.Background()
db, err := helpers.CreateConnection(&gorm.Config{
Logger: logging.DBLogger{Logger: log.With().Str("module", "sql").Logger()},
})
if err != nil {
log.Panic().Str("module", "sql").Err(err).Msg("unable to connect to database")
}
helpers.AutoMigrate(db)
helpers.ImportFromFio(db)
ctx = helpers.AddDb(ctx, db)
discord, err := bot.New(ctx, "trader", os.Getenv("DISCORD_TOKEN"))
if err != nil {
log.Err(err).Msg("Unable to create discord bot")
}
discord.AddCommand(commands.StockCommand{}, true)
discord.AddCommand(commands.PriceCommand{}, true)
err = discord.Open()
if err != nil {
log.Fatal().Err(err).Msg("Unable to start discord session")
}
defer discord.Close()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
log.Info().Msg("Press Ctrl+C to exit")
<-stop
log.Info().Msg("Shutting down")
}