Skip to content

Commit

Permalink
add branding context command
Browse files Browse the repository at this point in the history
  • Loading branch information
mlnrDev committed Dec 29, 2023
1 parent daf959c commit a602954
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 51 deletions.
65 changes: 28 additions & 37 deletions handlers/dearrow.go → handlers/branding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handlers

import (
"bytes"
"dearrow-thumbnails/types"
"dearrow-thumbnails/util"
"fmt"
"io"
Expand All @@ -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.
Expand All @@ -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 {
Expand All @@ -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())
}
18 changes: 11 additions & 7 deletions handlers/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
14 changes: 10 additions & 4 deletions handlers/delete_embeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ 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.").
Build())
}
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.").
Expand All @@ -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.").
Expand Down
34 changes: 34 additions & 0 deletions handlers/mode.go
Original file line number Diff line number Diff line change
@@ -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())
}
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"io"
"log/slog"
"net/http"
"net/url"
"os"
"os/signal"
"regexp"
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions util/embed.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit a602954

Please sign in to comment.