mid day commit
This commit is contained in:
67
relay/bot.go
67
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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,5 @@ type BotConfig struct {
|
||||
|
||||
type LogConfig struct {
|
||||
Level slog.Level `toml:"level"`
|
||||
Format string `toml:"format"`
|
||||
AddSource bool `toml:"add_source"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user