Skip to content

Commit

Permalink
general: include attachments from forwards (#1828)
Browse files Browse the repository at this point in the history
* lib: add func get all attachments to msg structs

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

* general: include attachments from forwards

Include attachments from forwarded messages across various features of
the bot which reference a message's attachments.

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

* automod: trim superfluous code

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

---------

Signed-off-by: SoggySaussages <vmdmaharaj@gmail.com>
  • Loading branch information
SoggySaussages authored Jan 22, 2025
1 parent 2930dff commit 613be2d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion automod/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ func (mc *MessageAttachmentCondition) IsMet(data *TriggeredRuleData, settings in
return true, nil
}

if contains := len(data.Message.Attachments) > 0; mc.HasAttachments {
if contains := len(data.Message.GetMessageAttachments()) > 0; mc.HasAttachments {
return contains, nil
} else {
return !contains, nil
Expand Down
12 changes: 7 additions & 5 deletions automod/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ func (s *SlowmodeTrigger) UserSettings() []*SettingDef {
}

func (s *SlowmodeTrigger) CheckMessage(triggerCtx *TriggerContext, cs *dstate.ChannelState, m *discordgo.Message) (bool, error) {
if s.Attachments && len(m.Attachments) < 1 {
if s.Attachments && len(m.GetMessageAttachments()) < 1 {
return false, nil
}

Expand Down Expand Up @@ -794,12 +794,14 @@ func (s *SlowmodeTrigger) CheckMessage(triggerCtx *TriggerContext, cs *dstate.Ch
}

if s.Attachments {
if len(v.Attachments) < 1 {
vAttachments := v.GetMessageAttachments()

if len(vAttachments) < 1 {
continue // we're only checking messages with attachments
}
if settings.SingleMessageAttachments {
// Add the count of all attachments of this message to the amount
amount += len(v.Attachments)
amount += len(vAttachments)
} else {
amount++
}
Expand Down Expand Up @@ -1098,7 +1100,7 @@ func (spam *SpamTrigger) CheckMessage(triggerCtx *TriggerContext, cs *dstate.Cha
break
}

if len(v.Attachments) > 0 {
if len(v.GetMessageAttachments()) > 0 {
break // treat any attachment as a different message, in the future i may download them and check hash or something? maybe too much
}

Expand Down Expand Up @@ -1532,7 +1534,7 @@ func (mat *MessageAttachmentTrigger) UserSettings() []*SettingDef {
}

func (mat *MessageAttachmentTrigger) CheckMessage(triggerCtx *TriggerContext, cs *dstate.ChannelState, m *discordgo.Message) (bool, error) {
contains := len(m.Attachments) > 0
contains := len(m.GetMessageAttachments()) > 0
if contains && mat.RequiresAttachment {
return true, nil
} else if !contains && !mat.RequiresAttachment {
Expand Down
16 changes: 13 additions & 3 deletions lib/discordgo/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ type Message struct {
Activity *MessageActivity `json:"activity"`

// An array of StickerItem objects, is the message contains any.
StickerItems []*StickerItem `json:"sticker_items"`
ApplicationID int64 `json:"application_id,string"`
StickerItems []*StickerItem `json:"sticker_items"`
ApplicationID int64 `json:"application_id,string"`
}

type MessageSnapshot struct {
Expand All @@ -183,6 +183,16 @@ func (m *Message) GetMessageContents() []string {
return contents
}

func (m *Message) GetMessageAttachments() []*MessageAttachment {
attachments := m.Attachments
for _, s := range m.MessageSnapshots {
if s.Message != nil && len(s.Message.Attachments) > 0 {
attachments = append(attachments, s.Message.Attachments...)
}
}
return attachments
}

func (m *Message) GetGuildID() int64 {
return m.GuildID
}
Expand Down Expand Up @@ -261,7 +271,7 @@ type MessageSend struct {
AllowedMentions AllowedMentions `json:"allowed_mentions,omitempty"`
Reference *MessageReference `json:"message_reference,omitempty"`
Flags MessageFlags `json:"flags,omitempty"`
StickerIDs []int64 `json:"sticker_ids"`
StickerIDs []int64 `json:"sticker_ids"`

// TODO: Remove this when compatibility is not required.
File *File `json:"-"`
Expand Down
12 changes: 12 additions & 0 deletions lib/dstate/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,18 @@ func (m *MessageState) GetMessageContents() []string {
return contents
}

func (m *MessageState) GetMessageAttachments() []discordgo.MessageAttachment {
attachments := m.Attachments
for _, s := range m.MessageSnapshots {
if s.Message != nil && len(s.Message.Attachments) > 0 {
for _, a := range s.Message.Attachments {
attachments = append(attachments, *a)
}
}
}
return attachments
}

func (m *MessageState) ContentWithMentionsReplaced() string {
content := m.Content

Expand Down
2 changes: 1 addition & 1 deletion moderation/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ func (f *IgnorePinnedMessagesFilter) Matches(msg *dstate.MessageState) (delete b
type MessagesWithAttachmentsFilter struct{}

func (*MessagesWithAttachmentsFilter) Matches(msg *dstate.MessageState) (delete bool) {
return len(msg.Attachments) > 0
return len(msg.GetMessageAttachments()) > 0
}

// Only delete messages satisfying ToID<=id<=FromID.
Expand Down
2 changes: 1 addition & 1 deletion tickets/tickets_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ func createLogs(gs *dstate.GuildSet, conf *models.TicketConfig, ticket *models.T
for _, msg := range m {
// download attachments
OUTER:
for _, att := range msg.Attachments {
for _, att := range msg.GetMessageAttachments() {
msg.Content += fmt.Sprintf("(attachment: %s)", att.Filename)

totalAttachmentSize += att.Size
Expand Down

0 comments on commit 613be2d

Please sign in to comment.