I am trying to fetch the guild owner Id's of the servers my bot is currently in. Everything I try returns undefined.
Current owner fetching from cache. I also tried guild.fetchOwner()
let owner = client.users.cache.get(guild.ownerID);
if (typeof owner !== 'undefined') {
console.log(owner)
} else {
console.log("Couldn't get owner!")
}
My for of loop:
for (const [id, guild] of client.guilds.cache) {}
CodePudding user response:
The Guild.fetchOwner
method fetches the owner from the API and always returns the owner.
let getOwners = async () => {
let owner = await guild.fetchOwner().catch(err => err)
return owner
}
getOwners().then(owner => {
if(owner !== undefined){
console.log(`ID: ${owner.user.id}\nUsername: ${owner.user.username}`)
}
})
What is happening?
- I created a async function called
getOwners()
that will fetch the owner of the current guild in the for loop.- then I called the function and if the returned owner !== undefined, you can do what you'd like with that.
I used an example of console logging the returned user data, just to show you some of the stuff you can access with the returned owner.
CodePudding user response:
Thanks guys! However I figured it out.
I moved this function to my ready event to ensure that the client had time to login. That is why it was returning undefined.