Home > Mobile >  Discord bot: ReferenceError: moment is not defined
Discord bot: ReferenceError: moment is not defined

Time:09-28

I was coding my discord bot but when I try to use the "userinfo" command console gives me this error:

ReferenceError: moment is not defined,

here is my userinfo.js:

const { MessageEmbed } = require('discord.js');

module.exports = {
name: "userinfo",
aliases: ['ui'],
description: "Shows account stats",
execute(message, args, commandName, client, Discord) {
    const Target = message.mentions.users.first() || message.author;
    const Member = message.guild.members.cache.get(Target.id);

    const Response = new MessageEmbed()
    .setAuthor(`${Target.username}`, Target.displayAvatarURL({dynamic: true}))
    .setThumbnail(Target.displayAvatarURL({dynamic: true}))
    .setColor('NOT_QUITE_BLACK')
    .addField("User ID", `${Target.id}`, false)
    .addField("Roles", `${Member.roles.cache.map(r => r).join(' ').replace("@everyone", " ")}`)
    .addField("Server member since", `${moment(Member.joinedAt).format('MMMM Do YYYY, h:mm:ss a')}\n**-** ${moment(Member.joinedAt).startOf('day').fromNow()}`)
    .addField("Discord member since", `${moment(Member.createdAt).format('MMMM Do YYYY, h:mm:ss a')}\n**-** ${moment(Member.createdAt).startOf('day').fromNow()}`)
    message.reply({embeds: [Response]});
}

}

I tried to do npm install moment --save in console but nothing changes

CodePudding user response:

Just like discord.js, you will also have to require moment to use it:

const moment = require('moment');

Note that Moment.js is no longer in active development, see here. You may want to consider using another library such as Luxon.

CodePudding user response:

It's because you're not pulling moment into your script. You need to add something like

const moment = require('moment');

below

const { MessageEmbed } = require('discord.js');
  • Related