You didn't actually post the code that's adding that
spawnMenu function, but basically you can replace that whole part with:
Code:
if unit:match("^raid") then
frame:SetAttribute("type2", "menu")
else
frame:SetAttribute("type2", "togglemenu")
end
You don't need to write your own function to toggle the menu anymore.