Button styles

This commit is contained in:
rxdn 2021-07-03 21:02:49 +01:00
parent 099b49e140
commit 02ec5c8a90
8 changed files with 51 additions and 31 deletions

View File

@ -210,7 +210,7 @@ func (d *multiPanelCreateData) sendEmbed(ctx *botcontext.BotContext, isPremium b
buttons[i] = component.BuildButton(component.Button{
Label: panel.Title,
CustomId: panel.CustomId,
Style: component.ButtonStylePrimary,
Style: component.ButtonStyle(panel.ButtonStyle),
Emoji: emoji.Emoji{
Name: panel.ReactionEmote,
},

View File

@ -40,6 +40,7 @@ type panelBody struct {
Teams []database.SupportTeam `json:"teams"`
ImageUrl *string `json:"image_url,omitempty"`
ThumbnailUrl *string `json:"thumbnail_url,omitempty"`
ButtonStyle component.ButtonStyle `json:"button_style,string"`
}
func CreatePanel(ctx *gin.Context) {
@ -94,7 +95,7 @@ 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, premiumTier > premium.None)
msgId, err := data.sendEmbed(&botContext, data.Title, customId, emoji, data.ImageUrl, data.ThumbnailUrl, data.ButtonStyle, premiumTier > premium.None)
if err != nil {
var unwrapped request.RestError
if errors.As(err, &unwrapped) && unwrapped.StatusCode == 403 {
@ -128,6 +129,7 @@ func CreatePanel(ctx *gin.Context) {
CustomId: customId,
ImageUrl: data.ImageUrl,
ThumbnailUrl: data.ThumbnailUrl,
ButtonStyle: int(data.ButtonStyle),
}
panelId, err := dbclient.Client.Panel.Create(panel)
@ -269,6 +271,14 @@ func (p *panelBody) doValidations(ctx *gin.Context, guildId uint64) bool {
return false
}
if !p.verifyButtonStyle() {
ctx.AbortWithStatusJSON(400, gin.H{
"success": false,
"error": "Invalid button style",
})
return false
}
return true
}
@ -343,7 +353,11 @@ func (p *panelBody) verifyThumbnailUrl() bool {
return p.ThumbnailUrl == nil || (len(*p.ThumbnailUrl) <= 255 && urlRegex.MatchString(*p.ThumbnailUrl))
}
func (p *panelBody) sendEmbed(ctx *botcontext.BotContext, title, customId, emote string, imageUrl, thumbnailUrl *string, isPremium bool) (uint64, error) {
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).
@ -365,26 +379,16 @@ func (p *panelBody) sendEmbed(ctx *botcontext.BotContext, title, customId, emote
data := rest.CreateMessageData{
Embed: e,
Components: []component.Component{
{
Type: component.ComponentActionRow,
ComponentData: component.ActionRow{
Components: []component.Component{
{
Type: component.ComponentButton,
ComponentData: component.Button{
Label: title,
CustomId: customId,
Style: component.ButtonStylePrimary,
Emoji: emoji.Emoji{
Name: emote,
},
Url: nil,
Disabled: false,
},
},
},
component.BuildActionRow(component.BuildButton(component.Button{
Label: title,
CustomId: customId,
Style: buttonStyle,
Emoji: emoji.Emoji{
Name: emote,
},
},
Url: nil,
Disabled: false,
})),
},
}

View File

@ -9,6 +9,7 @@ import (
"github.com/TicketsBot/common/premium"
"github.com/TicketsBot/database"
"github.com/gin-gonic/gin"
"github.com/rxdn/gdl/objects/interaction/component"
"github.com/rxdn/gdl/rest"
"github.com/rxdn/gdl/rest/request"
"strconv"
@ -106,7 +107,8 @@ func UpdatePanel(ctx *gin.Context) {
existing.Title != data.Title ||
existing.ReactionEmote != data.Emote ||
existing.ImageUrl != data.ImageUrl ||
existing.ThumbnailUrl != data.ThumbnailUrl
existing.ThumbnailUrl != data.ThumbnailUrl ||
component.ButtonStyle(existing.ButtonStyle) != data.ButtonStyle
emoji, _ := data.getEmoji() // already validated
newMessageId := existing.MessageId
@ -115,7 +117,7 @@ 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, premiumTier > premium.None)
newMessageId, err = data.sendEmbed(&botContext, data.Title, existing.CustomId, data.Emote, data.ImageUrl, data.ThumbnailUrl, data.ButtonStyle, premiumTier > premium.None)
if err != nil {
var unwrapped request.RestError
if errors.As(err, &unwrapped) && unwrapped.StatusCode == 403 {
@ -146,6 +148,9 @@ func UpdatePanel(ctx *gin.Context) {
WelcomeMessage: data.WelcomeMessage,
WithDefaultTeam: data.WithDefaultTeam,
CustomId: existing.CustomId,
ImageUrl: data.ImageUrl,
ThumbnailUrl: data.ThumbnailUrl,
ButtonStyle: int(data.ButtonStyle),
}
if err = dbclient.Client.Panel.Update(panel); err != nil {

View File

@ -39,6 +39,7 @@ func GetTranscriptHandler(ctx *gin.Context) {
}
// Verify the user has permissions to be here
// ticket.UserId cannot be 0
if ticket.UserId != userId {
permLevel, err := utils.GetPermissionLevel(guildId, userId)
if err != nil {

View File

@ -31,6 +31,7 @@ func AuthenticateGuild(isApiMethod bool, requiredPermissionLevel permission.Perm
ctx.Keys["guildid"] = parsed
// TODO: Do we need this? Only really serves as a check whether the bot is in the server
guild, found := cache.Instance.GetGuild(parsed, false)
if !found {
if isApiMethod {
@ -42,8 +43,6 @@ func AuthenticateGuild(isApiMethod bool, requiredPermissionLevel permission.Perm
return
}
ctx.Keys["guild"] = guild
// Verify the user has permissions to be here
userId := ctx.Keys["userid"].(uint64)

View File

@ -54,6 +54,7 @@ func StartServer() {
guildAuthApiAdmin := apiGroup.Group("/:id", middleware.AuthenticateGuild(true, permission.Admin))
guildAuthApiSupport := apiGroup.Group("/:id", middleware.AuthenticateGuild(true, permission.Support))
guildAuthApiEveryone := apiGroup.Group("/:id", middleware.AuthenticateGuild(true, permission.Everyone))
{
guildAuthApiSupport.GET("/channels", api.ChannelsHandler)
guildAuthApiSupport.GET("/premium", api.PremiumHandler)
@ -79,7 +80,8 @@ func StartServer() {
guildAuthApiAdmin.DELETE("/multipanels/:panelid", api_panels.MultiPanelDelete)
guildAuthApiSupport.GET("/transcripts", createLimiter(5, 5 * time.Second), createLimiter(20, time.Minute), api_transcripts.ListTranscripts)
guildAuthApiSupport.GET("/transcripts/:ticketId", createLimiter(10, 10 * time.Second), api_transcripts.GetTranscriptHandler)
// Allow regular users to get their own transcripts, make sure you check perms inside
guildAuthApiEveryone.GET("/transcripts/:ticketId", createLimiter(10, 10 * time.Second), api_transcripts.GetTranscriptHandler)
guildAuthApiSupport.GET("/tickets", api_ticket.GetTickets)
guildAuthApiSupport.GET("/tickets/:ticketId", api_ticket.GetTicket)

View File

@ -14,6 +14,14 @@
<CategoryDropdown label="Ticket Category" col4=true {channels} bind:value={data.category_id}/>
<EmojiInput label="Button Emoji" col4=true bind:value={data.emote}/>
</div>
<div class="row">
<Dropdown col4=true label="Button Style" bind:value={data.button_style}>
<option value="1">Blue</option>
<option value="2">Grey</option>
<option value="3">Green</option>
<option value="4">Red</option>
</Dropdown>
</div>
<div class="row" style="justify-content: center">
<div class="col-3">
<Button icon="fas fa-sliders-h" fullWidth=true type="button"
@ -44,8 +52,8 @@
</div>
</div>
<div class="row">
<Input col2={true} label="Large Image URL" bind:value={data.image_url} />
<Input col2={true} label="Small Image URL" bind:value={data.thumbnail_url} />
<Input col2={true} label="Large Image URL" bind:value={data.image_url}/>
<Input col2={true} label="Small Image URL" bind:value={data.thumbnail_url}/>
</div>
</div>
</div>
@ -60,10 +68,10 @@
import {createEventDispatcher, onMount} from 'svelte';
import {colourToInt} from "../../js/util";
import {setDefaultHeaders} from "../../includes/Auth.svelte";
import CategoryDropdown from "../CategoryDropdown.svelte";
import EmojiInput from "../form/EmojiInput.svelte";
import Select from 'svelte-select';
import Dropdown from "../form/Dropdown.svelte";
export let guildId;
export let seedDefault = true;
@ -83,6 +91,7 @@
mentions: [],
default_team: true,
teams: [],
button_style: "1",
};
}

2
go.mod
View File

@ -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-20210604175952-03cfa14c16e1
github.com/TicketsBot/database v0.0.0-20210630165045-4745a3db99e1
github.com/TicketsBot/database v0.0.0-20210703200035-33b8cc0a309c
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