'client.user' is possible null

The error that VS Code gives me is in the title.
const client: Client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
],
});

function setActivity() {
client.user.setPresence({
activities: [
{
name: "SOME TEXT",
type: ActivityType.Playing,
},
],
status: "online",
});
}
const client: Client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
],
});

function setActivity() {
client.user.setPresence({
activities: [
{
name: "SOME TEXT",
type: ActivityType.Playing,
},
],
status: "online",
});
}
Can someone help me? I'm a newbie in TypeScript btw. [email protected] nodejs v18.15.0
18 Replies
d.js toolkit
d.js toolkit2y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Turboman3000
Turboman3000OP2y ago
Like
const client: Client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
],
});

client.login(dev ? config.devToken : config.token);

function setActivity() {
client.user.setPresence({
activities: [
{
name: "SOME TEXT",
type: ActivityType.Playing,
},
],
status: "online",
});
}
const client: Client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
],
});

client.login(dev ? config.devToken : config.token);

function setActivity() {
client.user.setPresence({
activities: [
{
name: "SOME TEXT",
type: ActivityType.Playing,
},
],
status: "online",
});
}
I'm Calling the setActivity() function in the ClientReady event btw. I'm getting the same error
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mac
Mac2y ago
Yeah but client is not defined inside that function. Console log client when u call the function
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs2y ago
method Client#isReady() Returns whether the client has logged in, indicative of being able to access properties such as user and application.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Turboman3000
Turboman3000OP2y ago
bruh
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Turboman3000
Turboman3000OP2y ago
Their is some more code thats private for getting the Presence of an Json file because of this i have to use it in a function
function setActivity() {
const cj = "SOME TEXT";
if(!client.isReady())

client.user.setPresence({
activities: [
{
name: cj,
type: ActivityType.Playing,
},
],
status: "online",
});

setTimeout(() => {
setActivity();
}, 60 * 1000 * 1);
}
function setActivity() {
const cj = "SOME TEXT";
if(!client.isReady())

client.user.setPresence({
activities: [
{
name: cj,
type: ActivityType.Playing,
},
],
status: "online",
});

setTimeout(() => {
setActivity();
}, 60 * 1000 * 1);
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Turboman3000
Turboman3000OP2y ago
It's just an example
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Turboman3000
Turboman3000OP2y ago
Their is normally an json array
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Turboman3000
Turboman3000OP2y ago
return? oh right I missed it xD But now it works fine. I didnt know that ts and VS Code will look at stuff like this
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server