Skip to content

Commit

Permalink
templates: add message forwarding (#1764)
Browse files Browse the repository at this point in the history
* templates: add message forward func

Signed-off-by: SoggySaussages <vmdmaharaj@gmail.com>

* templates: add msg forward guild checks

Signed-off-by: SoggySaussages <vmdmaharaj@gmail.com>

* templates: fix incorrect CreateMessageSend err

Signed-off-by: SoggySaussages <vmdmaharaj@gmail.com>

---------

Signed-off-by: SoggySaussages <vmdmaharaj@gmail.com>
Co-authored-by: Ashish <51633862+ashishjh-bst@users.noreply.github.com>
  • Loading branch information
SoggySaussages and ashishjh-bst authored Nov 20, 2024
1 parent ad2f8b8 commit 3f11dde
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
26 changes: 26 additions & 0 deletions common/templates/context_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ func (c *Context) tmplSendDM(s ...interface{}) string {
}
msgSend.Components = append(serverInfo, msgSend.Components...)

if msgSend.Reference != nil {
if msgSend.Reference.Type == discordgo.MessageReferenceTypeForward {
if originChannel := c.ChannelArgNoDM(msgSend.Reference.ChannelID); originChannel != 0 {
hasPerms, _ := bot.BotHasPermissionGS(c.GS, originChannel, discordgo.PermissionViewChannel|discordgo.PermissionReadMessageHistory)
if !hasPerms {
msgSend.Reference = &discordgo.MessageReference{}
}
} else {
msgSend.Reference = &discordgo.MessageReference{}
}
}
}

channel, err := common.BotSession.UserChannelCreate(c.MS.User.ID)
if err != nil {
return ""
Expand Down Expand Up @@ -390,6 +403,19 @@ func (c *Context) tmplSendMessage(filterSpecialMentions bool, returnID bool) fun
msgSend.Components = append(serverInfo, msgSend.Components...)
}

if msgSend.Reference != nil {
if msgSend.Reference.Type == discordgo.MessageReferenceTypeForward {
if originChannel := c.ChannelArgNoDM(msgSend.Reference.ChannelID); originChannel != 0 {
hasPerms, _ := bot.BotHasPermissionGS(c.GS, originChannel, discordgo.PermissionViewChannel|discordgo.PermissionReadMessageHistory)
if !hasPerms {
msgSend.Reference = &discordgo.MessageReference{}
}
} else {
msgSend.Reference = &discordgo.MessageReference{}
}
}
}

m, err = common.BotSession.ChannelMessageSendComplex(cid, msgSend)

if err == nil && returnID {
Expand Down
42 changes: 37 additions & 5 deletions common/templates/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package templates

import (
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"math"
Expand All @@ -10,9 +12,7 @@ import (
"strconv"
"strings"
"time"
"crypto/sha256"
"encoding/base64"


"emperror.dev/errors"
"github.com/botlabs-gg/yagpdb/v2/bot"
"github.com/botlabs-gg/yagpdb/v2/common"
Expand Down Expand Up @@ -431,6 +431,38 @@ func CreateMessageSend(values ...interface{}) (*discordgo.MessageSend, error) {
}
msg.Components = append(msg.Components, discordgo.ActionsRow{[]discordgo.MessageComponent{menu}})
}
case "forward":
if val == nil {
continue
}
var m map[string]interface{}
switch t := val.(type) {
case SDict:
m = t
case *SDict:
m = *t
case map[string]interface{}:
m = t
default:
return nil, errors.New("invalid value passed to forward; must be an sdict with channel and message")
}

msg.Reference = &discordgo.MessageReference{
Type: 1,
}
for k, v := range m {
switch strings.ToLower(k) {
case "channel":
msg.Reference.ChannelID = ToInt64(v)
if msg.Reference.ChannelID <= 0 {
return nil, errors.New(fmt.Sprintf("invalid channel id '%s' provided to forward.", ToString(val)))
}
case "message":
msg.Reference.MessageID = ToInt64(v)
if msg.Reference.MessageID <= 0 {
return nil, errors.New(fmt.Sprintf("invalid message id '%s' provided to forward.", ToString(val)))
}
}
case "sticker":
if val == nil {
continue
Expand All @@ -445,7 +477,7 @@ func CreateMessageSend(values ...interface{}) (*discordgo.MessageSend, error) {
msg.StickerIDs = append(msg.StickerIDs, ToInt64(val))
}
default:
return nil, errors.New(`invalid key "` + key + `" passed to send message builder`)
return nil, errors.New(`invalid key "` + key + `" passed to send message builder.`)
}

}
Expand Down Expand Up @@ -1671,7 +1703,7 @@ func tmplEncodeBase64(str string) string {
func tmplSha256(str string) string {
hash := sha256.New()
hash.Write([]byte(str))

sha256 := base64.URLEncoding.EncodeToString(hash.Sum(nil))

return sha256
Expand Down

0 comments on commit 3f11dde

Please sign in to comment.