How do I check if a messages user has a specific role?

let channel = client.channels.cache.get(DISCORD_COLLABS_CHANNEL);
let messages = await channel.messages.fetch({limit: 100, cache: false});
console.log (' here is the first message', Array.from(messages)[0]);
let lastPostByAdmin = Array.from(messages).filter(m=>m[1].member.roles.cache.has(DISCORD_ADMIN_ROLE));
let channel = client.channels.cache.get(DISCORD_COLLABS_CHANNEL);
let messages = await channel.messages.fetch({limit: 100, cache: false});
console.log (' here is the first message', Array.from(messages)[0]);
let lastPostByAdmin = Array.from(messages).filter(m=>m[1].member.roles.cache.has(DISCORD_ADMIN_ROLE));
it doesn't like the last line, cant find .member (but on the docs it says messages have a member property containing the guild member?) I can do user, but it doesn't look like that lets you fetch the roles, just has the basic info.
5 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
skeddles
skeddlesOP2y ago
"discord.js": "^14.7.1",
failed to fetch latest collab TypeError: Cannot read properties of null (reading 'roles')
at /home/skeddles/lospec-website/projects/homepage/tasks/_update-homepage.js:148:68
at Array.filter (<anonymous>)
at getLatestCollab (/home/skeddles/lospec-website/projects/homepage/tasks/_update-homepage.js:148:46)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async getLatestNewsArticles (/home/skeddles/lospec-website/projects/homepage/tasks/_update-homepage.js:125:4)
at async module.exports.action (/home/skeddles/lospec-website/projects/homepage/tasks/_update-homepage.js:26:20)
at async /home/skeddles/lospec-website/projects/homepage/routes/_homepage.js:19:2
failed to fetch latest collab TypeError: Cannot read properties of null (reading 'roles')
at /home/skeddles/lospec-website/projects/homepage/tasks/_update-homepage.js:148:68
at Array.filter (<anonymous>)
at getLatestCollab (/home/skeddles/lospec-website/projects/homepage/tasks/_update-homepage.js:148:46)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async getLatestNewsArticles (/home/skeddles/lospec-website/projects/homepage/tasks/_update-homepage.js:125:4)
at async module.exports.action (/home/skeddles/lospec-website/projects/homepage/tasks/_update-homepage.js:26:20)
at async /home/skeddles/lospec-website/projects/homepage/routes/_homepage.js:19:2
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
skeddles
skeddlesOP2y ago
like this?
let lastPostByAdmin = messages.filter(async m => {
let author = await m.author.fetch();
return author.roles.cache.has(DISCORD_ADMIN_ROLE);
});
let lastPostByAdmin = messages.filter(async m => {
let author = await m.author.fetch();
return author.roles.cache.has(DISCORD_ADMIN_ROLE);
});
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server