It looks like you want to be able to always get images even after exhausting the list, but that you don't want to have duplicates until you've gone through the entire array.
Here is a possible implementation using a generator function and the Fisher-Yates shuffle:
// shuffle a copy of the array
function shuffled(arr) {
const copy = arr.slice();
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = copy[i];
copy[i] = copy[j];
copy[j] = temp;
}
return copy;
}
// give next element in array; if exhausted, shuffle and repeat
function* imageGenerator(arr) {
while (true) {
for (const e of shuffled(arr)) {
yield e;
}
}
}
// proof of concept; [1, 2, 3] would be replaced with your array
const images = imageGenerator([1, 2, 3]);
for (let i = 0; i < 5; i++) {
// get next image
console.log(images.next().value);
}
.as-console-wrapper {min-height: 100%;}
Here's how it might look in your case:
const discord = require('discord.js');
const image = require('../image.json')
// shuffle a copy of the array
function shuffled(arr) {
const copy = arr.slice();
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = copy[i];
copy[i] = copy[j];
copy[j] = temp;
}
return copy;
}
// give next element in array; if exhausted, shuffle and repeat
function* imageGenerator(arr) {
while (true) {
for (const e of shuffled(arr)) {
yield e;
}
}
}
const images = imageGenerator(image);
exports.run = async (client, message, args) => {
const embed = new Discord.MessageEmbed()
.setColor('RED')
.setDescription(`Dog`)
.setImage(images.next().value)
await message.channel.send(embed);
}