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ā€¢16mo 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!
the
theOPā€¢16mo ago
nop pretty sure my code is just ass lol
Shaurya
Shauryaā€¢16mo ago
client.user is not always available
Deca
Decaā€¢16mo ago
I think I used interaction.user.
Shaurya
Shauryaā€¢16mo ago
use optional chaining
Deca
Decaā€¢16mo ago
Shaurya
Shauryaā€¢16mo ago
what? @_thethe this is the solution, you can do this
Deca
Decaā€¢16mo ago
oh sorry I think I misunderstood the question
the
theOPā€¢16mo ago
atleast it doesnt give me errors now šŸ™‚ the status still not changing šŸ¤”
Shaurya
Shauryaā€¢16mo ago
client.user is undefined, try logging the client and check what's logging
Deca
Decaā€¢16mo ago
You could also do something else if there's no client user found
the
theOPā€¢16mo ago
oh yeah uh its undefined šŸ¤”
Unknown User
Unknown Userā€¢16mo ago
Message Not Public
Sign In & Join Server To View
the
theOPā€¢16mo ago
lol
Unknown User
Unknown Userā€¢16mo ago
Message Not Public
Sign In & Join Server To View
Shaurya
Shauryaā€¢16mo ago
then there's an issue in your params I guess, yeah you can do that ^
Unknown User
Unknown Userā€¢16mo ago
Message Not Public
Sign In & Join Server To View
Deca
Decaā€¢16mo 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;
},
the
theOPā€¢16mo ago
yeah okay now its not undefined anymore prayge
Unknown User
Unknown Userā€¢16mo ago
Message Not Public
Sign In & Join Server To View
the
theOPā€¢16mo ago
ahhh now it works prayge
Deca
Decaā€¢16mo ago
yeah I think that makes sense thanks
Unknown User
Unknown Userā€¢16mo ago
Message Not Public
Sign In & Join Server To View
Deca
Decaā€¢16mo 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ā€¢16mo ago
Message Not Public
Sign In & Join Server To View
Deca
Decaā€¢16mo ago
I mean
interaction.client.guilds.cache.get(interaction.guildId);
interaction.client.guilds.cache.get(interaction.guildId);
Unknown User
Unknown Userā€¢16mo ago
Message Not Public
Sign In & Join Server To View
Deca
Decaā€¢16mo ago
yes šŸ‘
Want results from more Discord servers?
Add your server