Skip to content

Commit

Permalink
Merge pull request #22 from elliotwms/autopin
Browse files Browse the repository at this point in the history
Trigger channel import on new classic pins
  • Loading branch information
elliotwms authored Dec 22, 2021
2 parents b24858e + 1a25cf2 commit f8a81d0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
16 changes: 16 additions & 0 deletions internal/eventhandlers/channel_pins_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package eventhandlers

import (
"github.com/bwmarrin/discordgo"
"github.com/elliotwms/pinbot/internal/commandhandlers"
"github.com/sirupsen/logrus"
)

func ChannelPinsUpdate(log *logrus.Entry) func(s *discordgo.Session, e *discordgo.ChannelPinsUpdate) {
return func(s *discordgo.Session, e *discordgo.ChannelPinsUpdate) {
commandhandlers.ImportChannelCommandHandler(&commandhandlers.ImportChannelCommand{
ChannelID: e.ChannelID,
GuildID: e.GuildID,
}, s, log)
}
}
1 change: 1 addition & 0 deletions internal/pinbot/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func (bot *Bot) registerHandlers() func() {
bot.session.AddHandler(eventhandlers.ChannelUpdate(bot.log.WithField(logFieldHandler, "ChannelUpdate"))),
bot.session.AddHandler(eventhandlers.ChannelDelete(bot.log.WithField(logFieldHandler, "ChannelDelete"))),
bot.session.AddHandler(eventhandlers.InteractionCreate(bot.log.WithField(logFieldHandler, "InteractionCreate"))),
bot.session.AddHandler(eventhandlers.ChannelPinsUpdate(bot.log.WithField(logFieldHandler, "ChannelPinsUpdate"))),
}

return func() {
Expand Down
15 changes: 14 additions & 1 deletion internal/tests/pin_stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,10 @@ func (s *PinStage) the_bot_should_log_the_message_as_an_avoided_self_pin() {
}, 1*time.Second, 10*time.Millisecond)
}

func (s *PinStage) the_message_is_pinned() {
func (s *PinStage) the_message_is_pinned() *PinStage {
s.require.NoError(s.session.ChannelMessagePin(s.channel.ID, s.message.ID))

return s
}

func (s *PinStage) an_import_is_triggered() {
Expand Down Expand Up @@ -281,3 +283,14 @@ func (s *PinStage) the_pin_message_should_have_n_embeds(n int) *PinStage {

return s
}

func (s *PinStage) the_import_is_cleaned_up() *PinStage {
s.a_pin_message_should_be_posted_in_the_last_channel()

s.require.NoError(s.session.ChannelMessageDelete(s.pinMessage.ChannelID, s.pinMessage.ID))
s.messages = []*discordgo.Message{}

s.require.NoError(s.session.MessageReactionsRemoveAll(s.message.ChannelID, s.message.ID))

return s
}
23 changes: 19 additions & 4 deletions internal/tests/pin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,30 @@ func TestPinSelfPinDisabled(t *testing.T) {
the_bot_should_log_the_message_as_an_avoided_self_pin()
}

func TestPinClassicPinTriggersChannelImport(t *testing.T) {
given, when, then := NewPinStage(t)

given.
a_channel_named("test").and().
the_message_is_posted()

when.
the_message_is_pinned()

then.
a_pin_message_should_be_posted_in_the_last_channel().and().
the_bot_should_add_the_emoji("👀").and().
the_bot_should_add_the_emoji("✅")
}

func TestPinImportCommand(t *testing.T) {
given, when, then := NewPinStage(t)

given.
a_channel_named("test").and().
the_message_is_posted().and().
the_message_is_pinned()
the_message_is_pinned().and().
the_import_is_cleaned_up()

when.
an_import_is_triggered()
Expand All @@ -111,9 +128,7 @@ func TestPinImportCommandIgnoreAlreadyPinned(t *testing.T) {
an_import_is_triggered()

then.
a_pin_message_should_be_posted_in_the_last_channel().and().
the_bot_should_add_the_emoji("👀").and().
the_bot_should_add_the_emoji("✅")
the_bot_should_log_the_message_as_already_pinned()
}

func TestPinWithImage(t *testing.T) {
Expand Down

0 comments on commit f8a81d0

Please sign in to comment.