Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cc: add functions and triggers for components and modals #1619

Merged
merged 31 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
87daa8c
cc: add functions and triggers for components and modals
SoggySaussages Mar 6, 2024
9c3b085
cc: restructure and rename functions
SoggySaussages Mar 7, 2024
12c7dea
cc: restructure custom id validation
SoggySaussages Mar 7, 2024
d6e975d
cc: add buttons and menus to complexMessage<Edit>
SoggySaussages Mar 7, 2024
296e06d
cc: allow duplicate keys in complexMessage<Edit>
SoggySaussages Mar 7, 2024
013ea4f
cc: hotfix
SoggySaussages Mar 7, 2024
ddc257f
cc: upstream message unmarshal function
SoggySaussages Mar 8, 2024
7e8ef72
cc: sendMessageType delegated to own type
SoggySaussages Mar 8, 2024
a4590d7
cc/web: gramatical consistency
SoggySaussages Mar 8, 2024
90f93b3
cc: idiomatify templates prefix check
SoggySaussages Mar 8, 2024
eb45410
cc: add limits to functions and triggers
SoggySaussages Mar 8, 2024
18a2a23
cc: allow sendModal to accept an sdict
SoggySaussages Mar 8, 2024
ff18478
cc/buttons: add more style compatibility
SoggySaussages Mar 8, 2024
aefb9a3
cc/interactions: allow file in response + followup
SoggySaussages Mar 8, 2024
37a5554
cc/interactions: improve cid parsing
SoggySaussages Mar 9, 2024
d18ee34
cc/interactions: restructure dot context
SoggySaussages Mar 9, 2024
9d4df98
cc/interactions: add getResponse
SoggySaussages Mar 14, 2024
7694b5f
cc/interactions: fix empty message content edits
SoggySaussages Mar 14, 2024
5e0cf44
dgo: use WebhookMessage
SoggySaussages Mar 15, 2024
54deb65
Revert "dgo: use WebhookMessage"
SoggySaussages Mar 16, 2024
daec60c
dgo: support string in EndpointWebhookMessage
SoggySaussages Mar 16, 2024
091e212
cc/interaction: fix modal cid parse
SoggySaussages Mar 31, 2024
e2d15d7
cc:clarify ccid triggers on dashboard
SoggySaussages Mar 31, 2024
1734c65
cc/interactions: move functions to separate file
SoggySaussages Jun 6, 2024
8108fc5
cc/interactions: update js in accordance with 1646
SoggySaussages Jun 6, 2024
7d2e661
cc/interactions: frontend fix
SoggySaussages Jun 6, 2024
5ac51da
cc/interactions: adjust js naming
SoggySaussages Jun 9, 2024
db4d0fb
cc/interactions: pass interaction context w execCC
SoggySaussages Jun 12, 2024
98b401b
cc/interactions: fix non-text menus' context data
SoggySaussages Jun 13, 2024
b4d21c2
cc/interactions: fix text menus' context data
SoggySaussages Jun 13, 2024
98062f9
cc/interactions: allow components in DM
SoggySaussages Jun 14, 2024
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
96 changes: 77 additions & 19 deletions common/templates/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ var (
"sdict": StringKeyDictionary,
"structToSdict": StructToSdict,
"cembed": CreateEmbed,
"cbutton": CreateButton,
"cmenu": CreateSelectMenu,
"cmodal": CreateModal,
"cslice": CreateSlice,
"complexMessage": CreateMessageSend,
"complexMessageEdit": CreateMessageEdit,
Expand Down Expand Up @@ -140,6 +143,7 @@ func RegisterSetupFunc(f ContextSetupFunc) {

func init() {
RegisterSetupFunc(baseContextFuncs)
RegisterSetupFunc(interactionContextFuncs)

msgpack.RegisterExt(1, (*SDict)(nil))
msgpack.RegisterExt(2, (*Dict)(nil))
Expand Down Expand Up @@ -193,8 +197,9 @@ type ContextFrame struct {
MentionHere bool
MentionRoles []int64

DelResponse bool
PublishResponse bool
DelResponse bool
PublishResponse bool
EphemeralResponse bool

DelResponseDelay int
EmbedsToSend []*discordgo.MessageEmbed
Expand All @@ -203,6 +208,13 @@ type ContextFrame struct {
isNestedTemplate bool
parsedTemplate *template.Template
SendResponseInDM bool

Interaction *CustomCommandInteraction
}

type CustomCommandInteraction struct {
*discordgo.Interaction
RespondedTo bool
}

func NewContext(gs *dstate.GuildSet, cs *dstate.ChannelState, ms *dstate.MemberState) *Context {
Expand Down Expand Up @@ -438,21 +450,18 @@ func (c *Context) MessageSend(content string) *discordgo.MessageSend {
}

// SendResponse sends the response and handles reactions and the like
func (c *Context) SendResponse(content string) (*discordgo.Message, error) {
func (c *Context) SendResponse(content string) (m *discordgo.Message, err error) {
channelID := int64(0)

if !c.CurrentFrame.SendResponseInDM {
if c.CurrentFrame.CS == nil {
return nil, nil
}

if hasPerms, _ := bot.BotHasPermissionGS(c.GS, c.CurrentFrame.CS.ID, discordgo.PermissionSendMessages); !hasPerms {
// don't bother sending the response if we dont have perms
return nil, nil
sendType := sendMessageGuildChannel
if c.CurrentFrame.Interaction != nil {
if c.CurrentFrame.Interaction.RespondedTo {
sendType = sendMessageInteractionFollowup
} else {
sendType = sendMessageInteractionResponse
}

channelID = c.CurrentFrame.CS.ID
} else {
} else if c.CurrentFrame.SendResponseInDM || (c.CurrentFrame.CS != nil && c.CurrentFrame.CS.IsPrivate()) {
sendType = sendMessageDM
if c.CurrentFrame.CS != nil && c.CurrentFrame.CS.Type == discordgo.ChannelTypeDM {
channelID = c.CurrentFrame.CS.ID
} else {
Expand All @@ -462,9 +471,19 @@ func (c *Context) SendResponse(content string) (*discordgo.Message, error) {
}
channelID = privChannel.ID
}
} else {
if c.CurrentFrame.CS == nil {
return nil, nil
}

if hasPerms, _ := bot.BotHasPermissionGS(c.GS, c.CurrentFrame.CS.ID, discordgo.PermissionSendMessages); !hasPerms {
// don't bother sending the response if we dont have perms
return nil, nil
}

channelID = c.CurrentFrame.CS.ID
}

isDM := c.CurrentFrame.SendResponseInDM || (c.CurrentFrame.CS != nil && c.CurrentFrame.CS.IsPrivate())
msgSend := c.MessageSend("")
var embeds []*discordgo.MessageEmbed
embeds = append(embeds, c.CurrentFrame.EmbedsToSend...)
Expand All @@ -474,7 +493,7 @@ func (c *Context) SendResponse(content string) (*discordgo.Message, error) {
// no point in sending the response if it gets deleted immedietely
return nil, nil
}
if isDM {
if sendType == sendMessageDM {
msgSend.Components = []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
Expand All @@ -488,10 +507,40 @@ func (c *Context) SendResponse(content string) (*discordgo.Message, error) {
},
}
}
m, err := common.BotSession.ChannelMessageSendComplex(channelID, msgSend)
if c.CurrentFrame.EphemeralResponse {
msgSend.Flags |= discordgo.MessageFlagsEphemeral
}
var getErr error
switch sendType {
case sendMessageInteractionResponse:
err = common.BotSession.CreateInteractionResponse(c.CurrentFrame.Interaction.ID, c.CurrentFrame.Interaction.Token, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: msgSend.Content,
Embeds: msgSend.Embeds,
AllowedMentions: &msgSend.AllowedMentions,
Flags: msgSend.Flags,
},
})
if err == nil {
c.CurrentFrame.Interaction.RespondedTo = true
m, getErr = common.BotSession.GetOriginalInteractionResponse(common.BotApplication.ID, c.CurrentFrame.Interaction.Token)
}
case sendMessageInteractionFollowup:
m, err = common.BotSession.CreateFollowupMessage(common.BotApplication.ID, c.CurrentFrame.Interaction.Token, &discordgo.WebhookParams{
Content: msgSend.Content,
Embeds: msgSend.Embeds,
AllowedMentions: &msgSend.AllowedMentions,
Flags: int64(msgSend.Flags),
})
default:
m, err = common.BotSession.ChannelMessageSendComplex(channelID, msgSend)
}
if err != nil {
logger.WithError(err).Error("Failed sending message")
} else {
} else if getErr != nil {
logger.WithError(getErr).Error("Failed getting interaction response")
} else if !c.CurrentFrame.EphemeralResponse {
if c.CurrentFrame.DelResponse {
MaybeScheduledDeleteMessage(c.GS.ID, channelID, m.ID, c.CurrentFrame.DelResponseDelay)
}
Expand All @@ -509,9 +558,18 @@ func (c *Context) SendResponse(content string) (*discordgo.Message, error) {
}
}

return m, nil
return
}

type sendMessageType uint

const (
sendMessageGuildChannel sendMessageType = 0
sendMessageDM sendMessageType = 1
sendMessageInteractionResponse sendMessageType = 2
sendMessageInteractionFollowup sendMessageType = 3
)

// IncreaseCheckCallCounter Returns true if key is above the limit
func (c *Context) IncreaseCheckCallCounter(key string, limit int) bool {
current, ok := c.Counters[key]
Expand Down
9 changes: 7 additions & 2 deletions common/templates/context_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,15 @@ func (c *Context) tmplSendMessage(filterSpecialMentions bool, returnID bool) fun
return ""
}

sendType := sendMessageGuildChannel
cid := c.ChannelArg(channel)
if cid == 0 {
return ""
}

isDM := cid != c.ChannelArgNoDM(channel)
if cid != c.ChannelArgNoDM(channel) {
sendType = sendMessageDM
}

var m *discordgo.Message
msgSend := &discordgo.MessageSend{
Expand All @@ -351,6 +354,7 @@ func (c *Context) tmplSendMessage(filterSpecialMentions bool, returnID bool) fun
case *discordgo.MessageEmbed:
msgSend.Embeds = []*discordgo.MessageEmbed{typedMsg}
case []*discordgo.MessageEmbed:
msgSend.Embeds = typedMsg
case *discordgo.MessageSend:
msgSend = typedMsg
if !filterSpecialMentions {
Expand All @@ -363,7 +367,7 @@ func (c *Context) tmplSendMessage(filterSpecialMentions bool, returnID bool) fun
msgSend.Content = ToString(msg)
}

if isDM {
if sendType == sendMessageDM {
msgSend.Components = []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
Expand Down Expand Up @@ -437,6 +441,7 @@ func (c *Context) tmplEditMessage(filterSpecialMentions bool) func(channel inter
}
msgEdit.Content = typedMsg.Content
msgEdit.Embeds = typedMsg.Embeds
msgEdit.Components = typedMsg.Components
msgEdit.AllowedMentions = typedMsg.AllowedMentions
default:
temp := fmt.Sprint(msg)
Expand Down
Loading