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

GuildChannel.permissionsFor(user) returns null with all caches enabled #15

Closed KevinNovak closed 4 years ago

KevinNovak commented 4 years ago

When using the GuildChannel.permissionsFor(), null is returned.

Example:

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

const MY_TOKEN = '{INSERT_TOKEN_HERE}';

(() => {
    client.on('message', msg => {
        if (msg.content.toLowerCase() !== 'test') {
            return;
        }

        // This comes back null
        let authorPerms = msg.channel.permissionsFor(msg.author);

        // This will fail because the above is null
        let authorIsAdmin = authorPerms.has('ADMINISTRATOR');

        msg.channel.send(authorIsAdmin ? 'You are an admin!' : 'You are not an admin!');
    });

    client.login(MY_TOKEN);
})();
timotejroiko commented 4 years ago

Hey there,

it fails because msg.author resolves to a user ID, and then looks into the member cache to find a matching ID but there is none since members are not cached automatically. You can however use .permissionsFor(msg.member) instead which takes the member instance directly from the message and bypasses the cache.

KevinNovak commented 4 years ago

Thank you for the explanation, I saw that about members not being cached automatically but didn't put it together. I'll mark this as closed.