124 lines
2.9 KiB
Go
124 lines
2.9 KiB
Go
package relay
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"homestead/homestead_to_go/relay/commands"
|
|
"log"
|
|
"log/slog"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/disgoorg/disgo"
|
|
"github.com/disgoorg/disgo/bot"
|
|
"github.com/disgoorg/disgo/cache"
|
|
"github.com/disgoorg/disgo/discord"
|
|
"github.com/disgoorg/disgo/events"
|
|
"github.com/disgoorg/disgo/gateway"
|
|
"github.com/disgoorg/paginator"
|
|
)
|
|
|
|
func New(cfg Config) *Bot {
|
|
return &Bot{
|
|
Cfg: cfg,
|
|
Paginator: paginator.New(),
|
|
}
|
|
}
|
|
|
|
func onMessageCreate(event *events.MessageCreate) {
|
|
if event.Message.Author.Bot {
|
|
return
|
|
}
|
|
if !commands.ChannelStore.IsRegistered(event.Message.ChannelID) {
|
|
return
|
|
}
|
|
|
|
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,
|
|
content,
|
|
)
|
|
}
|
|
|
|
func plural(count int, singular, plural string) string {
|
|
if count == 1 {
|
|
return singular
|
|
}
|
|
return plural
|
|
}
|
|
|
|
func (b *Bot) SetupBot(listeners ...bot.EventListener) error {
|
|
client, err := disgo.New(b.Cfg.Bot.Token,
|
|
bot.WithGatewayConfigOpts(gateway.WithIntents(gateway.IntentGuilds, gateway.IntentGuildMessages, gateway.IntentMessageContent)),
|
|
bot.WithCacheConfigOpts(cache.WithCaches(cache.FlagGuilds)),
|
|
bot.WithEventListeners(b.Paginator),
|
|
bot.WithEventListeners(listeners...),
|
|
bot.WithEventListeners(&events.ListenerAdapter{
|
|
OnMessageCreate: onMessageCreate,
|
|
}),
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
b.Client = client
|
|
return nil
|
|
}
|
|
|
|
func (b *Bot) OnReady(_ *events.Ready) {
|
|
slog.Info(b.Cfg.Bot.Name + " ready.")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
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))
|
|
}
|
|
}
|