There is some code on this page that might help.
https://docs.godotengine.org/en/stable/tutorials/physics/ray-casting.html
But for many things, it will be easier to use a RayCast node and place it on the gun barrel and set it up in the editor. You can still alter the properties with code, but it makes it easier to see what is going on.
In terms of random spread, you probably want to use a single ray cast (as I said, with a RayCast node pointing straight from the gun) find the collision point, and then spawn an Area or other collision shape that is about the size of the spread. Or you can take the collision point and just generate some other vectors in code that are randomly close to that point. This works for a single object.
If you need to account for multiple enemies or objects it gets a little more complex. There are many ways, but you probably don't want to fire 100 ray casts to simulate a buck shot. Though I guess if you limited it to say 5 or 10 rays (with random offset) that should be doable.