Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #364 from rtrox/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
andersfylling authored Jan 5, 2021
2 parents ed89f04 + 31f2f05 commit 0442f95
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
8 changes: 8 additions & 0 deletions std/msgfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ func (f *msgFilter) IsByBot(evt interface{}) interface{} {
return messageIsBot(evt, true)
}

func (f *msgFilter) NotByWebhook(evt interface{}) interface{} {
return messageIsWebhook(evt, false)
}

func (f *msgFilter) IsByWebhook(evt interface{}) interface{} {
return messageIsWebhook(evt, true)
}

func (f *msgFilter) HasBotMentionPrefix(evt interface{}) interface{} {
return messageHasPrefix(evt, mentionString(f.botID))
}
Expand Down
80 changes: 80 additions & 0 deletions std/msgfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,86 @@ func TestMsgFilter_IsByBot(t *testing.T) {
}
}

func TestMsgFilter_NotByWebhook(t *testing.T) {
var webhookID disgord.Snowflake = 456
var botID disgord.Snowflake = 123

messageWithWebhookID := &disgord.Message{
Author: &disgord.User{},
WebhookID: webhookID,
}

messageWithoutWebhookID := &disgord.Message{
Author: &disgord.User{},
WebhookID: 0,
}

testCases := []struct {
name string
evt interface{}
shouldPassThrough bool
}{
{"MessageCreate_FromWebhook", &disgord.MessageCreate{Message: messageWithWebhookID}, false},
{"MessageUpdate_FromWebhook", &disgord.MessageUpdate{Message: messageWithWebhookID}, false},
{"MessageCreate_NotWebhook", &disgord.MessageCreate{Message: messageWithoutWebhookID}, true},
{"MessageUpdate_NotWebhook", &disgord.MessageUpdate{Message: messageWithoutWebhookID}, true},
}

filter, _ := newMsgFilter(context.Background(), &clientRESTMock{id: botID})

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := filter.NotByWebhook(tc.evt)
if tc.shouldPassThrough && result == nil {
t.Error("expected to passthrough")
}
if !tc.shouldPassThrough && result != nil {
t.Error("expected a filter match")
}
})
}
}

func TestMsgFilter_IsByWebhook(t *testing.T) {
var webhookID disgord.Snowflake = 456
var botID disgord.Snowflake = 123

messageWithWebhookID := &disgord.Message{
Author: &disgord.User{},
WebhookID: webhookID,
}

messageWithoutWebhookID := &disgord.Message{
Author: &disgord.User{},
WebhookID: 0,
}

testCases := []struct {
name string
evt interface{}
shouldPassThrough bool
}{
{"MessageCreate_FromWebhook", &disgord.MessageCreate{Message: messageWithWebhookID}, true},
{"MessageUpdate_FromWebhook", &disgord.MessageUpdate{Message: messageWithWebhookID}, true},
{"MessageCreate_NotWebhook", &disgord.MessageCreate{Message: messageWithoutWebhookID}, false},
{"MessageUpdate_NotWebhook", &disgord.MessageUpdate{Message: messageWithoutWebhookID}, false},
}

filter, _ := newMsgFilter(context.Background(), &clientRESTMock{id: botID})

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := filter.IsByWebhook(tc.evt)
if tc.shouldPassThrough && result == nil {
t.Error("expected to passthrough")
}
if !tc.shouldPassThrough && result != nil {
t.Error("expected a filter match")
}
})
}
}

func TestMsgFilter_ContainsBotMention(t *testing.T) {
var botID disgord.Snowflake = 123
filter, _ := newMsgFilter(context.Background(), &clientRESTMock{id: botID})
Expand Down
17 changes: 17 additions & 0 deletions std/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,20 @@ func messageIsBot(evt interface{}, isBot bool) interface{} {

return evt
}

func messageIsWebhook(evt interface{}, isWebhook bool) interface{} {
var msg *disgord.Message
if msg = getMsg(evt); msg == nil {
return nil
}

// Webhook ID is only set if message is sourced from a webhook.
// https://discord.com/developers/docs/resources/channel#message-object-message-structure
// "You can tell if a message is generated by a webhook by checking for the webhook_id
// on the message object."
if msg.WebhookID.IsZero() == isWebhook {
return nil
}

return evt
}

0 comments on commit 0442f95

Please sign in to comment.