uptime command

Hello i try to get a uptime command working but it gives error that uptime is undefinided
10 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Basgamer999
Basgamer999OP3y ago
const { SlashCommandBuilder } = require('discord.js');
const { client } = require('discord.js')

module.exports = {
data: new SlashCommandBuilder()
.setName('uptime')
.setDescription('Showing the uptime of the bot'),
async execute(interaction) {
let uptime = client.uptime()
await interaction.reply(uptime);
},
};
const { SlashCommandBuilder } = require('discord.js');
const { client } = require('discord.js')

module.exports = {
data: new SlashCommandBuilder()
.setName('uptime')
.setDescription('Showing the uptime of the bot'),
async execute(interaction) {
let uptime = client.uptime()
await interaction.reply(uptime);
},
};
this is my code o i run discord.js 14.5.0 instead of 14.6 node versionv16.17.1
TypeError: Cannot read properties of undefined (reading 'uptime')
at Object.execute (C:\Users\Gamegebak\Documents\Code\Bot\commands\uptime.js:9:29)
at Client.<anonymous> (C:\Users\Gamegebak\Documents\Code\Bot\index.js:39:17)
at Client.emit (node:events:525:35)
at InteractionCreateAction.handle (C:\Users\Gamegebak\Documents\Code\Bot\node_modules\discord.js\src\client\actions\InteractionCreate.js:81:12)
at Object.module.exports [as INTERACTION_CREATE] (C:\Users\Gamegebak\Documents\Code\Bot\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (C:\Users\Gamegebak\Documents\Code\Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31)
at WebSocketShard.onPacket (C:\Users\Gamegebak\Documents\Code\Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:481:22)
at WebSocketShard.onMessage (C:\Users\Gamegebak\Documents\Code\Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:321:10)
at WebSocket.onMessage (C:\Users\Gamegebak\Documents\Code\Bot\node_modules\ws\lib\event-target.js:199:18)
at WebSocket.emit (node:events:513:28)
TypeError: Cannot read properties of undefined (reading 'uptime')
at Object.execute (C:\Users\Gamegebak\Documents\Code\Bot\commands\uptime.js:9:29)
at Client.<anonymous> (C:\Users\Gamegebak\Documents\Code\Bot\index.js:39:17)
at Client.emit (node:events:525:35)
at InteractionCreateAction.handle (C:\Users\Gamegebak\Documents\Code\Bot\node_modules\discord.js\src\client\actions\InteractionCreate.js:81:12)
at Object.module.exports [as INTERACTION_CREATE] (C:\Users\Gamegebak\Documents\Code\Bot\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (C:\Users\Gamegebak\Documents\Code\Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31)
at WebSocketShard.onPacket (C:\Users\Gamegebak\Documents\Code\Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:481:22)
at WebSocketShard.onMessage (C:\Users\Gamegebak\Documents\Code\Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:321:10)
at WebSocket.onMessage (C:\Users\Gamegebak\Documents\Code\Bot\node_modules\ws\lib\event-target.js:199:18)
at WebSocket.emit (node:events:513:28)
i try to make a /uptime command
CanineData
CanineData3y ago
discord js doesn't export anything called 'client', you need to get the client from either interaction or have it as an argument in your execute function
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Basgamer999
Basgamer999OP3y ago
Huh? I am new to discord bot coding in JS. Any examples somewhere?
probablyraging
this let uptime = client.uptime() should be let uptime = interaction.client.uptime
Basgamer999
Basgamer999OP3y ago
thx
const { SlashCommandBuilder } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('uptime')
.setDescription('Showing the uptime of the bot'),
async execute(interaction) {
let uptime = interaction.client.uptime()
await interaction.reply(uptime);
},
};
const { SlashCommandBuilder } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('uptime')
.setDescription('Showing the uptime of the bot'),
async execute(interaction) {
let uptime = interaction.client.uptime()
await interaction.reply(uptime);
},
};
I tried that but its still not working or do i need to put it somewhere else then after execute?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Basgamer999
Basgamer999OP3y ago
o oops thx. How would i decode this to human readable time?
d.js docs
d.js docs3y ago
Documentation suggestion for @basgamer999:mdn Date.prototype.toLocaleString() The toLocaleString() method returns a string with a language-sensitive representation of this date. In implementations with Intl.DateTimeFormat API support, this method simply calls Intl.DateTimeFormat.
Want results from more Discord servers?
Add your server