Install discord-inviter
$ npm install discord-inviter
Install discord.js
$ npm install discord.js
- Simple & easy to use 🎗️
- Support vanity urls 🔗
- Returns full object with invite data 📡
At first install the discord-inviter package
const { Client, Intents } = require("discord.js"); // npm i discord.js
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_INVITES,
],
});
var { inviteTracker } = require("discord-inviter"), // npm i discord-inviter
tracker = new inviteTracker(client);
client.on("ready", () => console.log("Bot Ready"));
// "guildMemberAdd" event to get full invite data
tracker.on("guildMemberAdd", async (member, inviter, invite, error) => {
// return when get error
if(error) return console.error(error);
// get the channel
let channel = member.guild.channels.cache.get("939210311276306455"),
Msg = `Welcome ${member.user}, invited by <@!${inviter.id}>, code ${invite.code}, invite count ${invite.count}`;
// change welcome message when the member is bot
if (member.user.bot)
Msg = `Welcome ${member.user}, invited by <@!${inviter.id}>`;
// send welcome message
channel.send(Msg);
});
// "error" event to get any error
tracker.on("error", (guild, err) => {
console.error(guild?.name, err);
});
client.on("messageCreate", async (message) => {
// get member invites count
if (message.content == "invites") {
var invite = await inviteTracker.getMemberInvites(message.member);
message.channel.send(
`${message.author.tag} has ${invite.count} invite(s).`
);
// get server top invites
} else if (message.content == "top-invites") {
var top = await inviteTracker.getTopInvites(message.guild);
message.channel.send(
top
.map((i, n) => `\`#${n + 1}\`- **${i.user.tag}** has __${i.count}__`)
.join("\n")
);
// get info of any invite code
} else if (message.content == "invite-info") {
var invite = await inviteTracker.getInfo(client, "VwTxJaqjsJ");
if (!invite) return;
message.channel.send(
`Guild: ${invite.guild.name},\nInviter: ${
invite?.inviter
? `${invite.inviter.tag}`
: "Owner"
},\nLink: ${invite.url}`
);
}
});