Wai Siu-Bo (Stephen Chow) is a bard known for his quick wit and tall stories. One day, the police ambush Chan Kan-nam, the leader of the revolutionary Heaven and Earth Society, at the brothel where Siu-bo works. Siu-bo saves Chan Kan-nam and asks him to teach him kung-fu in return. Kan-nam accepts and inducts Siu-bo into the Heaven and Earth Society as his apprentice, with his first assignment being to infiltrate the palace and steal some secret materials. Members are marked by the Society's mantra on their feet, but Kan-nam only manages to write down two on Siu-bo's left foot before he flinches from ticklishness.
On the day of the palace recruitment, Siu-bo accidentally stumbles into the eunuch room before being saved by Hoi Tai-fu, the palace's head eunuch. Suspicious of the Empress Dowager's identity and unable to confront her himself, Tai-fu strongarms Siu-bo into stealing the Empress Dowager's copy of the Sutra of Forty-two Chapters. When Siu-bo is discovered, Tai-fu fights off the Empress, who possesses an unexpected level of combat prowess. Amidst the conflict, Siu-bo inadvertently befriends the Princess and the Emperor, thinking that they are eunuchs. The Emperor offers to give Siu-bo both his copy and the Empress' copies of the Sutra if he can defeat the Emperor in a fight. Siu-bo fights dirty and defeats the Emperor after grabbing his crotch, only to realize that the pair are royalty. Shortly afterward, one of the Emperor's seditious Generals, Oboi, bursts in to make demands. When Siu-bo successfully maneuvers the Emperor around a political confrontation with Oboi, and the Princess uncovers that Siu-bo is not a eunuch himself, he is promoted to the position of spy for the Emperor, told to keep an eye on Tai-fu and Oboi.
韋小寶(周星馳飾)是麗春院妓女韋春花(吳君如飾)的弟弟;一次在說書他與陳近南見面的虛構情節時,遇上鰲拜手下圍剿真正的陳近南(劉松仁飾)及青木堂堂主(劉江飾)。韋小寶最終救陳近南脫困,然而青木堂堂主已戰死。由於眾人皆看到是韋小寶救出陳近南,妓院老闆(李家鼎飾)要求陳近南帶韋小寶離開。因此陳近南只好帶韋小寶離開妓院,回天地會總部。
在天地會中,陳近南對群眾表示他探得清廷在關外藏有寶藏,事關滿清國運,秘密則藏在《四十二章經》中,需要有人潛入皇宮中取得。在眾人推托之際,陳近南使計讓韋小寶站出,派他潛入宮中,並要在他腳板上刺上「清明」、「反復」,做為天地會「反清復明」的暗號,以免將來被自己人所殺,在刺完「清明」後,韋小寶就說另一腳他以後會刺。