It's not working in pm, since the $chan identifier has no value in a pm, so there's no information in regards to where the person is to be banned from.

Change $chan in your modified code to the actual channel name.