From 10852a01e2bfd3b542ede2d66fc9b727c9b3ef70 Mon Sep 17 00:00:00 2001 From: rxdn <29165304+rxdn@users.noreply.github.com> Date: Fri, 28 May 2021 23:34:31 +0100 Subject: [PATCH] Multipanel buttons --- .../endpoints/api/panel/multipanelcreate.go | 62 ++++++++----------- .../endpoints/api/panel/multipanelupdate.go | 14 +---- go.mod | 4 +- .../includes/multipaneleditmodal.tmpl | 2 +- 4 files changed, 30 insertions(+), 52 deletions(-) diff --git a/app/http/endpoints/api/panel/multipanelcreate.go b/app/http/endpoints/api/panel/multipanelcreate.go index cbbb6f4..08e4e7a 100644 --- a/app/http/endpoints/api/panel/multipanelcreate.go +++ b/app/http/endpoints/api/panel/multipanelcreate.go @@ -13,7 +13,8 @@ import ( "github.com/gin-gonic/gin" "github.com/rxdn/gdl/objects/channel" "github.com/rxdn/gdl/objects/channel/embed" - "github.com/rxdn/gdl/objects/channel/message" + "github.com/rxdn/gdl/objects/guild/emoji" + "github.com/rxdn/gdl/objects/interaction/component" "github.com/rxdn/gdl/rest" "github.com/rxdn/gdl/rest/request" "golang.org/x/sync/errgroup" @@ -56,7 +57,7 @@ func MultiPanelCreate(ctx *gin.Context) { // get premium status premiumTier := rpc.PremiumClient.GetTierByGuildId(guildId, true, botContext.Token, botContext.RateLimiter) - messageId, err := data.sendEmbed(&botContext, premiumTier > premium.None) + messageId, err := data.sendEmbed(&botContext, premiumTier > premium.None, panels) if err != nil { var unwrapped request.RestError if errors.As(err, &unwrapped); unwrapped.StatusCode == 403 { @@ -68,17 +69,6 @@ func MultiPanelCreate(ctx *gin.Context) { return } - if err := data.addReactions(&botContext, data.ChannelId, messageId, panels); err != nil { - var unwrapped request.RestError - if errors.As(err, &unwrapped); unwrapped.StatusCode == 403 { - ctx.JSON(500, utils.ErrorJson(errors.New("I do not have permission to add reactions in the provided channel"))) - } else { - ctx.JSON(500, utils.ErrorJson(err)) - } - - return - } - multiPanel := database.MultiPanel{ MessageId: messageId, ChannelId: data.ChannelId, @@ -184,13 +174,6 @@ func (d *multiPanelCreateData) validatePanels(guildId uint64) (panels []database // find panel struct for _, panel := range existingPanels { if panel.PanelId == panelId { - // check there isn't a panel with the same reaction emote - for _, previous := range panels { - if previous.ReactionEmote == panel.ReactionEmote { - return nil, errors.New("2 sub-panels cannot have the same reaction emotes") - } - } - valid = true panels = append(panels, panel) } @@ -204,7 +187,7 @@ func (d *multiPanelCreateData) validatePanels(guildId uint64) (panels []database return } -func (d *multiPanelCreateData) sendEmbed(ctx *botcontext.BotContext, isPremium bool) (messageId uint64, err error) { +func (d *multiPanelCreateData) sendEmbed(ctx *botcontext.BotContext, isPremium bool, panels []database.Panel) (uint64, error) { e := embed.NewEmbed(). SetTitle(d.Title). SetDescription(d.Content). @@ -215,22 +198,29 @@ func (d *multiPanelCreateData) sendEmbed(ctx *botcontext.BotContext, isPremium b e.SetFooter("Powered by ticketsbot.net", "https://cdn.discordapp.com/avatars/508391840525975553/ac2647ffd4025009e2aa852f719a8027.png?size=256") } - var msg message.Message - msg, err = rest.CreateMessage(ctx.Token, ctx.RateLimiter, d.ChannelId, rest.CreateMessageData{Embed: e}) + buttons := make([]component.Component, len(panels)) + for i, panel := range panels { + buttons[i] = component.BuildButton(component.Button{ + Label: panel.Title, + CustomId: panel.CustomId, + Style: component.ButtonStylePrimary, + Emoji: emoji.Emoji{ + Name: panel.ReactionEmote, + }, + }) + } + + data := rest.CreateMessageData{ + Embed: e, + Components: []component.Component{ + component.BuildActionRow(buttons...), + }, + } + + msg, err := rest.CreateMessage(ctx.Token, ctx.RateLimiter, d.ChannelId, data) if err != nil { - return + return 0, err } - messageId = msg.Id - return -} - -func (d *multiPanelCreateData) addReactions(ctx *botcontext.BotContext, channelId, messageId uint64, panels []database.Panel) (err error) { - for _, panel := range panels { - if err = rest.CreateReaction(ctx.Token, ctx.RateLimiter, channelId, messageId, panel.ReactionEmote); err != nil { - return err - } - } - - return + return msg.Id, nil } diff --git a/app/http/endpoints/api/panel/multipanelupdate.go b/app/http/endpoints/api/panel/multipanelupdate.go index 1333896..3b74f10 100644 --- a/app/http/endpoints/api/panel/multipanelupdate.go +++ b/app/http/endpoints/api/panel/multipanelupdate.go @@ -77,7 +77,7 @@ func MultiPanelUpdate(ctx *gin.Context) { premiumTier := rpc.PremiumClient.GetTierByGuildId(guildId, true, botContext.Token, botContext.RateLimiter) // send new message - messageId, err := data.sendEmbed(&botContext, premiumTier > premium.None) + messageId, err := data.sendEmbed(&botContext, premiumTier > premium.None, panels) if err != nil { var unwrapped request.RestError if errors.As(err, &unwrapped) && unwrapped.StatusCode == 403 { @@ -89,18 +89,6 @@ func MultiPanelUpdate(ctx *gin.Context) { return } - // add reactions to new message - if err := data.addReactions(&botContext, data.ChannelId, messageId, panels); err != nil { - var unwrapped request.RestError - if errors.As(err, &unwrapped) && unwrapped.StatusCode == 403 { - ctx.JSON(500, utils.ErrorJson(errors.New("I do not have permission to add reactions in the provided channel"))) - } else { - ctx.JSON(500, utils.ErrorJson(err)) - } - - return - } - // update DB updated := database.MultiPanel{ Id: multiPanel.Id, diff --git a/go.mod b/go.mod index 388bdb9..8353018 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/BurntSushi/toml v0.3.1 github.com/TicketsBot/archiverclient v0.0.0-20210220155137-a562b2f1bbbb github.com/TicketsBot/common v0.0.0-20210527174309-f7344004ae32 - github.com/TicketsBot/database v0.0.0-20210528144640-ac827439e059 + github.com/TicketsBot/database v0.0.0-20210528165404-2addee803304 github.com/TicketsBot/worker v0.0.0-20210528135955-34744f610804 github.com/apex/log v1.1.2 github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff // indirect @@ -21,7 +21,7 @@ require ( github.com/jackc/pgx/v4 v4.7.1 github.com/pasztorpisti/qs v0.0.0-20171216220353-8d6c33ee906c github.com/pkg/errors v0.9.1 - github.com/rxdn/gdl v0.0.0-20210527190730-a6b87abf7fc8 + github.com/rxdn/gdl v0.0.0-20210528152706-5da182da93db github.com/sirupsen/logrus v1.5.0 github.com/ulule/limiter/v3 v3.5.0 golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a diff --git a/public/templates/includes/multipaneleditmodal.tmpl b/public/templates/includes/multipaneleditmodal.tmpl index 88a4124..9453879 100644 --- a/public/templates/includes/multipaneleditmodal.tmpl +++ b/public/templates/includes/multipaneleditmodal.tmpl @@ -112,7 +112,7 @@ return 0; } - $('#multi-edit-panels').selectpicker('val', panel.panels.map(p => p.message_id)); + $('#multi-edit-panels').selectpicker('val', panel.panels.map(p => p.panel_id)); } async function updateMultiPanel() {