Resend panels

This commit is contained in:
rxdn 2021-09-13 17:51:17 +01:00
parent 6f1d9c13fc
commit 514fc7d3b4
13 changed files with 436 additions and 128 deletions

View File

@ -12,13 +12,8 @@ import (
"github.com/TicketsBot/database"
"github.com/gin-gonic/gin"
"github.com/rxdn/gdl/objects/channel"
"github.com/rxdn/gdl/objects/channel/embed"
"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"
"math"
)
type multiPanelCreateData struct {
@ -29,6 +24,16 @@ type multiPanelCreateData struct {
Panels []int `json:"panels"`
}
func (d *multiPanelCreateData) IntoMessageData(isPremium bool) multiPanelMessageData {
return multiPanelMessageData{
ChannelId: d.ChannelId,
Title: d.Title,
Content: d.Content,
Colour: int(d.Colour),
IsPremium: isPremium,
}
}
func MultiPanelCreate(ctx *gin.Context) {
guildId := ctx.Keys["guildid"].(uint64)
@ -62,7 +67,8 @@ func MultiPanelCreate(ctx *gin.Context) {
return
}
messageId, err := data.sendEmbed(&botContext, premiumTier > premium.None, panels)
messageData := data.IntoMessageData(premiumTier > premium.None)
messageId, err := messageData.send(&botContext, panels)
if err != nil {
var unwrapped request.RestError
if errors.As(err, &unwrapped); unwrapped.StatusCode == 403 {
@ -197,61 +203,3 @@ func (d *multiPanelCreateData) validatePanels(guildId uint64) (panels []database
return
}
func (d *multiPanelCreateData) sendEmbed(ctx *botcontext.BotContext, isPremium bool, panels []database.Panel) (uint64, error) {
e := embed.NewEmbed().
SetTitle(d.Title).
SetDescription(d.Content).
SetColor(int(d.Colour))
if !isPremium {
// TODO: Don't harcode
e.SetFooter("Powered by ticketsbot.net", "https://cdn.discordapp.com/avatars/508391840525975553/ac2647ffd4025009e2aa852f719a8027.png?size=256")
}
buttons := make([]component.Component, len(panels))
for i, panel := range panels {
var buttonEmoji *emoji.Emoji
if panel.ReactionEmote != "" {
buttonEmoji = &emoji.Emoji{
Name: panel.ReactionEmote,
}
}
buttons[i] = component.BuildButton(component.Button{
Label: panel.Title,
CustomId: panel.CustomId,
Style: component.ButtonStyle(panel.ButtonStyle),
Emoji: buttonEmoji,
})
}
var rows []component.Component
for i := 0; i <= int(math.Ceil(float64(len(buttons)/5))); i++ {
lb := i * 5
ub := lb + 5
if ub >= len(buttons) {
ub = len(buttons)
}
if lb >= ub {
break
}
row := component.BuildActionRow(buttons[lb:ub]...)
rows = append(rows, row)
}
data := rest.CreateMessageData{
Embeds: []*embed.Embed{e},
Components: rows,
}
msg, err := rest.CreateMessage(ctx.Token, ctx.RateLimiter, d.ChannelId, data)
if err != nil {
return 0, err
}
return msg.Id, nil
}

View File

@ -0,0 +1,88 @@
package api
import (
"github.com/TicketsBot/GoPanel/botcontext"
"github.com/TicketsBot/database"
"github.com/rxdn/gdl/objects/channel/embed"
"github.com/rxdn/gdl/objects/guild/emoji"
"github.com/rxdn/gdl/objects/interaction/component"
"github.com/rxdn/gdl/rest"
"math"
)
type multiPanelMessageData struct {
ChannelId uint64
Title string
Content string
Colour int
IsPremium bool
}
func multiPanelIntoMessageData(panel database.MultiPanel, isPremium bool) multiPanelMessageData {
return multiPanelMessageData{
ChannelId: panel.ChannelId,
Title: panel.Title,
Content: panel.Content,
Colour: panel.Colour,
IsPremium: isPremium,
}
}
func (d *multiPanelMessageData) send(ctx *botcontext.BotContext, panels []database.Panel) (uint64, error) {
e := embed.NewEmbed().
SetTitle(d.Title).
SetDescription(d.Content).
SetColor(d.Colour)
if !d.IsPremium {
// TODO: Don't harcode
e.SetFooter("Powered by ticketsbot.net", "https://ticketsbot.net/assets/img/logo.png")
}
buttons := make([]component.Component, len(panels))
for i, panel := range panels {
var buttonEmoji *emoji.Emoji
if panel.ReactionEmote != "" {
buttonEmoji = &emoji.Emoji{
Name: panel.ReactionEmote,
}
}
buttons[i] = component.BuildButton(component.Button{
Label: panel.Title,
CustomId: panel.CustomId,
Style: component.ButtonStyle(panel.ButtonStyle),
Emoji: buttonEmoji,
})
}
var rows []component.Component
for i := 0; i <= int(math.Ceil(float64(len(buttons)/5))); i++ {
lb := i * 5
ub := lb + 5
if ub >= len(buttons) {
ub = len(buttons)
}
if lb >= ub {
break
}
row := component.BuildActionRow(buttons[lb:ub]...)
rows = append(rows, row)
}
data := rest.CreateMessageData{
Embeds: []*embed.Embed{e},
Components: rows,
}
msg, err := rest.CreateMessage(ctx.Token, ctx.RateLimiter, d.ChannelId, data)
if err != nil {
return 0, err
}
return msg.Id, nil
}

View File

@ -0,0 +1,96 @@
package api
import (
"errors"
"github.com/TicketsBot/GoPanel/botcontext"
dbclient "github.com/TicketsBot/GoPanel/database"
"github.com/TicketsBot/GoPanel/rpc"
"github.com/TicketsBot/GoPanel/utils"
"github.com/TicketsBot/common/premium"
"github.com/gin-gonic/gin"
"github.com/rxdn/gdl/rest"
"github.com/rxdn/gdl/rest/request"
"strconv"
)
func MultiPanelResend(ctx *gin.Context) {
guildId := ctx.Keys["guildid"].(uint64)
// parse panel ID
panelId, err := strconv.Atoi(ctx.Param("panelid"))
if err != nil {
ctx.JSON(400, utils.ErrorJson(err))
return
}
// retrieve panel from DB
multiPanel, ok, err := dbclient.Client.MultiPanels.Get(panelId)
if err != nil {
ctx.JSON(500, utils.ErrorJson(err))
return
}
// check panel exists
if !ok {
ctx.JSON(404, utils.ErrorJson(errors.New("No panel with the provided ID found")))
return
}
// check panel is in the same guild
if guildId != multiPanel.GuildId {
ctx.JSON(403, utils.ErrorJson(errors.New("Guild ID doesn't match")))
return
}
// get bot context
botContext, err := botcontext.ContextForGuild(guildId)
if err != nil {
ctx.AbortWithStatusJSON(500, utils.ErrorJson(err))
return
}
// delete old message
if err := rest.DeleteMessage(botContext.Token, botContext.RateLimiter, multiPanel.ChannelId, multiPanel.MessageId); err != nil {
var unwrapped request.RestError
if errors.As(err, &unwrapped) && !unwrapped.IsClientError() {
ctx.JSON(500, utils.ErrorJson(err))
return
}
}
// get premium status
premiumTier, err := rpc.PremiumClient.GetTierByGuildId(guildId, true, botContext.Token, botContext.RateLimiter)
if err != nil {
ctx.JSON(500, utils.ErrorJson(err))
return
}
panels, err := dbclient.Client.MultiPanelTargets.GetPanels(multiPanel.Id)
if err != nil {
ctx.JSON(500, utils.ErrorJson(err))
return
}
// send new message
messageData := multiPanelIntoMessageData(multiPanel, premiumTier > premium.None)
messageId, err := messageData.send(&botContext, panels)
if 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 send messages in the provided channel")))
} else {
ctx.JSON(500, utils.ErrorJson(err))
}
return
}
if err = dbclient.Client.MultiPanels.UpdateMessageId(multiPanel.Id, messageId); err != nil {
ctx.JSON(500, utils.ErrorJson(err))
return
}
ctx.JSON(200, gin.H{
"success": true,
})
}

View File

@ -91,7 +91,8 @@ func MultiPanelUpdate(ctx *gin.Context) {
}
// send new message
messageId, err := data.sendEmbed(&botContext, premiumTier > premium.None, panels)
messageData := data.IntoMessageData(premiumTier > premium.None)
messageId, err := messageData.send(&botContext, panels)
if err != nil {
var unwrapped request.RestError
if errors.As(err, &unwrapped) && unwrapped.StatusCode == 403 {

View File

@ -13,10 +13,7 @@ import (
"github.com/TicketsBot/database"
"github.com/gin-gonic/gin"
"github.com/rxdn/gdl/objects/channel"
"github.com/rxdn/gdl/objects/channel/embed"
"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"
"regexp"
@ -43,6 +40,26 @@ type panelBody struct {
ButtonStyle component.ButtonStyle `json:"button_style,string"`
}
func (p *panelBody) IntoPanelMessageData(customId string, isPremium bool) panelMessageData {
var emoji *string
if p.Emote != "" {
emoji = &p.Emote
}
return panelMessageData{
ChannelId: p.ChannelId,
Title: p.Title,
Content: p.Content,
CustomId: customId,
Colour: int(p.Colour),
ImageUrl: p.ImageUrl,
ThumbnailUrl: p.ThumbnailUrl,
Emoji: emoji,
ButtonStyle: p.ButtonStyle,
IsPremium: isPremium,
}
}
func CreatePanel(ctx *gin.Context) {
guildId := ctx.Keys["guildid"].(uint64)
@ -99,7 +116,10 @@ func CreatePanel(ctx *gin.Context) {
customId := utils.RandString(80)
emoji, _ := data.getEmoji() // already validated
msgId, err := data.sendEmbed(&botContext, data.Title, customId, emoji, data.ImageUrl, data.ThumbnailUrl, data.ButtonStyle, premiumTier > premium.None)
data.Emote = emoji
messageData := data.IntoPanelMessageData(customId, premiumTier > premium.None)
msgId, err := messageData.send(&botContext)
if err != nil {
var unwrapped request.RestError
if errors.As(err, &unwrapped) && unwrapped.StatusCode == 403 {
@ -363,51 +383,3 @@ func (p *panelBody) verifyThumbnailUrl() bool {
func (p *panelBody) verifyButtonStyle() bool {
return p.ButtonStyle >= component.ButtonStylePrimary && p.ButtonStyle <= component.ButtonStyleDanger
}
func (p *panelBody) sendEmbed(ctx *botcontext.BotContext, title, customId, emote string, imageUrl, thumbnailUrl *string, buttonStyle component.ButtonStyle, isPremium bool) (uint64, error) {
e := embed.NewEmbed().
SetTitle(p.Title).
SetDescription(p.Content).
SetColor(int(p.Colour))
if imageUrl != nil {
e.SetImage(*imageUrl)
}
if thumbnailUrl != nil {
e.SetThumbnail(*thumbnailUrl)
}
if !isPremium {
// TODO: Don't harcode
e.SetFooter("Powered by ticketsbot.net", "https://ticketsbot.net/assets/img/logo.png")
}
var buttonEmoji *emoji.Emoji
if emote != "" {
buttonEmoji = &emoji.Emoji{
Name: emote,
}
}
data := rest.CreateMessageData{
Embeds: []*embed.Embed{e},
Components: []component.Component{
component.BuildActionRow(component.BuildButton(component.Button{
Label: title,
CustomId: customId,
Style: buttonStyle,
Emoji: buttonEmoji,
Url: nil,
Disabled: false,
})),
},
}
msg, err := rest.CreateMessage(ctx.Token, ctx.RateLimiter, p.ChannelId, data)
if err != nil {
return 0, err
}
return msg.Id, nil
}

View File

@ -0,0 +1,87 @@
package api
import (
"github.com/TicketsBot/GoPanel/botcontext"
"github.com/TicketsBot/database"
"github.com/rxdn/gdl/objects/channel/embed"
"github.com/rxdn/gdl/objects/guild/emoji"
"github.com/rxdn/gdl/objects/interaction/component"
"github.com/rxdn/gdl/rest"
)
type panelMessageData struct {
ChannelId uint64
Title, Content, CustomId string
Colour int
ImageUrl, ThumbnailUrl, Emoji *string
ButtonStyle component.ButtonStyle
IsPremium bool
}
func panelIntoMessageData(panel database.Panel, isPremium bool) panelMessageData {
var emoji *string
if panel.ReactionEmote != "" {
emoji = &panel.ReactionEmote
}
return panelMessageData{
ChannelId: panel.ChannelId,
Title: panel.Title,
Content: panel.Content,
CustomId: panel.CustomId,
Colour: int(panel.Colour),
ImageUrl: panel.ImageUrl,
ThumbnailUrl: panel.ThumbnailUrl,
Emoji: emoji,
ButtonStyle: component.ButtonStyle(panel.ButtonStyle),
IsPremium: isPremium,
}
}
func (p *panelMessageData) send(ctx *botcontext.BotContext) (uint64, error) {
e := embed.NewEmbed().
SetTitle(p.Title).
SetDescription(p.Content).
SetColor(p.Colour)
if p.ImageUrl != nil {
e.SetImage(*p.ImageUrl)
}
if p.ThumbnailUrl != nil {
e.SetThumbnail(*p.ThumbnailUrl)
}
if !p.IsPremium {
e.SetFooter("Powered by ticketsbot.net", "https://ticketsbot.net/assets/img/logo.png")
}
var buttonEmoji *emoji.Emoji
if p.Emoji != nil {
buttonEmoji = &emoji.Emoji{
Name: *p.Emoji,
}
}
data := rest.CreateMessageData{
Embeds: []*embed.Embed{e},
Components: []component.Component{
component.BuildActionRow(component.BuildButton(component.Button{
Label: p.Title,
CustomId: p.CustomId,
Style: p.ButtonStyle,
Emoji: buttonEmoji,
Url: nil,
Disabled: false,
})),
},
}
msg, err := rest.CreateMessage(ctx.Token, ctx.RateLimiter, p.ChannelId, data)
if err != nil {
return 0, err
}
return msg.Id, nil
}

View File

@ -0,0 +1,81 @@
package api
import (
"errors"
"github.com/TicketsBot/GoPanel/botcontext"
dbclient "github.com/TicketsBot/GoPanel/database"
"github.com/TicketsBot/GoPanel/rpc"
"github.com/TicketsBot/GoPanel/utils"
"github.com/TicketsBot/common/premium"
"github.com/gin-gonic/gin"
"github.com/rxdn/gdl/rest"
"github.com/rxdn/gdl/rest/request"
"strconv"
)
func ResendPanel(ctx *gin.Context) {
guildId := ctx.Keys["guildid"].(uint64)
botContext, err := botcontext.ContextForGuild(guildId)
if err != nil {
ctx.AbortWithStatusJSON(500, utils.ErrorJson(err))
return
}
panelId, err := strconv.Atoi(ctx.Param("panelid"))
if err != nil {
ctx.AbortWithStatusJSON(400, utils.ErrorJson(err))
return
}
// get existing
panel, err := dbclient.Client.Panel.GetById(panelId)
if err != nil {
ctx.AbortWithStatusJSON(500, utils.ErrorJson(err))
return
}
// check guild ID matches
if panel.GuildId != guildId {
ctx.AbortWithStatusJSON(400, gin.H{
"success": false,
"error": "Guild ID does not match",
})
return
}
// delete old message
if err := rest.DeleteMessage(botContext.Token, botContext.RateLimiter, panel.ChannelId, panel.GuildId); err != nil {
var unwrapped request.RestError
if errors.As(err, &unwrapped) && !unwrapped.IsClientError() {
ctx.JSON(500, utils.ErrorJson(err))
return
}
}
premiumTier, err := rpc.PremiumClient.GetTierByGuildId(guildId, true, botContext.Token, botContext.RateLimiter)
if err != nil {
ctx.JSON(500, utils.ErrorJson(err))
return
}
messageData := panelIntoMessageData(panel, premiumTier > premium.None)
msgId, err := messageData.send(&botContext)
if err != nil {
var unwrapped request.RestError
if errors.As(err, &unwrapped) && unwrapped.StatusCode == 403 {
ctx.JSON(500, utils.ErrorStr("I do not have permission to send messages in the provided channel"))
} else {
ctx.JSON(500, utils.ErrorJson(err))
}
return
}
if err = dbclient.Client.Panel.UpdateMessageId(panel.PanelId, msgId); err != nil {
ctx.AbortWithStatusJSON(500, utils.ErrorJson(err))
return
}
ctx.JSON(200, utils.SuccessResponse)
}

View File

@ -83,15 +83,15 @@ func UpdatePanel(ctx *gin.Context) {
panelIds[i] = panel.PanelId
}
data := multiPanelCreateData{
messageData := multiPanelMessageData{
Title: multiPanel.Title,
Content: multiPanel.Content,
Colour: int32(multiPanel.Colour),
Colour: multiPanel.Colour,
ChannelId: multiPanel.ChannelId,
Panels: panelIds,
IsPremium: premiumTier > premium.None,
}
messageId, err := data.sendEmbed(&botContext, premiumTier > premium.None, panels)
messageId, err := messageData.send(&botContext, panels)
if err != nil {
ctx.JSON(500, utils.ErrorJson(err))
return
@ -123,7 +123,8 @@ func UpdatePanel(ctx *gin.Context) {
// delete old message, ignoring error
_ = rest.DeleteMessage(botContext.Token, botContext.RateLimiter, existing.ChannelId, existing.MessageId)
newMessageId, err = data.sendEmbed(&botContext, data.Title, existing.CustomId, data.Emote, data.ImageUrl, data.ThumbnailUrl, data.ButtonStyle, premiumTier > premium.None)
messageData := data.IntoPanelMessageData(existing.CustomId, premiumTier > premium.None)
newMessageId, err = messageData.send(&botContext)
if err != nil {
var unwrapped request.RestError
if errors.As(err, &unwrapped) && unwrapped.StatusCode == 403 {

View File

@ -86,11 +86,13 @@ func StartServer() {
guildAuthApiAdmin.GET("/panels", api_panels.ListPanels)
guildAuthApiAdmin.POST("/panels", api_panels.CreatePanel)
guildAuthApiAdmin.POST("/panels/:panelid", rl(middleware.RateLimitTypeGuild, 5, 5*time.Second), api_panels.ResendPanel)
guildAuthApiAdmin.PATCH("/panels/:panelid", api_panels.UpdatePanel)
guildAuthApiAdmin.DELETE("/panels/:panelid", api_panels.DeletePanel)
guildAuthApiAdmin.GET("/multipanels", api_panels.MultiPanelList)
guildAuthApiAdmin.POST("/multipanels", api_panels.MultiPanelCreate)
guildAuthApiAdmin.POST("/multipanels/:panelid", rl(middleware.RateLimitTypeGuild, 5, 5*time.Second), api_panels.MultiPanelResend)
guildAuthApiAdmin.PATCH("/multipanels/:panelid", api_panels.MultiPanelUpdate)
guildAuthApiAdmin.DELETE("/multipanels/:panelid", api_panels.MultiPanelDelete)

View File

@ -1,5 +1,5 @@
{#if $notifyModal}
<div class="modal" transition:fade>
<div class="modal" transition:fade="{{duration: 500}}">
<div class="modal-wrapper" bind:this={wrapper}>
<Card footer="{true}" footerRight="{true}" fill="{false}">
<span slot="title">{$notifyTitle}</span>
@ -15,7 +15,7 @@
</div>
</div>
<div class="modal-backdrop" transition:fade>
<div class="modal-backdrop" transition:fade="{{duration: 500}}">
</div>
{/if}

View File

@ -22,6 +22,7 @@
<th>Channel</th>
<th>Panel Title</th>
<th class="category-col">Ticket Channel Category</th>
<th>Resend</th>
<th>Edit</th>
<th>Delete</th>
</tr>
@ -32,6 +33,9 @@
<td>#{channels.find((c) => c.id === panel.channel_id)?.name ?? 'Unknown Channel'}</td>
<td>{panel.title}</td>
<td class="category-col">{channels.find((c) => c.id === panel.category_id)?.name ?? 'Unknown Category'}</td>
<td>
<Button on:click={() => resendPanel(panel.panel_id)}>Resend</Button>
</td>
<td>
<Button on:click={() => openEditModal(panel.panel_id)}>Edit</Button>
</td>
@ -71,6 +75,7 @@
<thead>
<tr>
<th>Embed Title</th>
<th>Resend</th>
<th>Edit</th>
<th>Delete</th>
</tr>
@ -79,6 +84,9 @@
{#each multiPanels as panel}
<tr>
<td>{panel.title}</td>
<td>
<Button on:click={() => resendMultiPanel(panel.id)}>Resend</Button>
</td>
<td>
<Button on:click={() => openMultiEditModal(panel.id)}>Edit</Button>
</td>
@ -157,6 +165,16 @@
multiEditModal = true;
}
async function resendPanel(panelId) {
const res = await axios.post(`${API_URL}/api/${guildId}/panels/${panelId}`);
if (res.status !== 200) {
notifyError(res.data.error);
return;
}
notifySuccess("Panel resent successfully");
}
async function deletePanel(panelId) {
const res = await axios.delete(`${API_URL}/api/${guildId}/panels/${panelId}`);
if (res.status !== 200) {
@ -167,6 +185,16 @@
panels = panels.filter((p) => p.panel_id !== panelId);
}
async function resendMultiPanel(id) {
const res = await axios.post(`${API_URL}/api/${guildId}/multipanels/${id}`);
if (res.status !== 200) {
notifyError(res.data.error);
return;
}
notifySuccess("Multipanel resent successfully")
}
async function deleteMultiPanel(id) {
const res = await axios.delete(`${API_URL}/api/${guildId}/multipanels/${id}`);
if (res.status !== 200) {

8
go.mod
View File

@ -5,9 +5,9 @@ go 1.14
require (
github.com/BurntSushi/toml v0.3.1
github.com/TicketsBot/archiverclient v0.0.0-20210220155137-a562b2f1bbbb
github.com/TicketsBot/common v0.0.0-20210903095620-eb02b87cb4ca
github.com/TicketsBot/database v0.0.0-20210903140813-152a2383314c
github.com/TicketsBot/worker v0.0.0-20210904102739-4fb249194842
github.com/TicketsBot/common v0.0.0-20210910205523-7ce93fba6fa5
github.com/TicketsBot/database v0.0.0-20210906215136-2d0c54bd1109
github.com/TicketsBot/worker v0.0.0-20210910205947-89f7bd5ccf67
github.com/apex/log v1.1.2
github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff // indirect
github.com/getsentry/sentry-go v0.11.0
@ -23,7 +23,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-20210903095530-5a1c35525d2a
github.com/rxdn/gdl v0.0.0-20210906182609-337cb3c44a4c
github.com/sirupsen/logrus v1.5.0
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
)

View File

@ -31,3 +31,7 @@ func Base64Decode(s string) string {
func Base64Encode(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
}
func StrPtr(s string) *string {
return &s
}