- Added command handler for processing Discord interactions related to lights and switches. - Implemented command registration for light control commands: list, on, off, and toggle. - Created a gRPC client for communicating with the home automation gateway. - Developed application logic for handling light and switch commands, including listing, turning on/off, and toggling lights. - Introduced telemetry setup for OpenTelemetry integration. - Added configuration loading for Discord token, gateway address, and OpenTelemetry endpoint. - Defined core driven ports for interacting with the home automation gateway.
93 lines
2.3 KiB
Go
93 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
"github.com/joho/godotenv"
|
|
|
|
discordadapter "gitea.nik4nao.com/nik/home-services/discord-bot/internal/adapters/primary/discord"
|
|
"gitea.nik4nao.com/nik/home-services/discord-bot/internal/adapters/secondary/gateway"
|
|
"gitea.nik4nao.com/nik/home-services/discord-bot/internal/app"
|
|
"gitea.nik4nao.com/nik/home-services/discord-bot/internal/config"
|
|
"gitea.nik4nao.com/nik/home-services/discord-bot/internal/telemetry"
|
|
)
|
|
|
|
var version = "dev"
|
|
|
|
func main() {
|
|
_ = godotenv.Load()
|
|
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
slog.Error("config error", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
shutdown, err := telemetry.Setup(ctx, cfg, version)
|
|
if err != nil {
|
|
slog.Error("telemetry setup failed", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
haClient, err := gateway.New(ctx, cfg.HAGatewayAddr)
|
|
if err != nil {
|
|
slog.Error("ha-gateway client setup failed", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
defer func() {
|
|
if err := haClient.Close(); err != nil {
|
|
slog.Error("ha-gateway client close failed", "err", err)
|
|
}
|
|
}()
|
|
|
|
commandApp := app.NewCommandApp(haClient)
|
|
|
|
session, err := discordgo.New("Bot " + cfg.DiscordToken)
|
|
if err != nil {
|
|
slog.Error("create discord session failed", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
session.Identify.Intents = discordgo.IntentsGuilds
|
|
|
|
handler := discordadapter.NewHandler(commandApp)
|
|
handler.Register(session)
|
|
|
|
if err := session.Open(); err != nil {
|
|
slog.Error("open discord session failed", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if err := discordadapter.RegisterCommands(session, cfg.GuildID); err != nil {
|
|
slog.Error("register discord commands failed", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
scope := "global"
|
|
if cfg.GuildID != "" {
|
|
scope = cfg.GuildID
|
|
}
|
|
slog.Info("discord bot started", "command_scope", scope, "ha_gateway_addr", cfg.HAGatewayAddr, "version", version)
|
|
|
|
<-ctx.Done()
|
|
slog.InfoContext(ctx, "shutting down discord bot")
|
|
|
|
if err := session.Close(); err != nil {
|
|
slog.ErrorContext(ctx, "close discord session failed", "err", err)
|
|
}
|
|
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := shutdown(shutdownCtx); err != nil {
|
|
slog.Error("telemetry shutdown error", "err", err)
|
|
}
|
|
}
|