From e188ceab79a3597722ec8be42ab836feb6d7201c Mon Sep 17 00:00:00 2001 From: Overlord Date: Tue, 9 Dec 2025 10:54:36 +0100 Subject: [PATCH] mid day commit --- config.toml | 3 +-- main.go | 51 +++++++++++++++--------------------- relay/bot.go | 67 ++++++++++++++++++++++++++++++++++++++++-------- relay/structs.go | 1 - 4 files changed, 78 insertions(+), 44 deletions(-) diff --git a/config.toml b/config.toml index 5616293..0215231 100644 --- a/config.toml +++ b/config.toml @@ -1,9 +1,8 @@ [log] level = "info" -format = "text" add_source = true [bot] dev_guilds = [] -token = "token_here" +token = "MTQ0NDI1MzEyNjM5ODg0MDkyMw.GhaGdH.FCevb77atgpDSLwz9KRZIBg-3xEo1WCnXyn9uk" name= "HomesteadRelay" diff --git a/main.go b/main.go index bce1ad8..7e82c70 100644 --- a/main.go +++ b/main.go @@ -26,13 +26,21 @@ func main() { os.Exit(-1) } - fmt.Printf("%v\n", cfg) + if err := commands.InitStorage(); err != nil { + slog.Error("Error initializing storage", "err", err) + os.Exit(-1) + } + + slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ + AddSource: cfg.Log.AddSource, + Level: cfg.Log.Level, + }))) - setupLogger(cfg.Log) slog.Info(fmt.Sprintf("Starting %s...", cfg.Bot.Name)) - b := relay.New(*cfg) + // + b := relay.New(*cfg) h := handler.New() h.Command("/ping", commands.PingHandler) h.Command("/sync", commands.SyncHandler) @@ -42,49 +50,32 @@ func main() { os.Exit(-1) } - defer func() { - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - b.Client.Close(ctx) - }() + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + + defer b.Client.Close(ctx) + defer cancel() + + // if err = handler.SyncCommands(b.Client, commands.Commands, cfg.Bot.DevGuilds); err != nil { slog.Error("Failed to sync commands", slog.Any("err", err)) } - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() if err = b.Client.OpenGateway(ctx); err != nil { slog.Error("Failed to open gateway", slog.Any("err", err)) os.Exit(-1) } - slog.Info("Bot is running. Press CTRL-C to exit.") + slog.Info("Bot is running.") + s := make(chan os.Signal, 1) signal.Notify(s, syscall.SIGINT, syscall.SIGTERM) + <-s + slog.Info("Shutting down bot...") } -func setupLogger(cfg relay.LogConfig) { - opts := &slog.HandlerOptions{ - AddSource: cfg.AddSource, - Level: cfg.Level, - } - - var sHandler slog.Handler - switch cfg.Format { - case "json": - sHandler = slog.NewJSONHandler(os.Stdout, opts) - case "text": - sHandler = slog.NewTextHandler(os.Stdout, opts) - default: - slog.Error("Unknown log format", slog.String("format", cfg.Format)) - os.Exit(-1) - } - slog.SetDefault(slog.New(sHandler)) -} - // $ git config user.name // $ git config user.email // $ go mod tidy; go mod download diff --git a/relay/bot.go b/relay/bot.go index ec22657..294527e 100644 --- a/relay/bot.go +++ b/relay/bot.go @@ -2,9 +2,11 @@ package relay import ( "context" + "fmt" "homestead/homestead_to_go/relay/commands" "log" "log/slog" + "strings" "time" "github.com/disgoorg/disgo" @@ -24,28 +26,71 @@ func New(cfg Config) *Bot { } func onMessageCreate(event *events.MessageCreate) { - // Ignore bot messages if event.Message.Author.Bot { return } - - // Check if channel is registered if !commands.ChannelStore.IsRegistered(event.Message.ChannelID) { return } - // Log the message - log.Printf("[%s] %s#%s: %s", + content := event.Message.Content + + var parts []string + if len(event.Message.Attachments) > 0 { + images := 0 + gifs := 0 + videos := 0 + files := 0 + + for _, att := range event.Message.Attachments { + if att.ContentType != nil { + switch { + case strings.HasPrefix(*att.ContentType, "image/gif"): + gifs++ + case strings.HasPrefix(*att.ContentType, "image/"): + images++ + case strings.HasPrefix(*att.ContentType, "video/"): + videos++ + default: + files++ + } + } + } + + if images > 0 { + parts = append(parts, fmt.Sprintf("%d %s", images, plural(images, "Image", "Images"))) + } + if gifs > 0 { + parts = append(parts, fmt.Sprintf("%d %s", gifs, plural(gifs, "Gif", "Gifs"))) + } + if videos > 0 { + parts = append(parts, fmt.Sprintf("%d %s", videos, plural(videos, "Video", "Videos"))) + } + if files > 0 { + parts = append(parts, fmt.Sprintf("%d %s", files, plural(files, "File", "Files"))) + } + } + + if len(event.Message.Embeds) > 0 { + parts = append(parts, fmt.Sprintf("%d %s", len(event.Message.Embeds), plural(len(event.Message.Embeds), "Embed", "Embeds"))) + } + + if len(parts) > 0 { + content = fmt.Sprintf("%s", strings.Join(parts, ", ")) + } + + log.Printf("[%s] %s: %s", event.Message.ChannelID, event.Message.Author.Username, - event.Message.Author.Discriminator, - event.Message.Content, + content, ) +} - // You can also log attachments, embeds, etc. - if len(event.Message.Attachments) > 0 { - log.Printf(" └─ Attachments: %d", len(event.Message.Attachments)) +func plural(count int, singular, plural string) string { + if count == 1 { + return singular } + return plural } func (b *Bot) SetupBot(listeners ...bot.EventListener) error { @@ -72,7 +117,7 @@ func (b *Bot) OnReady(_ *events.Ready) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - if err := b.Client.SetPresence(ctx, gateway.WithListeningActivity("you"), gateway.WithOnlineStatus(discord.OnlineStatusOnline)); err != nil { + if err := b.Client.SetPresence(ctx, gateway.WithWatchingActivity("HideoutKitty"), gateway.WithOnlineStatus(discord.OnlineStatusIdle)); err != nil { slog.Error("Failed to set presence", slog.Any("err", err)) } } diff --git a/relay/structs.go b/relay/structs.go index 214d728..fad4a23 100644 --- a/relay/structs.go +++ b/relay/structs.go @@ -27,6 +27,5 @@ type BotConfig struct { type LogConfig struct { Level slog.Level `toml:"level"` - Format string `toml:"format"` AddSource bool `toml:"add_source"` }