簡體   English   中英

discord.js v13 - 類型錯誤:無法讀取未定義的屬性(讀取“正常運行時間”)

[英]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,
  ],
});

使用client時在命令文件中定義它:

const { client } = interaction;

在 index.js 添加module.exports = client;

並在 uptime.js 中添加const client = require('../../index.js')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM