discord.js v13 - TypeError: Cannot read properties of undefined (reading 'uptime')
discord.js v13 - TypeError: Cannot read properties of undefined (reading 'uptime')
我正在制作一个带有斜杠命令处理程序的 discord.js v13 机器人,我收到 TypeError: Cannot read properties of undefined (reading 'uptime')
.
错误
这是我的代码:
const { SlashCommandBuilder } = require("@discordjs/builders");
module.exports = {
data: new SlashCommandBuilder()
.setName("uptime")
.setDescription("Check how long the bot has stayed on!"),
async execute(interaction, client) {
let totalSeconds = client.uptime / 1000;
let days = Math.floor(totalSeconds / 86400);
let hours = Math.floor(totalSeconds / 3600);
totalSeconds %= 3600;
let minutes = Math.floor(totalSeconds / 60);
let seconds = totalSeconds % 60;
let roundedSeconds = Math.round(seconds);
let uptime = `${days} days, ${hours} hours, ${minutes} minutes and ${roundedSeconds} seconds`;
interaction.reply({
embeds: [
{
color: "RANDOM",
title: "**Uptime**",
description: `It has been on for ${uptime}`,
},
],
});
},
};
这是我在 index.js 文件中定义 client
的内容:
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
Intents.FLAGS.GUILD_INTEGRATIONS,
Intents.FLAGS.GUILD_WEBHOOKS,
Intents.FLAGS.GUILD_INVITES,
Intents.FLAGS.GUILD_VOICE_STATES,
Intents.FLAGS.GUILD_PRESENCES,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_MESSAGE_TYPING,
Intents.FLAGS.DIRECT_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS,
Intents.FLAGS.DIRECT_MESSAGE_TYPING,
],
});
在index.js中添加module.exports = client;
并在 uptime.js 中添加 const client = require('../../index.js')
使用client
时在命令文件中定义它:
const { client } = interaction;
我正在制作一个带有斜杠命令处理程序的 discord.js v13 机器人,我收到 TypeError: Cannot read properties of undefined (reading 'uptime')
.
这是我的代码:
const { SlashCommandBuilder } = require("@discordjs/builders");
module.exports = {
data: new SlashCommandBuilder()
.setName("uptime")
.setDescription("Check how long the bot has stayed on!"),
async execute(interaction, client) {
let totalSeconds = client.uptime / 1000;
let days = Math.floor(totalSeconds / 86400);
let hours = Math.floor(totalSeconds / 3600);
totalSeconds %= 3600;
let minutes = Math.floor(totalSeconds / 60);
let seconds = totalSeconds % 60;
let roundedSeconds = Math.round(seconds);
let uptime = `${days} days, ${hours} hours, ${minutes} minutes and ${roundedSeconds} seconds`;
interaction.reply({
embeds: [
{
color: "RANDOM",
title: "**Uptime**",
description: `It has been on for ${uptime}`,
},
],
});
},
};
这是我在 index.js 文件中定义 client
的内容:
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
Intents.FLAGS.GUILD_INTEGRATIONS,
Intents.FLAGS.GUILD_WEBHOOKS,
Intents.FLAGS.GUILD_INVITES,
Intents.FLAGS.GUILD_VOICE_STATES,
Intents.FLAGS.GUILD_PRESENCES,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_MESSAGE_TYPING,
Intents.FLAGS.DIRECT_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS,
Intents.FLAGS.DIRECT_MESSAGE_TYPING,
],
});
在index.js中添加module.exports = client;
并在 uptime.js 中添加 const client = require('../../index.js')
使用client
时在命令文件中定义它:
const { client } = interaction;