timotejroiko / discord.js-light

All the power of discord.js, zero caching. This library modifies discord.js's internal classes and functions in order to give you full control over its caching behaviour.
Apache License 2.0
292 stars 29 forks source link

Owner Name and NickName #17

Closed develogo closed 4 years ago

develogo commented 4 years ago

I'm trying to load the information from the server and save it to a database. I'm getting information like name, id, urlImage, owner id ... But I want to get the owner's name, and I'm getting a null result. I know that DiscordLight does not store cache. Would there be any other way of obtaining this information?

const Discord = require('discord.js-light');
const client = new Discord.Client({
  cacheGuilds: true,
  cacheChannels: true,
  cacheOverwrites: false,
  cacheRoles: true,
  cacheEmojis: false,
  cachePresences: false
});

client.on('ready', async () => {
  console.log(`Logged in as ${client.user.tag}!`);

  client.guilds.cache.forEach((value) => {
    console.log(value.name);  //return Guild Name
    console.log(value.id); //return Guild ID
    console.log(value.iconURL()); //return urlImage
    console.log(value.owner); //return null
    console.log(value.ownerID); //return Owner Id
  });    
});
timotejroiko commented 4 years ago

You need to fetch the owner:

let owner = await client.users.fetch(value.ownerID)

If you dont want to cache the guild owner, you can add false in the second parameter:

let owner = await client.users.fetch(value.ownerID, false)

Then you can get it from owner.username

develogo commented 4 years ago

Thanks, this worked for me, I just changed to get the name of the owner of each server. It looked like this: let owner = await value.owner.fetch(value.ownerID)

Congratulations for the incredible work!

timotejroiko commented 4 years ago

If you're gonna use User#fetch then you dont need to pass any parameter, just await value.owner.fetch() is enough since it already implies the ID contained in the owner. The first parameter is a boolean value for whether or not to bypass the cache when fetching.