First of all, this code will console log every Guild#id when a new message is getting received by the bot.
client.on("message", message => {
const servers = client.guilds.cache.map(guild => guild.id);
console.log(servers);
})
I would recommend due to personal experience to map all guilds inside the ready
event.
To change the nickname from the bot (You actually don't need mapping all guilds) you could do this by writing this code into your ready
event:
client.guilds.cache.forEach((guild) => {
guild.me.setNickname(/* Nickname as a string */);
});
You should write this code into your ready event because if you write it just into the file you would try to change your nickname while you haven't even fetched all guilds from the API.
If you only want to do this after you executed a specific command, you can use the code from above and paste it into the command.
Reference:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…