Move chat command handling code from C++ to Lua (#5528)