Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channel permissionsFor can be null #5056

Closed
1 task done
Koyamie opened this issue Nov 28, 2020 · 8 comments · Fixed by #5066 or #5260
Closed
1 task done

Channel permissionsFor can be null #5056

Koyamie opened this issue Nov 28, 2020 · 8 comments · Fixed by #5066 or #5260

Comments

@Koyamie
Copy link
Contributor

Koyamie commented Nov 28, 2020

Please describe the problem you are having in as much detail as possible:
When using Channel.permissionsFor, function can return null but there is no check for that.

Include a reproducible code sample here, if possible:
I havn't used permissionsFor directly but used to check Message.deleted and received an error.

TypeError: Cannot read property 'has' of null
    at Message.get deletable [as deletable] (/home/koyamie/Koya/node_modules/discord.js/src/structures/Message.js:412:69)

Example here: https://github.com/discordjs/discord.js/blob/master/src/structures/Message.js#L412

Further details:

  • discord.js version: latest master commit
  • Node.js version: 14.15.1
  • Operating system: debian 9
  • Priority this issue should have – please be realistic and elaborate if possible: medium

Relevant client options:

  • partials: ['REACTION','MESSAGE']
  • gateway intents: 13999
  • other: none
  • I have also tested the issue on latest master, commit hash:
@andyearnshaw
Copy link

Can someone please explain why channelPermissions can be null? Over the last week or so I'm seeing it return null for the following:

        if (!discordChannel || !discordChannel.permissionsFor(discordChannel.guild.me).has('SEND_MESSAGES')) {

This never happened before the upgrade to v12, but perhaps it's related to something else?

@Extroonie
Copy link
Contributor

Can someone please explain why channelPermissions can be null? Over the last week or so I'm seeing it return null for the following:

        if (!discordChannel || !discordChannel.permissionsFor(discordChannel.guild.me).has('SEND_MESSAGES')) {

This never happened before the upgrade to v12, but perhaps it's related to something else?

It is only null if it couldn't resolve the given argument (user or role).
In your provided snippet, it seems like you somehow lost your ClientUser representing the GuildMember of the given guild, which shouldn't generally happen unless you explicitly delete it from the guild member cache or the bot is removed from the guild. In case of the former, you can fetch it again using the GuildMemberManager#fetch method (though you shouldn't be removing it from the cache at the first point).

@andyearnshaw
Copy link

andyearnshaw commented Jan 25, 2021

@Extroonie it doesn't seem like either of those things are the issue as the error occurs intermittently. I don't have any code that intentionally deletes from the guild member cache, and if the bot had been kicked from a guild this would happen every time. One of my fellow developers has been able to reproduce this on his local machine several times, but again this is intermittent.

I have this code running on startup to resume what we call subscriptions, where a user can subscribe to game updates for a particular channel. We check that the bot still has permission to send messages for a channel that has an active subscription, and remove the subscription if the check fails. I'm apprehensive about using ?. as it seems like there's an unknown here and I don't want to remove subscriptions for users who have not done anything wrong.

@Extroonie
Copy link
Contributor

@andyearnshaw I wasn't able to reproduce that on version 12.5.1 (latest). Could you please create a reprex? and the fetching approach should just work fine.

@almostSouji
Copy link
Member

permissionsFor(memberOrRole) {
const member = this.guild.members.resolve(memberOrRole);
if (member) return this.memberPermissions(member);
const role = this.guild.roles.resolve(memberOrRole);
if (role) return this.rolePermissions(role);
return null;

Null is returned if neither a GuildMember nor a Role instance can be resolved from the provided argument. This would for example be the case if your bot's GuildMember is not cached. (Should that be the case <Guild>.me will already return null, meaning null is what arrives in #permissionsFor, which will, in turn, cause #permissionsFor to return null)

If you have further questions about discord.js usage, please ask them on the discord, this issue was resolved with the linked PR and commit.


If you need help with discord.js installation or usage, please go to the discord.js Discord server instead:
https://discord.gg/bRCvFy9
This issue tracker is only for bug reports and enhancement suggestions. You won't receive any basic help here.

@kyranet
Copy link
Member

kyranet commented Jan 25, 2021

I have created #5260 to make permissionsFor's return types to reflect what the code exactly does, hopefully this will resolve all the concerns regarding when the method returns null and when it doesn't.

@kyranet kyranet linked a pull request Jan 25, 2021 that will close this issue
5 tasks
@andyearnshaw
Copy link

I'll work on making sure guild.me is not null and see if that resolves the issue. It might be worth mentioning in the docs why guild.me might be null.

@kyranet
Copy link
Member

kyranet commented Jan 25, 2021

I'll work on making sure guild.me is not null and see if that resolves the issue. It might be worth mentioning in the docs why guild.me might be null.

Under normal circunstances, guild.me is never null even if you only have the GUILDS intent, that is, because the GUILD_CREATE packet always come with at least one member: the client's (ref).

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
5 participants