diff --git a/handlers/dearrow.go b/handlers/branding.go similarity index 57% rename from handlers/dearrow.go rename to handlers/branding.go index adf738f..013f762 100644 --- a/handlers/dearrow.go +++ b/handlers/branding.go @@ -2,7 +2,6 @@ package handlers import ( "bytes" - "dearrow-thumbnails/types" "dearrow-thumbnails/util" "fmt" "io" @@ -12,16 +11,38 @@ import ( "github.com/disgoorg/disgo/discord" "github.com/disgoorg/disgo/handler" "github.com/disgoorg/json" - "github.com/schollz/jsonstore" ) var ( videoIDRegex = regexp.MustCompile(`[a-zA-Z0-9-_]{11}`) ) -func (h *Handler) HandleBranding(event *handler.CommandEvent) (err error) { +func (h *Handler) HandleBrandingSlash(event *handler.CommandEvent) error { data := event.SlashCommandInteractionData() videoID := videoIDRegex.FindString(data.String("video")) + hide, ok := data.OptBool("hide") + if !ok { + hide = true + } + return h.handleBranding(event, videoID, hide) +} + +func (h *Handler) HandleBrandingContext(event *handler.CommandEvent) error { + var videoID string + + data := event.MessageCommandInteractionData() + message := data.TargetMessage() + embeds := message.Embeds + if len(embeds) != 0 { + videoID = util.ParseVideoID(embeds[0]) + } + if videoID == "" { + videoID = videoIDRegex.FindString(message.Content) + } + return h.handleBranding(event, videoID, true) +} + +func (h *Handler) handleBranding(event *handler.CommandEvent, videoID string, hide bool) error { messageBuilder := discord.NewMessageCreateBuilder().SetEphemeral(true) if videoID == "" { return event.CreateMessage(messageBuilder. @@ -35,16 +56,16 @@ func (h *Handler) HandleBranding(event *handler.CommandEvent) (err error) { SetContent("DeArrow API failed to respond within 2 seconds."). Build()) } - return + return err } defer rs.Body.Close() b, err := io.ReadAll(rs.Body) if err != nil { - return + return err } var out bytes.Buffer - if err = json.Indent(&out, b, "", " "); err != nil { - return + if err := json.Indent(&out, b, "", " "); err != nil { + return err } content := fmt.Sprintf("```json\n%s\n```", out.String()) if len(content) > 4096 { @@ -55,38 +76,8 @@ func (h *Handler) HandleBranding(event *handler.CommandEvent) (err error) { embedBuilder := discord.NewEmbedBuilder() embedBuilder.SetColor(0x001BFF) embedBuilder.SetDescription(content) - - hide, ok := data.OptBool("hide") - if !ok { - hide = true - } return event.CreateMessage(messageBuilder. SetEmbeds(embedBuilder.Build()). SetEphemeral(hide). Build()) } - -func (h *Handler) HandleModeGet(event *handler.CommandEvent) error { - return event.CreateMessage(discord.NewMessageCreateBuilder(). - SetContentf("Current mode is set to **%s**.", h.Bot.GetGuildData(*event.GuildID()).ThumbnailMode). - SetEphemeral(true). - Build()) -} - -func (h *Handler) HandleModeSet(event *handler.CommandEvent) (err error) { - data := event.SlashCommandInteractionData() - guildID := event.GuildID() - thumbnailMode := types.ThumbnailMode(data.Int("mode")) - if err = h.Bot.Keystore.Set(guildID.String(), types.GuildData{ - ThumbnailMode: thumbnailMode, - }); err != nil { - return - } - if err = jsonstore.Save(h.Bot.Keystore, h.Config.StoragePath); err != nil { - return - } - return event.CreateMessage(discord.NewMessageCreateBuilder(). - SetContentf("Mode has been set to **%s**.", thumbnailMode). - SetEphemeral(true). - Build()) -} diff --git a/handlers/commands.go b/handlers/commands.go index 2798c26..2f41514 100644 --- a/handlers/commands.go +++ b/handlers/commands.go @@ -2,11 +2,12 @@ package handlers import ( "dearrow-thumbnails/internal" + "log/slog" + "github.com/disgoorg/disgo/discord" "github.com/disgoorg/disgo/events" "github.com/disgoorg/disgo/handler" "github.com/lmittmann/tint" - "log/slog" ) func NewHandler(b *internal.Bot, c *internal.Config) *Handler { @@ -25,15 +26,18 @@ func NewHandler(b *internal.Bot, c *internal.Config) *Handler { Router: mux, } handlers.Group(func(r handler.Router) { - r.Route("/dearrow", func(r handler.Router) { - r.Route("/mode", func(r handler.Router) { - r.Command("/get", handlers.HandleModeGet) - r.Command("/set", handlers.HandleModeSet) - }) - r.Command("/branding", handlers.HandleBranding) + r.Route("/mode", func(r handler.Router) { + r.Command("/get", handlers.HandleModeGet) + r.Command("/set", handlers.HandleModeSet) }) }) + handlers.Group(func(r handler.Router) { + r.Command("/branding", handlers.HandleBrandingSlash) + r.Command("/Fetch branding", handlers.HandleBrandingContext) + }) + handlers.Command("/Delete embeds", handlers.HandleDeleteEmbeds) + //handlers.Command("/Download video", handlers.HandleDownloadVideo) return handlers } diff --git a/handlers/delete_embeds.go b/handlers/delete_embeds.go index 8d79b66..ad38d88 100644 --- a/handlers/delete_embeds.go +++ b/handlers/delete_embeds.go @@ -5,10 +5,16 @@ import ( "github.com/disgoorg/disgo/handler" ) -func (h *Handler) HandleDeleteEmbeds(event *handler.CommandEvent) (err error) { +func (h *Handler) HandleDeleteEmbeds(event *handler.CommandEvent) error { data := event.MessageCommandInteractionData() message := data.TargetMessage() + messageRef := message.MessageReference messageBuilder := discord.NewMessageCreateBuilder().SetEphemeral(true) + if messageRef == nil || messageRef.MessageID == nil { + return event.CreateMessage(messageBuilder. + SetContent("Message is not a reply."). + Build()) + } if message.Author.ID != h.Config.DeArrowUserID { return event.CreateMessage(messageBuilder. SetContent("Message is not a DeArrow reply."). @@ -16,7 +22,7 @@ func (h *Handler) HandleDeleteEmbeds(event *handler.CommandEvent) (err error) { } rest := event.Client().Rest() channelID := event.Channel().ID() - parent, err := rest.GetMessage(channelID, *message.MessageReference.MessageID) + parent, err := rest.GetMessage(channelID, *messageRef.MessageID) if err != nil { return event.CreateMessage(messageBuilder. SetContent("Failed to fetch the parent message."). @@ -27,8 +33,8 @@ func (h *Handler) HandleDeleteEmbeds(event *handler.CommandEvent) (err error) { SetContent("Only the message author can delete DeArrow embeds."). Build()) } - if err = rest.DeleteMessage(channelID, message.ID); err != nil { - return + if err := rest.DeleteMessage(channelID, message.ID); err != nil { + return err } return event.CreateMessage(messageBuilder. SetContent("Embeds have been deleted."). diff --git a/handlers/mode.go b/handlers/mode.go new file mode 100644 index 0000000..8170203 --- /dev/null +++ b/handlers/mode.go @@ -0,0 +1,34 @@ +package handlers + +import ( + "dearrow-thumbnails/types" + + "github.com/disgoorg/disgo/discord" + "github.com/disgoorg/disgo/handler" + "github.com/schollz/jsonstore" +) + +func (h *Handler) HandleModeGet(event *handler.CommandEvent) error { + return event.CreateMessage(discord.NewMessageCreateBuilder(). + SetContentf("Current mode is set to **%s**.", h.Bot.GetGuildData(*event.GuildID()).ThumbnailMode). + SetEphemeral(true). + Build()) +} + +func (h *Handler) HandleModeSet(event *handler.CommandEvent) (err error) { + data := event.SlashCommandInteractionData() + guildID := event.GuildID() + thumbnailMode := types.ThumbnailMode(data.Int("mode")) + if err = h.Bot.Keystore.Set(guildID.String(), types.GuildData{ + ThumbnailMode: thumbnailMode, + }); err != nil { + return + } + if err = jsonstore.Save(h.Bot.Keystore, h.Config.StoragePath); err != nil { + return + } + return event.CreateMessage(discord.NewMessageCreateBuilder(). + SetContentf("Mode has been set to **%s**.", thumbnailMode). + SetEphemeral(true). + Build()) +} diff --git a/main.go b/main.go index 863043d..62504db 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,6 @@ import ( "io" "log/slog" "net/http" - "net/url" "os" "os/signal" "regexp" @@ -162,8 +161,7 @@ func replaceYouTubeEmbeds(bot *internal.Bot, event *events.GenericGuildMessage) if provider == nil || provider.Name != "YouTube" { continue } - u, _ := url.Parse(embed.URL) - videoID := u.Query().Get("v") + videoID := util.ParseVideoID(embed) if videoID == "" { continue } diff --git a/util/embed.go b/util/embed.go new file mode 100644 index 0000000..3297b70 --- /dev/null +++ b/util/embed.go @@ -0,0 +1,12 @@ +package util + +import ( + "net/url" + + "github.com/disgoorg/disgo/discord" +) + +func ParseVideoID(embed discord.Embed) string { + u, _ := url.Parse(embed.URL) + return u.Query().Get("v") +}