user undefined

I am encounting following error: TypeError: Cannot read properties of undefined (reading 'user') Here is the code:
const { SlashCommandBuilder } = require("discord.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("status")
.setDescription("Sets the status of the bot")
.addStringOption((option) =>
option
.setName("type")
.setDescription("Choose which one you want it to be")
.setRequired(true)
.addChoices(
{ name: "Online", value: "online" },
{ name: "Idle", value: "idle" },
{ name: "Do not disturb", value: "dnd" },
{ name: "Invisible", value: "invisible" }
)
),
async execute(interaction, client) {
const { options } = interaction;
const type = options.getString("type");

try {
await client.user.setPresence({ status: type });
} catch (error) {
console.log(error);
}
},
};
const { SlashCommandBuilder } = require("discord.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("status")
.setDescription("Sets the status of the bot")
.addStringOption((option) =>
option
.setName("type")
.setDescription("Choose which one you want it to be")
.setRequired(true)
.addChoices(
{ name: "Online", value: "online" },
{ name: "Idle", value: "idle" },
{ name: "Do not disturb", value: "dnd" },
{ name: "Invisible", value: "invisible" }
)
),
async execute(interaction, client) {
const { options } = interaction;
const type = options.getString("type");

try {
await client.user.setPresence({ status: type });
} catch (error) {
console.log(error);
}
},
};
Any Ideas why?
28 Replies
d.js toolkit
d.js toolkitā€¢17mo 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. - Issue solved? Press the button!
Dokter
DokterOPā€¢17mo ago
nop pretty sure my code is just ass lol
Shaurya
Shauryaā€¢17mo ago
client.user is not always available
Deca
Decaā€¢17mo ago
I think I used interaction.user.
Shaurya
Shauryaā€¢17mo ago
use optional chaining
Deca
Decaā€¢17mo ago
Shaurya
Shauryaā€¢17mo ago
what? @_thethe this is the solution, you can do this
Deca
Decaā€¢17mo ago
oh sorry I think I misunderstood the question
Dokter
DokterOPā€¢17mo ago
atleast it doesnt give me errors now šŸ™‚ the status still not changing šŸ¤”
Shaurya
Shauryaā€¢17mo ago
client.user is undefined, try logging the client and check what's logging
Deca
Decaā€¢17mo ago
You could also do something else if there's no client user found
Dokter
DokterOPā€¢17mo ago
oh yeah uh its undefined šŸ¤”
Unknown User
Unknown Userā€¢17mo ago
Message Not Public
Sign In & Join Server To View
Dokter
DokterOPā€¢17mo ago
lol
Unknown User
Unknown Userā€¢17mo ago
Message Not Public
Sign In & Join Server To View
Shaurya
Shauryaā€¢17mo ago
then there's an issue in your params I guess, yeah you can do that ^
Unknown User
Unknown Userā€¢17mo ago
Message Not Public
Sign In & Join Server To View
Deca
Decaā€¢17mo ago
Not related but Can I do something like this when some of functions are not in interaction?
getClient: () => {
return client;
},
getUser: (guildId: string, userId: string) => {
let guild = client.guilds.cache.get(guildId);
let member = guild?.members.cache.get(userId);

return member;
},
getClient: () => {
return client;
},
getUser: (guildId: string, userId: string) => {
let guild = client.guilds.cache.get(guildId);
let member = guild?.members.cache.get(userId);

return member;
},
Dokter
DokterOPā€¢17mo ago
yeah okay now its not undefined anymore prayge
Unknown User
Unknown Userā€¢17mo ago
Message Not Public
Sign In & Join Server To View
Dokter
DokterOPā€¢17mo ago
ahhh now it works prayge
Deca
Decaā€¢17mo ago
yeah I think that makes sense thanks
Unknown User
Unknown Userā€¢17mo ago
Message Not Public
Sign In & Join Server To View
Deca
Decaā€¢17mo ago
yeah but I believe this works with interaction.client too.
// It's a function declared in the same file where the `client` is
getUser: (guildId: string, userId: string) => {
let guild = client.guilds.cache.get(guildId);
let member = guild?.members.cache.get(userId);

return member;
},
// It's a function declared in the same file where the `client` is
getUser: (guildId: string, userId: string) => {
let guild = client.guilds.cache.get(guildId);
let member = guild?.members.cache.get(userId);

return member;
},
Unknown User
Unknown Userā€¢17mo ago
Message Not Public
Sign In & Join Server To View
Deca
Decaā€¢17mo ago
I mean
interaction.client.guilds.cache.get(interaction.guildId);
interaction.client.guilds.cache.get(interaction.guildId);
Unknown User
Unknown Userā€¢17mo ago
Message Not Public
Sign In & Join Server To View
Deca
Decaā€¢17mo ago
yes šŸ‘
Want results from more Discord servers?
Add your server