Skip to content
Advertisement

Invite created event (discord.js v12)

I am trying to send an embed whenever a invite is created.

Channel set file

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

module.exports = {
  name: "setinvite",
  description: "set invite channel log.",
  async execute(message, args) {
    if (!message.member.hasPermission(`ADMINISTRATOR`)) {
      return message.channel.send(
        `:x: You do not have permission to use this command!`
      );
    } else {
      let channelx =
        message.mentions.channels.first() ||
        message.guild.channels.cache.find((c) => c.id === args[0]);
      if (!channelx)
        return message.channel.send(
          `:x: Please specify a channel to make it as the modlogs!`
        );

      message.channel.send(`${channelx} has been set!`);
    }
  },
};

Index.js Modules (PS: I took the most relevant ones.)

const Discord = require("discord.js");
const client = new Discord.Client();
const fs = require("fs");
const { MessageEmbed } = require("discord.js");
const guildInvites = new Map();
const { channelx } = require("./commands/setinvite");

Index.js file

client.on("inviteCreate, message", async (invite) => {
  const setc = client.channels.cache.get(`${channelx}`);
  message.guild.fetchInvites().then((invites) => {
    let allInvites = invites.map((i) => ({
      name: "Invite",
      value: `**Inviter:** ${i.inviter}
    **Code:** https://discord.gg/${i.code}
    **Usages:** ${i.uses} of ${i.maxUses === 0 ? "∞" : i.maxUses}
    **Expires on:** ${
      i.maxAge
        ? new Date(i.createdTimestamp + i.maxAge * 1000).toLocaleString()
        : "never"
    }`,
      inline: true,
    }));

    setc.send(new Discord.MessageEmbed().addFields(allInvites));
  });
});


I don’t think the two events (inviteCreate, message) belong I did it because I received a error:

ReferenceError: message is not defined

Now, the channel set features works as intended but whenever the invite is created the embed doesn’t send.

Advertisement

Answer

You can’t merge all events inside one function.

You only need to keep the inviteCreate event. Then, you have to find a way to get the guild without using the “message” variable. Instead you can use the “invite” parameter that is present inside the inviteCreate event.

client.on("inviteCreate", async (invite) => {
  const setc = client.channels.cache.get(`${channelx}`);
  invite.guild.fetchInvites().then((invites) => {
    let allInvites = invites.map((i) => ({
      name: "Invite",
      value: `**Inviter:** ${i.inviter}
    **Code:** https://discord.gg/${i.code}
    **Usages:** ${i.uses} of ${i.maxUses === 0 ? "∞" : i.maxUses}
    **Expires on:** ${
      i.maxAge
        ? new Date(i.createdTimestamp + i.maxAge * 1000).toLocaleString()
        : "never"
    }`,
      inline: true,
    }));

    setc.send(new Discord.MessageEmbed().addFields(allInvites));
  });
});
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement