trevdev
trevdev
DIAdiscord.js - Imagine an app
Created by trevdev on 6/22/2024 in #djs-questions
How can I check if a user id has permission to send messages to a channel?
Hello, I am trying to check if a user has permission to send messages to a channel like this:
const canSpeak = channel.permissionsFor(senderId)?.has('SendMessages')
const canSpeak = channel.permissionsFor(senderId)?.has('SendMessages')
But this seems to always return true even when it shouldn't. Are there some intents I need to enable, and is this the wrong way to do this? I just need a basic way to tell if they should be able to chat in the channel. Could I just check if they are a member of the channel? I haven't used Discord all that much but I feel like it is possible to be a member of a channel yet still not have permission to chat in it. I would also like to know how to do this for reading messages/reading message history. Thank you!
7 replies
DIAdiscord.js - Imagine an app
Created by trevdev on 6/22/2024 in #djs-questions
guild.members.fetch will still fetch those who have left the server
Hey everyone, I am trying to make a simple function that checks if a user is in a server.
export async function userIsInGuild(userId: string, guildId: string) {
try {
const guild = await client.guilds.fetch(guildId)
if (!guild) {
return false
}

const member = await guild.members.fetch(userId)
if (!member) {
return false
}

return true
} catch {
return false
}
}
export async function userIsInGuild(userId: string, guildId: string) {
try {
const guild = await client.guilds.fetch(guildId)
if (!guild) {
return false
}

const member = await guild.members.fetch(userId)
if (!member) {
return false
}

return true
} catch {
return false
}
}
The problem is that when a user leaves the server, guild.members.fetch does not seem to be up to date. It will return a member as if they were still in the server. But it does work fine the other way around, if the client is already initialized and a player joins the server, it will be up to date with the fact that they joined. It just doesn't seem to work someone leaves the server. It will throw an error however if the client was initialized while the user was not in the server, which is good. It just can't tell who leaves the server during runtime. Is this normal behavior? Is there a way to make it so guild.members.fetch will always be accurate?
16 replies