I heard the Messenger of Allah ﷺ say, "Whoever comes to this mosque of mine for no purpose other than to learn something good or to teach it is like a Mujahid in the path of Allah. Whoever comes for any other reason is like a man who merely gazes at another's possessions."
حدثنا أبو بكر بن أبي شيبة حدثنا حاتم بن إسمعيل عن حميد بن صخر عن المقبري
سمعت رسول الله ﷺ يقول من جاء مسجدي هذا لم يأته إلا لخير يتعلمه أو يعلمه فهو بمنزلة المجاهد في سبيل الله ومن جاء لغير ذلك فهو بمنزلة الرجل ينظر إلى متاع غيره
[Its chain of transmission is hasan] Ahmad reported it: 2/418, from the hadith of Hatim bin Ismail through him, and Ibn Hibban, Hakim, Dhahabi, and Busiri graded it sahih.