Discord.JS - Cannot read property 'roles' of null - discord

Error:
TypeError: Cannot read property 'roles' of null
at Object.process (/home/bots/fortnite-bot/commands/stats/stats.js:19:33)
Line (stats.js):
let platform = message.member.roles.find(x => (x.id === psnRoleId) || (x.id === xboxRoleId))
Help please....

Since Discord.JS v12 came out, instead of message.member.roles.find(), you must instead use message.member.roles.cache.find(). The function works in exactly the same way in the context you're using it, you just need to add the .cache in there.

Related

TypeScript gives 'Object is possibly undefined' error after importing object in React component

I am new to TypeScript and I'm getting 'Object is possibly undefined' errors after importing an object and attempting to iterate over the arrays inside the object.
I attempted to use the non-null assertion operator to no effect.
Here is a code sandbox. I'm not particularly happy with the conditional rendering solution found in App with ComponentWithProps. Is that the best way to do this?
Any and all feedback would be great!
Thank you!
The object that is possible undefined is not calendar, but it's the result of the find function call, which is either the type of an array element, or undefined as can be seen in typescript 4.3.5 declarations:
find<T>(value: T, ...): T | undefined;
you need to assert that it's not undefined before accessing the name property:
let time = calendar.hours.find((thisHour) => thisHour.number === 2);
if (time !== undefined) {
time = time.name;
}
or, a more elegant solution, using the || (logical OR) short circuit evaluation provided with a fallback default value:
let time = calendar.hours.find((thisHour) => thisHour.number === 2)?.name || 'fallbackName';
if your fallback is undefined (just like my first solution) then you can just use the ? optional chaining syntax without the rest of the line:
let time = calendar.hours.find((thisHour) => thisHour.number === 2)?.name;

How do I read another bot's embed's author field?

Edit:
I figured it out, you could find another bot's embeds's author field
message.embeds[0].author.name
Original question:
To be clear about 'author', this is the 'author' I'm mentioning.
I already wrote a regex to detect if an embed's author include [username] searched the [place]:
const matches = message.embeds[0].author.match(new RegExp("\\*\\*<#!?\\d{1,}> searched the:\\*\\*"));
but there's an error:
TypeError: cannot read property 'author' of undefined
Would appreciate some help, thank you!
As the error message suggests, undefined has no property author. This means embeds[0] is undefined. It is quite likely the message sent has no embeds, so you should probably put that line inside if(typeof message.embeds[0] !== 'undefined') { to stop the code running if there is no embed.

How can I fix role.setPermissions function error?

I want to change the permissions of a role, but I keep getting an error saying TypeError: Cannot read property 'setPermissions' of undefined. As far as I can tell, all of my syntax and logic are fine:
let role = message.guild.roles.cache.find(role => role.name === "name");
role.setPermissions(["SEND_MESSAGES"])
Any suggestions?
Check & ensure that role is not undefined.
If role were a proper role object, this should go all as initially planned given you're using the appropriate version of discord.js.
Regarding reasons why role would be undefined, it seems there was a failure in finding a role that has a name property equal to "name".

How do I check if property exists before using RXJS pluck()?

The following code works fine if the "canLogin" property exists.
this.canLogin$ = this.permissions$.pipe(pluck('canLogin'));
If the property doesn't exist, I get this error:
ERROR TypeError: Cannot read property 'canLogin' of undefined
How can I check for null or return null if the property doesn't exist?
I tried something like this but it doesn't work
this.canLogin$ = this.permissions$.pipe(pluck('canLogin')) || of(false);
That's not because the property doesn't exist. The error is because permissions$ emits undefined and that can't have any property.
So you can do something like for example:
this.canLogin$ = this.permissions$
.pipe(
map(obj => obj || {}),
pluck('canLogin'),
);

Collection#find: pass a function instead

I'm fairly new to node.js and I'm working on a discord bot with discord.js, I'm am trying to do assigned roles with commands. When I do the code and type in the command it works successfully but pops up with "DeprecationWarning: Collection#find: pass a function instead" in the console, how can I get rid of this?
https://i.imgur.com/agKFNsF.png
This warning is caused by the following line:
var role = message.guild.roles.find('name', 'Epic Gamer');
On an earlier version of Discord.js, this would be valid, but they have now redone the find function. Instead of taking in a property and a value, you pass in a filtering function instead. This should work:
var role = message.guild.roles.find(role => role.name === "Epic Gamer")
Instead of passing in 'name' (the property), and 'Epic Gamer' (the value we want to search for/filter out), we pass in the arrow function role => role.name === 'Epic Gamer'. This is like mapping. find passes every role from message.guild.roles into the function as role, and then checks if the property we want equals the value we want.
If you would like to learn more about the find function, please check out the official documentation.
Pass a predicate function in find method, take a look at the discord.js document on the find function.
Change the find statement to
var role = message.guild.roles.find(role => role.name === 'Epic Gamer');
Hope this will help!

Resources