diff --git a/examples/bot_api.rb b/examples/bot_api.rb index 0653757db85e05f7efe4a85c26950cbac57af189..4481a2b00a81530e4457a639ebcfbd58664fc73c 100755 --- a/examples/bot_api.rb +++ b/examples/bot_api.rb @@ -23,6 +23,16 @@ command :spam, only: :dm, desc: 'Spams a bunch of nonsense' do |message_count = end end +command(:thumbsup, desc: 'Gives you a thumbs up', only: -> { room.user_can_send? client.mxid, 'm.reaction' }) do + room.send_event 'm.reaction', { + 'm.relates_to': { + rel_type: 'm.annotation', + event_id: event[:event_id], + key: '👍️' + } + } +end + command :echo, desc: 'Echoes the given message back as an m.notice' do |message| break if message.nil? # Don't echo empty requests diff --git a/lib/matrix_sdk/bot/base.rb b/lib/matrix_sdk/bot/base.rb index 4dd2879556cf465e03fc924c48e8236f2a6acc61..289caf2bb5a3c71adc9ea2035f050a157fdf0019 100644 --- a/lib/matrix_sdk/bot/base.rb +++ b/lib/matrix_sdk/bot/base.rb @@ -531,7 +531,7 @@ module MatrixSdk::Bot @event = MatrixSdk::Response.new(client.api, event.dup) return false if [handler.data[:only]].flatten.compact.any? do |only| if only.is_a? Proc - instance_exec(&only) + !instance_exec(&only) else case only.to_s.downcase.to_sym when :dm diff --git a/test/bot_test.rb b/test/bot_test.rb index b8fc6ce32092ae21ae0aaa4fbd530c870cad56ff..2b791c1932f548ba3ac64433ad88d9c84097c86d 100644 --- a/test/bot_test.rb +++ b/test/bot_test.rb @@ -22,6 +22,10 @@ class BotTest < Test::Unit::TestCase bot.send :test_event, event.event_id end + command(:test_only_proc, only: -> { room.user_can_send? client.mxid, 'm.reaction' }) do + bot.send :test_only_proc + end + event 'dev.ananace.ruby-sdk.TestEvent' do bot.send :test_state_event end @@ -159,6 +163,29 @@ class BotTest < Test::Unit::TestCase assert @bot.command_allowed? 'test_only', ev @bot.send :_handle_event, ev + ev = { + type: 'm.room.message', + sender: '@bob:example.com', + room_id: @id, + content: { + msgtype: 'm.text', + body: '!test_only_proc' + } + } + + @room.stubs(:user_can_send?).with('@alice:example.com', 'm.reaction').returns(false) + @bot.expects(:test_only_proc).never + + refute @bot.command_allowed? 'test_only_proc', ev + @bot.send :_handle_event, ev + + @room.stubs(:user_can_send?).with('@alice:example.com', 'm.reaction').returns(true) + + @bot.expects(:test_only_proc).once + + assert @bot.command_allowed? 'test_only_proc', ev + @bot.send :_handle_event, ev + @room.stubs(:dm?).returns(false) @bot.expects(:test_executed).never @@ -216,6 +243,7 @@ class BotTest < Test::Unit::TestCase def test_builtin_help @room.stubs(:dm?).returns(false) + @room.stubs(:user_can_send?).returns(false) @room.expects(:send_notice).with(<<~MSG.strip) Usage: @@ -254,6 +282,7 @@ class BotTest < Test::Unit::TestCase } @room.stubs(:dm?).returns(true) + @room.stubs(:user_can_send?).returns(true) @room.expects(:send_notice).with(<<~MSG.strip) Usage: @@ -263,6 +292,7 @@ class BotTest < Test::Unit::TestCase !test_arr [ARGS...] !test_only !test_event + !test_only_proc MSG @bot.send :_handle_event, {