Fixing a function that receives commands as arguments

That’s probably because lua uses % for patterns, see https://www.lua.org/pil/20.2.html. I assume it should be enough to replace % by %% in your code, but I can’t test right now.