The way my slash command handler works is I iterate through all the current client guilds, add an object to an array with the keys ‘id’ and ‘commands’, id being an integer of the guild id and commands being an array.
Then, iterate through the commands directory, require the file, check if the slash command builder has a ‘guilds’ element, in which the code will iterate through the guilds element and add the command builder to every guild commands array in the beforementioned guild array.
Then, once all is complete, iterate through the guilds array, find the guild by id, commands.set. All is working, except it only adds the commands to one of the guilds. In the iteration forEach of the guild array, I’ve made it console.log the id of the guild, and console.log after the setting of the commands. Both log properly, no errors thrown, but only my original testing server has the commands registered, and not a new server that a friend owns.
Ensured all intents are valid, administrator issued and role is high as it needs to be. Super confused!
let guilds = []
client.guilds.cache.forEach(guild => {
guilds.push({
id: guild.id,
commands: []
})
})
function addToGuild(id, obj) {
guilds.forEach(guild => {
if (guild.id == id) {
guild.commands.push(obj)
return true
}
})
}
fs.readdirSync(`${process.cwd()}/commands`).forEach(file => {
if (file.endsWith('.js')) {
let requiry = require(`${process.cwd()}/commands/${file}`)
if (requiry.command.guilds) {
requiry.command.guilds.forEach(guild => {
addToGuild(guild, requiry.command)
})
} else {
guilds.forEach(guild => {
guild.commands.push(requiry.command)
})
}
}
})
guilds.forEach((guild) => {
console.log(guild.id)
client.guilds.cache.find(guild => guild.id == guild.id).commands.set(guild.commands)
})