discorddiscord.jsinvite

Log when an invite is created (discord.js)


I am trying to log whenever a invite is created and want this to apply per guild (meaning that whenever a invite is created in guild a, it logs it. If a guild is created in guild b, it only logs it in that server.)

I am having a issue,

Issue: ReferenceError: maxAge is not defined

Here is my code:

const Discord = require("discord.js");
let config = require("../config.json");
const { MessageEmbed } = require("discord.js");
const client = new Discord.Client();

module.exports = {
        name: `printinvs`,
        description: `Prints invites created.`,
        async execute(message, args){


                message.guild.fetchInvites()
                .then   
               
                (invites => {

                        const printinvites = new Discord.MessageEmbed()
                        .setTitle(`Invite Information`)
                        .addField(`Max Age: ${maxAge}`)
                        .addField(`Max Uses: ${maxUses}`)
                        .addField(`Temporary: ${temporary}`)
                        .addField(`Invite URL: ${inviteurl}`)
                        .addField(`Invite created by: ${inviter}`)
                        .addField(`Channel: ${invite.channel}`)
                        .addField(`Time invited created: ${invite.createdAT}`)
                        .setColor(`RANDOM`)
        
                        message.client.channels.cache.get(channelid).send(printinvites);
                })

                

                

        }
}

Solution

  • This error is occuring because you never declared the latest invite, and you're using its properties without the actual invite.

    Try this code:

    let config = require("../config.json");
    const { MessageEmbed } = require("discord.js");
    const client = new Discord.Client();
    
    module.exports = {
            name: `printinvs`,
            description: `Prints invites created.`,
            async execute(message, args){
    
    
                    message.guild.fetchInvites()
                    .then   
                   
                    (invites => {
                            let latestInvite = invites.last();
                             
                             const printinvites = new Discord.MessageEmbed()
                            .setTitle(`Invite Information`)
                            .addField(`Max Age: ${latestInvite.maxAge}`)
                            .addField(`Max Uses: ${latestInvite.maxUses}`)
                            .addField(`Temporary: ${latestInvite.temporary}`)
                            .addField(`Invite URL: ${latestInvite.url}`)
                            .addField(`Invite created by: ${latestInvite.inviter}`)
                            .addField(`Channel: ${latestInvite.channel}`)
                            .addField(`Time invited created: ${latestInvite.createdAt}`)
                            .setColor(`RANDOM`)
            
                            message.client.channels.cache.get(channelid).send(printinvites);
                    })
    
                    
    
                    
    
            }
    }