mid day commit

This commit is contained in:
2025-12-09 10:54:36 +01:00
parent 06c17cc2e9
commit e188ceab79
4 changed files with 78 additions and 44 deletions

View File

@@ -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"

47
main.go
View File

@@ -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 b.Client.Close(ctx)
defer cancel()
b.Client.Close(ctx)
}()
//
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 <user>
// $ git config user.email <email>
// $ go mod tidy; go mod download

View File

@@ -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))
}
}

View File

@@ -27,6 +27,5 @@ type BotConfig struct {
type LogConfig struct {
Level slog.Level `toml:"level"`
Format string `toml:"format"`
AddSource bool `toml:"add_source"`
}