Believe it or not, the earth is a FAR better place for temperature regulation, including heat sinks. Add to that protection from radiation, and the Earth is by far the better solution for longevity. Availability of skilled workers for maintenance (essentially AI doctors) is also a prime consideration. Let's look at each of these issues in sequence;
You'd think that being in space (which is cold), the moon would be cold, right? Actually, it doesn't work like that. The earth has a massive ocean covering over two thirds of its surface, and a dense atmosphere covering all of it's surface that extends up quite a way. Both these attributes act as thermal masses. What thermal masses do is essentially 'even out' temperatures in that the ocean retains a massive amount of the heat energy that hits it from sunlight, and then slowly releases it through the night (or winter, etc.) What this means in effect is that the massive difference in temperature between night and day is evened out quite a bit; it's also the reason why days are hot and nights are cold in deserts. Very little water acting as that thermal mass. In Australia, we see this all the time when looking at temperatures inland compared to those on the coast. Coastal towns have warmer winters and cooler summers by comparison to many inland regional centres.
The atmosphere also does this, but to a far lesser degree. Nonetheless, this is why heat sinks work so well in computers and other industrial applications; they take the heat from the source, conduct it through a piece of metal with a massive surface area, which then passes the heat into the atmosphere around it. This in turn heats up the atmosphere a little but does so by drawing the heat away from the massively imbalanced heat sink.
The problem with the Moon in this scenario is that days on the moon will be very hot, nights will be extremely cold, and the heat generated in the computer infrastructure won't radiate out from the heat sinks because there's no medium (water or atmosphere) to carry it away. Add to that the impact of massive changes in temperature on things like batteries and you have really tough conditions in which to maintain your infrastructure housing this AI.
Then there's the Earth's magnetic field and ozone layer, each of which serve to protect the Earth from cosmic radiation. Unless you put your infrastructure underground the cosmic radiation will cook all but the most hardened computers pretty quickly, and underground creates logistical problems for your ability to access Earth for communications, or even the capacity to operate your space guns or other tools you mention in the question. But, for the sake of the argument underground may be less practical but it's doable, to a degree. You probably don't need to worry about pressurisation unless you have humans with you (more on that later) so that presents less of an issue, but you'd still need to mine out the tunnels for your hardware beforehand. It's a complication you don't need to worry about on Earth, meaning that Earth is still the most convenient location so far.
So; let's assume that your AI is sufficiently well versed in computer construction so as to perform maintenance and upgrades on itself. That works, to a point just like a human who get's sick or pulls a muscle knows what to do about it to recover. Sometimes though you end up with a situation that you can't resolve yourself. For a human, you'd go to a doctor. For an AI, you'd engage a trusted computer infrastructure engineer.
Last time I checked, there weren't many of those on the moon.
The reality is that the AI in all but the most remote or extreme versions of SciFi still rely on humans to varying degrees for their survival and this won't change. It's no different to our own society where despite our increasingly comprehensive knowledge of the human body at the layman level, we still need doctors. So would an AI, and the moon is just a difficult place to get that kind of assistance unless it's been colonised by humans, and then the question of which celestial body to reside on is more or less moot.
When you get right down to it, rare earth mineral availability aside, Earth has a lot of benefits for an AI for the same reason it does for humans; it's a protected place with a more even temperature variation than most other places in the solar system (despite what it feels like most of the time), protection from harmful radiation and a ready supply of trained personnel who can help care for it at those times of... shall we say illness? In any event, it's a good place for your AI, more so when you consider the availability of many different energy generation possibilities (equivalent of a food source) on top of all the points made so far. Put simply, your AI (like us all) needs to pick and choose who it trusts on Earth because we're all reliant on each other to some degree and your AI is no different in that regard. Moving to a human unfriendly environment only solves some problems by creating others.