Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion bot/button/handlers/multipanel.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,22 @@ func (h *MultiPanelHandler) Execute(ctx *context.SelectMenuContext) {
return
}

inputApiConfigs, err := dbclient.Client.FormInputApiConfig.GetByFormId(ctx, form.Id)
if err != nil {
ctx.HandleError(err)
return
}

inputApiHeaders, err := dbclient.Client.FormInputApiHeaders.GetAllByGuild(ctx, ctx.GuildId())
if err != nil {
ctx.HandleError(err)
return
}

if len(inputs) == 0 { // Don't open a blank form
_, _ = logic.OpenTicket(ctx.Context, ctx, &panel, panel.Title, nil)
} else {
modal := buildForm(panel, form, inputs, inputOptions)
modal := buildForm(ctx.UserId(), panel, form, inputs, inputOptions, inputApiConfigs, inputApiHeaders)
ctx.Modal(modal)
}
}
Expand Down
96 changes: 68 additions & 28 deletions bot/button/handlers/panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"

ct "context"

"github.com/TicketsBot-cloud/common/sentry"
"github.com/TicketsBot-cloud/database"
"github.com/TicketsBot-cloud/gdl/objects/interaction"
Expand All @@ -15,6 +17,7 @@ import (
"github.com/TicketsBot-cloud/worker/bot/constants"
"github.com/TicketsBot-cloud/worker/bot/customisation"
"github.com/TicketsBot-cloud/worker/bot/dbclient"
"github.com/TicketsBot-cloud/worker/bot/integrations"
"github.com/TicketsBot-cloud/worker/bot/logic"
"github.com/TicketsBot-cloud/worker/bot/utils"
"github.com/TicketsBot-cloud/worker/i18n"
Expand Down Expand Up @@ -46,26 +49,6 @@ func (h *PanelHandler) Execute(ctx *context.ButtonContext) {
return
}

// Check support hours
hasSupportHours, err := dbclient.Client.PanelSupportHours.HasSupportHours(ctx, panel.PanelId)
if err != nil {
ctx.HandleError(err)
return
}

if hasSupportHours {
isActive, err := dbclient.Client.PanelSupportHours.IsActiveNow(ctx, panel.PanelId)
if err != nil {
ctx.HandleError(err)
return
}

if !isActive {
ctx.Reply(customisation.Red, i18n.Error, i18n.MessageOutsideSupportHours)
return
}
}

// blacklist check
blacklisted, err := ctx.IsBlacklisted(ctx)
if err != nil {
Expand Down Expand Up @@ -104,10 +87,22 @@ func (h *PanelHandler) Execute(ctx *context.ButtonContext) {
return
}

inputApiConfigs, err := dbclient.Client.FormInputApiConfig.GetByFormId(ctx, form.Id)
if err != nil {
ctx.HandleError(err)
return
}

inputApiHeaders, err := dbclient.Client.FormInputApiHeaders.GetAllByGuild(ctx, ctx.GuildId())
if err != nil {
ctx.HandleError(err)
return
}

if len(inputs) == 0 { // Don't open a blank form
_, _ = logic.OpenTicket(ctx.Context, ctx, &panel, panel.Title, nil)
} else {
modal := buildForm(panel, form, inputs, inputOptions)
modal := buildForm(ctx.UserId(), panel, form, inputs, inputOptions, inputApiConfigs, inputApiHeaders)
ctx.Modal(modal)
}
}
Expand All @@ -116,7 +111,7 @@ func (h *PanelHandler) Execute(ctx *context.ButtonContext) {
}
}

func buildForm(panel database.Panel, form database.Form, inputs []database.FormInput, inputOptions map[int][]database.FormInputOption) button.ResponseModal {
func buildForm(userId uint64, panel database.Panel, form database.Form, inputs []database.FormInput, inputOptions map[int][]database.FormInputOption, inputApiConfigs []database.FormInputApiConfig, inputApiHeaders map[int][]database.FormInputApiHeader) button.ResponseModal {
components := make([]component.Component, len(inputs))
for i, input := range inputs {
var minLength, maxLength *int
Expand All @@ -132,6 +127,21 @@ func buildForm(panel database.Panel, form database.Form, inputs []database.FormI
}

var innerComponent component.Component
var apiConfig *database.FormInputApiConfig
var apiHeaders []database.FormInputApiHeader

for _, conf := range inputApiConfigs {
if conf.FormInputId == input.Id {
apiConfig = &conf
break
}
}

if apiConfig != nil {
if headers, ok := inputApiHeaders[apiConfig.Id]; ok {
apiHeaders = headers
}
}

options, ok := inputOptions[input.Id]
if !ok {
Expand All @@ -141,14 +151,43 @@ func buildForm(panel database.Panel, form database.Form, inputs []database.FormI
switch input.Type {
// String Select
case int(component.ComponentSelectMenu):
opts := make([]component.SelectOption, len(options))
for j, option := range options {
opts[j] = component.SelectOption{
Label: option.Label,
Value: option.Value,
Description: option.Description,
ctx := ct.Background()

opts := make([]component.SelectOption, 0)

if apiConfig != nil {
apiOpts, err := integrations.FetchInputOptions(ctx, userId, *apiConfig, apiHeaders)
if err != nil {
fmt.Println(err)
}

opts = make([]component.SelectOption, len(apiOpts))

for j, option := range apiOpts {
if j >= 25 { // Discord max
break
}
opts[j] = component.SelectOption{
Label: option.Label,
Value: option.Value,
Description: option.Description,
}
}
} else {
opts = make([]component.SelectOption, len(options))
for j, option := range options {
opts[j] = component.SelectOption{
Label: option.Label,
Value: option.Value,
Description: option.Description,
}
}
}

if maxLength != nil && *maxLength > len(opts) {
*maxLength = len(opts)
}

isRequired := input.MinLength != nil && *input.MinLength > 0
innerComponent = component.BuildSelectMenu(component.SelectMenu{
CustomId: input.CustomId,
Expand All @@ -157,6 +196,7 @@ func buildForm(panel database.Panel, form database.Form, inputs []database.FormI
MaxValues: maxLength,
Required: utils.Ptr(isRequired),
})

// Input Text
case 4:
innerComponent = component.BuildInputText(component.InputText{
Expand Down
6 changes: 5 additions & 1 deletion bot/button/responsemodal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package button

import (
"errors"
"fmt"

"github.com/TicketsBot-cloud/common/sentry"
"github.com/TicketsBot-cloud/gdl/objects/interaction"
"github.com/TicketsBot-cloud/worker"
)
Expand All @@ -20,5 +22,7 @@ func (r ResponseModal) Build() interface{} {
}

func (r ResponseModal) HandleDeferred(interactionData interaction.InteractionMetadata, worker *worker.Context) error {
return errors.New("cannot defer modal response")
err := errors.New("cannot defer modal response")
sentry.Error(fmt.Errorf("failed to send deferred modal response with custom_id %s: %w", r.Data.CustomId, err))
return err
}
9 changes: 9 additions & 0 deletions bot/command/context/applicationcommandcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"

"github.com/TicketsBot-cloud/common/experiments"
permcache "github.com/TicketsBot-cloud/common/permission"
"github.com/TicketsBot-cloud/common/premium"
"github.com/TicketsBot-cloud/common/sentry"
Expand Down Expand Up @@ -176,6 +177,14 @@ func (c *SlashCommandContext) IsBlacklisted(ctx context.Context) (bool, error) {
return utils.IsBlacklisted(ctx, c.GuildId(), c.UserId(), utils.ValueOrZero(c.Interaction.Member), permLevel)
}

func (c *SlashCommandContext) HasFeature(feature experiments.Experiment) bool {
manager := experiments.GetGlobalManager()
if manager == nil {
return false
}
return manager.HasFeature(c, c.GuildId(), feature)
}

/// InteractionContext functions

func (c *SlashCommandContext) InteractionMetadata() interaction.InteractionMetadata {
Expand Down
9 changes: 9 additions & 0 deletions bot/command/context/autoclosecontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package context
import (
"context"

"github.com/TicketsBot-cloud/common/experiments"
permcache "github.com/TicketsBot-cloud/common/permission"
"github.com/TicketsBot-cloud/common/premium"
"github.com/TicketsBot-cloud/gdl/objects/channel"
Expand Down Expand Up @@ -133,3 +134,11 @@ func (c *AutoCloseContext) IsBlacklisted(ctx context.Context) (bool, error) {
// if the command is not executed in a guild
return utils.IsBlacklisted(ctx, c.GuildId(), c.UserId(), member, permLevel)
}

func (c *AutoCloseContext) HasFeature(feature experiments.Experiment) bool {
manager := experiments.GetGlobalManager()
if manager == nil {
return false
}
return manager.HasFeature(c, c.GuildId(), feature)
}
9 changes: 9 additions & 0 deletions bot/command/context/buttoncontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"

"github.com/TicketsBot-cloud/common/experiments"
permcache "github.com/TicketsBot-cloud/common/permission"
"github.com/TicketsBot-cloud/common/premium"
"github.com/TicketsBot-cloud/common/sentry"
Expand Down Expand Up @@ -202,6 +203,14 @@ func (c *ButtonContext) IsBlacklisted(ctx context.Context) (bool, error) {
return utils.IsBlacklisted(ctx, c.GuildId(), c.UserId(), utils.ValueOrZero(c.Interaction.Member), permLevel)
}

func (c *ButtonContext) HasFeature(feature experiments.Experiment) bool {
manager := experiments.GetGlobalManager()
if manager == nil {
return false
}
return manager.HasFeature(c, c.GuildId(), feature)
}

/// InteractionContext functions

func (c *ButtonContext) InteractionMetadata() interaction.InteractionMetadata {
Expand Down
9 changes: 9 additions & 0 deletions bot/command/context/dashboardcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"

"github.com/TicketsBot-cloud/common/experiments"
permcache "github.com/TicketsBot-cloud/common/permission"
"github.com/TicketsBot-cloud/common/premium"
"github.com/TicketsBot-cloud/common/sentry"
Expand Down Expand Up @@ -183,3 +184,11 @@ func (c *DashboardContext) IsBlacklisted(ctx context.Context) (bool, error) {
// if the command is not executed in a guild
return utils.IsBlacklisted(ctx, c.GuildId(), c.UserId(), member, permLevel)
}

func (c *DashboardContext) HasFeature(feature experiments.Experiment) bool {
manager := experiments.GetGlobalManager()
if manager == nil {
return false
}
return manager.HasFeature(c, c.GuildId(), feature)
}
17 changes: 17 additions & 0 deletions bot/command/context/messagecomponentextensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package context

import (
"context"
"encoding/json"
"fmt"

"github.com/TicketsBot-cloud/common/sentry"
"github.com/TicketsBot-cloud/gdl/objects/interaction"
Expand All @@ -13,6 +15,7 @@ import (
"github.com/TicketsBot-cloud/worker/bot/customisation"
"github.com/TicketsBot-cloud/worker/bot/utils"
"github.com/TicketsBot-cloud/worker/i18n"
"github.com/sirupsen/logrus"
"go.uber.org/atomic"
)

Expand All @@ -38,6 +41,20 @@ func NewMessageComponentExtensions(
}

func (e *MessageComponentExtensions) Modal(res button.ResponseModal) {
if res.Data.CustomId == "" {
sentry.ErrorWithContext(fmt.Errorf("modal has empty custom_id"), e.ctx.ToErrorContext())
}
if res.Data.Title == "" {
sentry.ErrorWithContext(fmt.Errorf("modal has empty title"), e.ctx.ToErrorContext())
}
if len(res.Data.Components) == 0 {
sentry.ErrorWithContext(fmt.Errorf("modal has no components"), e.ctx.ToErrorContext())
}

modalJSON, _ := json.Marshal(res.Build())
logrus.Infof("sending modal - custom_id: %s, title: %s, components: %d, json: %s",
res.Data.CustomId, res.Data.Title, len(res.Data.Components), string(modalJSON))

e.hasReplied.Store(true)
e.responseChannel <- res
}
Expand Down
9 changes: 9 additions & 0 deletions bot/command/context/modalcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"time"

"github.com/TicketsBot-cloud/common/experiments"
permcache "github.com/TicketsBot-cloud/common/permission"
"github.com/TicketsBot-cloud/common/premium"
"github.com/TicketsBot-cloud/common/sentry"
Expand Down Expand Up @@ -226,6 +227,14 @@ func (c *ModalContext) IsBlacklisted(ctx context.Context) (bool, error) {
return utils.IsBlacklisted(ctx, c.GuildId(), c.UserId(), utils.ValueOrZero(c.Interaction.Member), permLevel)
}

func (c *ModalContext) HasFeature(feature experiments.Experiment) bool {
manager := experiments.GetGlobalManager()
if manager == nil {
return false
}
return manager.HasFeature(c, c.GuildId(), feature)
}

/// InteractionContext functions

func (c *ModalContext) InteractionMetadata() interaction.InteractionMetadata {
Expand Down
9 changes: 9 additions & 0 deletions bot/command/context/panelcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"

"github.com/TicketsBot-cloud/common/experiments"
permcache "github.com/TicketsBot-cloud/common/permission"
"github.com/TicketsBot-cloud/common/premium"
"github.com/TicketsBot-cloud/common/sentry"
Expand Down Expand Up @@ -189,3 +190,11 @@ func (c *PanelContext) IsBlacklisted(ctx context.Context) (bool, error) {
// if the command is not executed in a guild
return utils.IsBlacklisted(ctx, c.GuildId(), c.UserId(), member, permLevel)
}

func (c *PanelContext) HasFeature(feature experiments.Experiment) bool {
manager := experiments.GetGlobalManager()
if manager == nil {
return false
}
return manager.HasFeature(c, c.GuildId(), feature)
}
9 changes: 9 additions & 0 deletions bot/command/context/selectmenucontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"

"github.com/TicketsBot-cloud/common/experiments"
permcache "github.com/TicketsBot-cloud/common/permission"
"github.com/TicketsBot-cloud/common/premium"
"github.com/TicketsBot-cloud/common/sentry"
Expand Down Expand Up @@ -202,6 +203,14 @@ func (c *SelectMenuContext) IsBlacklisted(ctx context.Context) (bool, error) {
return utils.IsBlacklisted(ctx, c.GuildId(), c.UserId(), utils.ValueOrZero(c.Interaction.Member), permLevel)
}

func (c *SelectMenuContext) HasFeature(feature experiments.Experiment) bool {
manager := experiments.GetGlobalManager()
if manager == nil {
return false
}
return manager.HasFeature(c, c.GuildId(), feature)
}

/// InteractionContext functions

func (c *SelectMenuContext) InteractionMetadata() interaction.InteractionMetadata {
Expand Down
Loading