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

Did you find this page helpful?