Doesn't DiscordAPI reflect the latest role position?

I made a test guild for my bots. The guild has this roles scoreboard(?) - Owner - Mod - Member Guild has these members: - Me - My bot - Tom I gave Tom "Member" role (its position is "1") while I have "Owner" role (its position is "3") This is a code the bot has:
//...
client.on('messageCreate', async message => {
if(message.content !== 'test') return;

const myGuild = await client.guilds.fetch('GuildID');
// "fetch" a guild
try { await myGuild.members.fetch(message.author.id) } catch (e){ return message.channel.send("Author is not a member of my guild."); };
// If message author is not a member of the guild
try { await myGuild.members.fetch('UserID') } catch(e) { return message.channel.send('UserID is not a member of my guild.'); };
// If the user is not a member of the guild
const authorMember = await myGuild.members.fetch(message.author.id),
// "fetch" message author
authorRank = await authorMember.roles.highest.position,
// Author's role position
targetMember = await myGuild.members.fetch('UserID'),
// "fetch" the user
targetRank = await targetMember.roles.highest.position;
// The user's role position

console.log(`Author's role position: ${authorRank}\nThe Member's role position: ${targetRank}`);
})
//...
//...
client.on('messageCreate', async message => {
if(message.content !== 'test') return;

const myGuild = await client.guilds.fetch('GuildID');
// "fetch" a guild
try { await myGuild.members.fetch(message.author.id) } catch (e){ return message.channel.send("Author is not a member of my guild."); };
// If message author is not a member of the guild
try { await myGuild.members.fetch('UserID') } catch(e) { return message.channel.send('UserID is not a member of my guild.'); };
// If the user is not a member of the guild
const authorMember = await myGuild.members.fetch(message.author.id),
// "fetch" message author
authorRank = await authorMember.roles.highest.position,
// Author's role position
targetMember = await myGuild.members.fetch('UserID'),
// "fetch" the user
targetRank = await targetMember.roles.highest.position;
// The user's role position

console.log(`Author's role position: ${authorRank}\nThe Member's role position: ${targetRank}`);
})
//...
GuildID is my guild UserID is Tom's ID. Then, in my anothor guild, (my bot belongs to) I sent "test". Console:
Author's role position: 3
The Member's role position: 1
Author's role position: 3
The Member's role position: 1
It is very a result I had expected. Then, I gave Tom "Mod" role (its position is "2") I send "test" in the anothor guild again. Console:
Author's role position: 3
The Member's role position: 1
Author's role position: 3
The Member's role position: 1
The result was not changed. I can't understand.
2 Replies
d.js toolkit
d.js toolkit11mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Ropyle
Ropyle11mo ago
Thanks for your answer, but the problem is still here. Current code:
//...
client.on('messageCreate', async message => {
if(message.content !== 'test') return;

const myGuild = await client.guilds.fetch('GuildID', true);
// "fetch" a guild
try { await myGuild.members.fetch(message.author.id, true) } catch (e){ return message.channel.send("Author is not a member of my guild."); };
// If message author is not a member of the guild
try { await myGuild.members.fetch('UserID', true) } catch(e) { return message.channel.send('UserID is not a member of my guild.'); };
// If the user is not a member of the guild
const authorMember = await myGuild.members.fetch(message.author.id, true),
// "fetch" message author
authorRank = await authorMember.roles.highest.position,
// Author's role position
targetMember = await myGuild.members.fetch('UserID', true),
// "fetch" the user
targetRank = await targetMember.roles.highest.position;
// The user's role position

console.log(`Author's role position: ${authorRank}\nThe Member's role position: ${targetRank}`);
})
//...
//...
client.on('messageCreate', async message => {
if(message.content !== 'test') return;

const myGuild = await client.guilds.fetch('GuildID', true);
// "fetch" a guild
try { await myGuild.members.fetch(message.author.id, true) } catch (e){ return message.channel.send("Author is not a member of my guild."); };
// If message author is not a member of the guild
try { await myGuild.members.fetch('UserID', true) } catch(e) { return message.channel.send('UserID is not a member of my guild.'); };
// If the user is not a member of the guild
const authorMember = await myGuild.members.fetch(message.author.id, true),
// "fetch" message author
authorRank = await authorMember.roles.highest.position,
// Author's role position
targetMember = await myGuild.members.fetch('UserID', true),
// "fetch" the user
targetRank = await targetMember.roles.highest.position;
// The user's role position

console.log(`Author's role position: ${authorRank}\nThe Member's role position: ${targetRank}`);
})
//...
Also, I found when the code gets restarted, He gets the latest one. Now I am thinking of one bulldozing idea: When processing finished process.exit(0); so that every time the command get called, my bot can get Tom/Himself's latest position. completely Thonkang
myGuilds.members.fetch({ user: "UserID", force: true });
myGuilds.members.fetch({ user: "UserID", force: true });
this way ? Yes it is! Thanks all you guys and have a nice day!