首页 > 解决方案 > TypeError:无法读取未定义的属性(读取“createdTimestamp”)

问题描述

我正在尝试为我的不和谐机器人创建一个 ping 命令。我的代码看起来很简单:

index.js:

require("dotenv").config();
const { Client, Intents, Collection } = require("discord.js");
const client = new Client({
  intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
});

const fs = require("fs");
client.commands = new Collection();
const commandFiles = fs
  .readdirSync("./commands")
  .filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
  const command = require(`./commands/${file}`);
  client.commands.set(command.data.name, command);
}

const eventFiles = fs
  .readdirSync("./events")
  .filter((file) => file.endsWith(".js"));

for (const file of eventFiles) {
  const event = require(`./events/${file}`);
  if (event.once) {
    client.once(event.name, (...args) => event.execute(...args, client));
  } else {
    client.on(event.name, (...args) => event.execute(...args, client));
  }
}

client.on("interactionCreate", (interaction) => {
  console.log(interaction);
});

client.login(process.env.TOKEN);

消息创建.js:

require("dotenv").config();

module.exports = {
  name: "messageCreate",
  on: true,
  async execute(msg, client) {
    // If message author is a bot, or the message doesn't start with the prefix, return.
    if (msg.author.bot || !msg.content.startsWith(process.env.PREFIX)) return;

    var command = msg.content.substring(1).split(" ")[0].toLowerCase();

    // Remove the command from the args
    var args = msg.content.substring().split(/(?<=^\S+)\s/)[1];

    if (!client.commands.has(command)) return;

    try {
      await client.commands.get(command).execute(msg, args, client);
    } catch (error) {
      console.error(error);
      await msg.reply({
        content: "Error: Please check console for error(s)",
        ephemeral: true,
      });
    }
  },
};

平.js:

const { SlashCommandBuilder } = require("@discordjs/builders");
const { MessageEmbed } = require("discord.js");

module.exports = {
  data: new SlashCommandBuilder()
    .setName("ping")
    .setDescription("Replies to ping with pong"),
  async execute(msg, args, client, interaction) {
    const embed = new MessageEmbed()
      .setColor("#0099ff")
      .setTitle(" Pong!")
      .setDescription(
        `Latency is ${
          Date.now() - msg.createdTimestamp
        }ms. API Latency is ${Math.round(client.ws.ping)}ms`
      )
      .setTimestamp();
    await interaction.reply({
      embeds: [embed],
      ephemeral: true,
    });
  },
};

我正在传递我的 msg 参数,为什么它无法识别 ping.js 中的 msg.createdTimestamp?编辑:我更新了一些代码,更新了参数传递的方式。现在我的TypeError: Cannot read properties of undefined (reading 'reply')ping.js 文件出现错误。

标签: javascriptdiscord.js

解决方案


所以我想通了。msg我传递的部分实际上被传递给了interaction论点。只需更改msginteraction即可使一切正常工作:

ping.js

const { SlashCommandBuilder } = require("@discordjs/builders");
const { MessageEmbed } = require("discord.js");

module.exports = {
  data: new SlashCommandBuilder()
    .setName("ping")
    .setDescription("Replies to ping with pong"),
  async execute(interaction, args, client) {
    const embed = new MessageEmbed()
      .setColor("#0099ff")
      .setTitle(" Pong!")
      .setDescription(
        `Latency is ${
          Date.now() - interaction.createdTimestamp
        }ms. API Latency is ${Math.round(client.ws.ping)}ms`
      )
      .setTimestamp();
    await interaction.reply({
      embeds: [embed],
      ephemeral: true,
    });
  },
};


推荐阅读