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
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea
*.iml
.env
.idea
*.iml
.env
go.work
go.work.sum
2 changes: 1 addition & 1 deletion bot/button/handlers/addadmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (h *AddAdminHandler) Execute(ctx *context.ButtonContext) {
auditReason = fmt.Sprintf("Added admin member/role by %s", member.User.Username)
}

panels, _ := dbclient.Client.Panel.GetByGuild(ctx, ctx.GuildId())
panels, _ := dbclient.Client.Panel.GetByGuildWithoutComponents(ctx, ctx.GuildId())
seen := make(map[uint64]struct{})
for _, p := range panels {
if p.TicketNotificationChannel == nil {
Expand Down
2 changes: 1 addition & 1 deletion bot/button/handlers/addsupport.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func updateChannelPermissions(ctx cmdregistry.CommandContext, id uint64, mention
return
}

panels, err := dbclient.Client.Panel.GetByGuild(ctx, ctx.GuildId())
panels, err := dbclient.Client.Panel.GetByGuildWithoutComponents(ctx, ctx.GuildId())
if err != nil {
ctx.HandleError(err)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/TicketsBot-cloud/worker/bot/command/context"
"github.com/TicketsBot-cloud/worker/bot/customisation"
"github.com/TicketsBot-cloud/worker/bot/dbclient"
"github.com/TicketsBot-cloud/worker/bot/logic"
"github.com/TicketsBot-cloud/worker/bot/utils"
)

Expand Down Expand Up @@ -152,7 +153,7 @@ func buildPanelSettings(ctx *context.ModalContext, worker *w.Context, selectedPa
var settings []string

// Panel header
settings = append(settings, fmt.Sprintf("**Panel: %s**", selectedPanel.Title))
settings = append(settings, fmt.Sprintf("**Panel: %s**", logic.PanelLabel(*selectedPanel)))

// Basic settings
settings = append(settings, fmt.Sprintf("**Message ID:** `%d`", selectedPanel.MessageId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/TicketsBot-cloud/worker/bot/command/context"
"github.com/TicketsBot-cloud/worker/bot/customisation"
"github.com/TicketsBot-cloud/worker/bot/dbclient"
"github.com/TicketsBot-cloud/worker/bot/logic"
"github.com/TicketsBot-cloud/worker/bot/permissionwrapper"
"github.com/TicketsBot-cloud/worker/bot/utils"
)
Expand Down Expand Up @@ -224,7 +225,7 @@ func processPermissionChecks(selectedValues []string, worker *w.Context, guildId

body, hasMissing := checkPanelPermissions(worker, guildId, botMember, *panel)
sections = append(sections, permissionSection{
title: fmt.Sprintf("Panel: %s", panel.Title),
title: fmt.Sprintf("Panel: %s", logic.PanelLabel(*panel)),
body: body,
hasMissing: hasMissing,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func checkUserTicketPermissions(ctx *context.ModalContext, worker *w.Context, gu

// Check support roles
supportRoles, err := dbclient.Client.RolePermissions.GetSupportRoles(ctx, guildId)
if err == nil && ticketPermLevel != "**Ticket Permission:** Admin" && ticketPermLevel != "**Ticket Permission:** Server Owner" && ticketPermLevel != "**Ticket Permission:** Admin" {
if err == nil && ticketPermLevel != "**Ticket Permission:** Admin" && ticketPermLevel != "**Ticket Permission:** Server Owner" {
var supportRoleNames []string
for _, roleId := range member.Roles {
if utils.Contains(supportRoles, roleId) {
Expand Down Expand Up @@ -282,9 +282,9 @@ func checkUserTicketPermissions(ctx *context.ModalContext, worker *w.Context, gu
}

if userInTeam {
teamPanelDetails = append(teamPanelDetails, fmt.Sprintf("`%s` (direct)", panel.Title))
teamPanelDetails = append(teamPanelDetails, fmt.Sprintf("`%s` (direct)", logic.PanelLabel(panel)))
} else if len(accessViaRoles) > 0 {
teamPanelDetails = append(teamPanelDetails, fmt.Sprintf("`%s` (via %s)", panel.Title, strings.Join(accessViaRoles, ", ")))
teamPanelDetails = append(teamPanelDetails, fmt.Sprintf("`%s` (via %s)", logic.PanelLabel(panel), strings.Join(accessViaRoles, ", ")))
}
}

Expand Down Expand Up @@ -342,7 +342,7 @@ func checkRoleTicketPermissions(ctx *context.ModalContext, worker *w.Context, gu
}

if utils.Contains(teamRoles, roleId) {
teamPanels = append(teamPanels, fmt.Sprintf("`%s`", panel.Title))
teamPanels = append(teamPanels, fmt.Sprintf("`%s`", logic.PanelLabel(panel)))
}
}

Expand Down
4 changes: 3 additions & 1 deletion bot/button/handlers/admindebug/server/panelsettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/TicketsBot-cloud/worker/bot/button/registry/matcher"
"github.com/TicketsBot-cloud/worker/bot/command/context"
"github.com/TicketsBot-cloud/worker/bot/dbclient"
"github.com/TicketsBot-cloud/worker/bot/logic"
"github.com/TicketsBot-cloud/worker/bot/utils"
)

type AdminDebugServerPanelSettingsHandler struct{}
Expand Down Expand Up @@ -54,7 +56,7 @@ func (h *AdminDebugServerPanelSettingsHandler) Execute(ctx *context.ButtonContex
}
panelDesc := fmt.Sprintf("View settings for panel #%d", i+1)
options = append(options, component.SelectOption{
Label: fmt.Sprintf("Panel: %s", panel.Title),
Label: utils.StringMax(fmt.Sprintf("Panel: %s", logic.PanelLabel(panel)), 100),
Value: fmt.Sprintf("panel_%d", panel.MessageId),
Description: &panelDesc,
})
Expand Down
4 changes: 3 additions & 1 deletion bot/button/handlers/admindebug/server/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/TicketsBot-cloud/worker/bot/button/registry/matcher"
"github.com/TicketsBot-cloud/worker/bot/command/context"
"github.com/TicketsBot-cloud/worker/bot/dbclient"
"github.com/TicketsBot-cloud/worker/bot/logic"
"github.com/TicketsBot-cloud/worker/bot/utils"
)

type AdminDebugServerPermissionsHandler struct{}
Expand Down Expand Up @@ -63,7 +65,7 @@ func (h *AdminDebugServerPermissionsHandler) Execute(ctx *context.ButtonContext)
}
panelDesc := fmt.Sprintf("Check permissions for panel #%d", i+1)
options = append(options, component.SelectOption{
Label: fmt.Sprintf("Panel: %s", panel.Title),
Label: utils.StringMax(fmt.Sprintf("Panel: %s", logic.PanelLabel(panel)), 100),
Value: fmt.Sprintf("panel_%d_%d", panel.MessageId, guildId),
Description: &panelDesc,
})
Expand Down
3 changes: 2 additions & 1 deletion bot/button/handlers/closewithreasonsubmit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"fmt"
"strings"
"unicode/utf8"

"github.com/TicketsBot-cloud/worker/bot/button/registry"
"github.com/TicketsBot-cloud/worker/bot/button/registry/matcher"
Expand Down Expand Up @@ -43,7 +44,7 @@ func (h *CloseWithReasonSubmitHandler) Execute(ctx *context.ModalContext) {
}

// This must be malicious
if len(reason) > 1024 {
if utf8.RuneCountInString(reason) > 1024 {
ctx.HandleError(fmt.Errorf("Reason is too long"))
return
}
Expand Down
3 changes: 2 additions & 1 deletion bot/button/handlers/editclosereasonsubmit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"regexp"
"strconv"
"strings"
"unicode/utf8"

"github.com/TicketsBot-cloud/common/permission"
"github.com/TicketsBot-cloud/database"
Expand Down Expand Up @@ -79,7 +80,7 @@ func (h *EditCloseReasonSubmitHandler) Execute(ctx *context.ModalContext) {
}
}

if len(reason) > 1024 {
if utf8.RuneCountInString(reason) > 1024 {
ctx.HandleError(fmt.Errorf("reason too long"))
return
}
Expand Down
2 changes: 1 addition & 1 deletion bot/button/handlers/exitsurveysubmit.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (h *ExitSurveySubmitHandler) Execute(cmd *cmdcontext.ModalContext) {
inputsByCustomId[input.CustomId] = input
}

formAnswers := parseModalComponents(cmd.Interaction.Data.Components, inputsByCustomId)
formAnswers := parseModalComponents(cmd.Interaction.Data, inputsByCustomId)

if invalidLabel, valid := validateFormAnswers(formAnswers); !valid {
cmd.Reply(customisation.Red, i18n.Error, i18n.MessageFormMissingInput, invalidLabel)
Expand Down
30 changes: 26 additions & 4 deletions bot/button/handlers/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package handlers

import (
"fmt"
"strconv"
"strings"

"github.com/TicketsBot-cloud/common/sentry"
"github.com/TicketsBot-cloud/database"
"github.com/TicketsBot-cloud/gdl/objects"
"github.com/TicketsBot-cloud/gdl/objects/interaction"
"github.com/TicketsBot-cloud/gdl/objects/interaction/component"
"github.com/TicketsBot-cloud/worker/bot/button/registry"
Expand Down Expand Up @@ -67,7 +69,7 @@ func (h *FormHandler) Execute(ctx *context.ModalContext) {
return
}

formAnswers := parseModalComponents(data.Components, inputs)
formAnswers := parseModalComponents(data, inputs)

if invalidLabel, valid := validateFormAnswers(formAnswers); !valid {
ctx.Reply(customisation.Red, i18n.Error, i18n.MessageFormMissingInput, invalidLabel)
Expand All @@ -82,9 +84,9 @@ func (h *FormHandler) Execute(ctx *context.ModalContext) {
}

// parseModalComponents extracts answers from modal action rows, keyed by the matching FormInput.
func parseModalComponents(actionRows []interaction.ModalSubmitInteractionActionRowData, inputsByCustomId map[string]database.FormInput) map[database.FormInput]string {
func parseModalComponents(data interaction.ModalSubmitInteractionData, inputsByCustomId map[string]database.FormInput) map[database.FormInput]string {
answers := make(map[database.FormInput]string)
for _, actionRow := range actionRows {
for _, actionRow := range data.Components {
if actionRow.Component != nil {
c := actionRow.Component
var answer string
Expand All @@ -98,7 +100,7 @@ func parseModalComponents(actionRows []interaction.ModalSubmitInteractionActionR
case component.ComponentRoleSelect:
answer = joinMentions(c.Values, "role")
case component.ComponentMentionableSelect:
answer = strings.Trim(strings.Join(c.Values, ", "), "<@&!>")
answer = joinMentionables(c.Values, data.Resolved)
case component.ComponentChannelSelect:
answer = joinMentions(c.Values, "channel")
case component.ComponentRadioGroup:
Expand Down Expand Up @@ -162,3 +164,23 @@ func joinMentions(mentions []string, mentionType string) string {

return val
}

func joinMentionables(ids []string, resolved interaction.ResolvedData) string {
mentions := make([]string, len(ids))
for i, id := range ids {
mentions[i] = id

snowflake, err := strconv.ParseUint(id, 10, 64)
if err != nil {
continue
}

if _, ok := resolved.Roles[objects.Snowflake(snowflake)]; ok {
mentions[i] = fmt.Sprintf("<@&%s>", id)
} else if _, ok := resolved.Users[objects.Snowflake(snowflake)]; ok {
mentions[i] = fmt.Sprintf("<@%s>", id)
}
}

return strings.Join(mentions, ", ")
}
30 changes: 30 additions & 0 deletions bot/button/handlers/form_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package handlers

import (
"encoding/json"
"testing"

"github.com/TicketsBot-cloud/database"
"github.com/TicketsBot-cloud/gdl/objects/interaction"
"github.com/TicketsBot-cloud/gdl/objects/interaction/component"
"github.com/stretchr/testify/require"
)

func TestParseModalComponentsMentionableSelect(t *testing.T) {
raw := `{
"custom_id": "form_panel",
"components": [{"type": 18, "id": 1, "component": {"type": 7, "id": 2, "custom_id": "who", "values": ["111", "222", "333"]}}],
"resolved": {
"users": {"111": {"id": "111", "username": "user"}},
"members": {"111": {"roles": []}},
"roles": {"222": {"id": "222", "name": "role"}}
}
}`

var data interaction.ModalSubmitInteractionData
require.NoError(t, json.Unmarshal([]byte(raw), &data))

input := database.FormInput{Id: 1, CustomId: "who", Type: int(component.ComponentMentionableSelect)}
answers := parseModalComponents(data, map[string]database.FormInput{"who": input})
require.Equal(t, "<@111>, <@&222>, 333", answers[input])
}
3 changes: 2 additions & 1 deletion bot/button/handlers/tickets/edit/labelchangesubmit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
commandcontext "github.com/TicketsBot-cloud/worker/bot/command/context"
"github.com/TicketsBot-cloud/worker/bot/customisation"
"github.com/TicketsBot-cloud/worker/bot/dbclient"
"github.com/TicketsBot-cloud/worker/bot/logic"
"github.com/TicketsBot-cloud/worker/i18n"
)

Expand Down Expand Up @@ -81,7 +82,7 @@ func (h *LabelChangeSubmitHandler) Execute(ctx *commandcontext.ModalContext) {
return
}
if panel.PanelId != 0 {
topicMsg = fmt.Sprintf("%s | ", panel.Title)
topicMsg = fmt.Sprintf("%s | ", logic.PanelLabel(panel))
}
}

Expand Down
7 changes: 5 additions & 2 deletions bot/button/handlers/viewsurvey.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ func (h *ViewSurveyHandler) Execute(ctx *context.ButtonContext) {
SetAuthor(opener.Username, "", opener.AvatarUrl(256)).
SetColorPtr(ctx.GetColour(customisation.Green))

for _, answer := range surveyResponse.Responses {
fields := make([]embed.EmbedField, len(surveyResponse.Responses))
for i, answer := range surveyResponse.Responses {
var title string
if answer.Question == nil || *answer.Question == "" {
title = "Unknown Question"
Expand All @@ -115,9 +116,11 @@ func (h *ViewSurveyHandler) Execute(ctx *context.ButtonContext) {
response = "No response"
}

e.AddField(title, response, false)
fields[i] = embed.EmbedField{Name: title, Value: response}
}

logic.AddFormAnswerFields(e, fields)

var buttons []component.Component
buttons = append(buttons, logic.TranscriptLinkElement(ticket.HasTranscript)(ctx.Worker(), ticket)...)
buttons = append(buttons, logic.ThreadLinkElement(ticket.ChannelId != nil && ticket.IsThread)(ctx.Worker(), ticket)...)
Expand Down
8 changes: 8 additions & 0 deletions bot/command/context/mentionabletype.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ const (
MentionableTypeRole
)

func (m MentionableType) String() string {
if m == MentionableTypeRole {
return "role"
}

return "user"
}

func (m MentionableType) OverwriteType() channel.PermissionOverwriteType {
switch m {
case MentionableTypeUser:
Expand Down
2 changes: 1 addition & 1 deletion bot/command/impl/settings/removeadmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (c RemoveAdminCommand) Execute(ctx registry.CommandContext, id uint64) {
auditReason = fmt.Sprintf("Removed admin member/role by %s", member.User.Username)
}

panels, _ := dbclient.Client.Panel.GetByGuild(ctx, ctx.GuildId())
panels, _ := dbclient.Client.Panel.GetByGuildWithoutComponents(ctx, ctx.GuildId())
seen := make(map[uint64]struct{})
for _, p := range panels {
if p.TicketNotificationChannel == nil {
Expand Down
2 changes: 1 addition & 1 deletion bot/command/impl/settings/removesupport.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (c RemoveSupportCommand) Execute(ctx registry.CommandContext, id uint64) {
auditReason = fmt.Sprintf("Removed support member/role by %s", member.User.Username)
}

panels, _ := dbclient.Client.Panel.GetByGuild(ctx, ctx.GuildId())
panels, _ := dbclient.Client.Panel.GetByGuildWithoutComponents(ctx, ctx.GuildId())
seen := make(map[uint64]struct{})
for _, p := range panels {
if p.TicketNotificationChannel == nil {
Expand Down
3 changes: 2 additions & 1 deletion bot/command/impl/tickets/closerequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"
"time"
"unicode/utf8"

"github.com/TicketsBot-cloud/common/model"
"github.com/TicketsBot-cloud/common/permission"
Expand Down Expand Up @@ -60,7 +61,7 @@ func (CloseRequestCommand) Execute(ctx registry.CommandContext, closeDelay *int,
return
}

if reason != nil && len(*reason) > 255 {
if reason != nil && utf8.RuneCountInString(*reason) > 255 {
ctx.Reply(customisation.Red, i18n.Error, i18n.MessageCloseReasonTooLong)
return
}
Expand Down
2 changes: 1 addition & 1 deletion bot/command/impl/tickets/oncall.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c OnCallCommand) GetExecutor() interface{} {
}

func (OnCallCommand) Execute(ctx registry.CommandContext) {
panels, err := dbclient.Client.Panel.GetByGuild(ctx, ctx.GuildId())
panels, err := dbclient.Client.Panel.GetByGuildWithoutComponents(ctx, ctx.GuildId())
if err != nil {
ctx.HandleError(err)
return
Expand Down
Loading
Loading