How to make a discord bot create an invite for every server it joins?

Actually, invites are not for the server, but for the channel.

  • Each channel in your server has it’s own Instant Invite link, and which ever channel you pull the link from is where the user will land on your server each time they try to access it.
  • For example, if you send Glados an invite link from your #rules channel, then every time she accesses your server the #rules chat channel will be the first channel she sees.

This code shows how you can create an invite to the first channel your bot finds (not necessarily the first in channel list):

guild.channels.cache.first()
.createInvite() //you can add {options} if you want
.then(invite => embed.addField("Invite link", invite.url, true)) // if promise is accepted
.catch(() => embed.addField("Invite link", "Missing permissions")) // if promise is rejected (90% because you don't have permissions, 10% because of server lag etc., either way you don't get the link)

Since creating an invite may be longer than sending an embed, I suggest adding some asynchronous action. Whole solution:

client.on("guildCreate", async (guild) => {
    let channel = new Discord.WebhookClient(
        client.config.webhook.id,
        client.config.webhook.token
    );
    const embed = new Discord.MessageEmbed()
        .setColor(client.config.embed.color)
        .setThumbnail(guild.iconURL({ dynamic: true }))
        .setTitle("New Server!")
        .addField("Server Name", guild.name, true)
        .addField("Server ID", guild.id, true)
        .addField("Owner ID", guild.ownerID, true)
        .addField("Owner Mention", `<@${guild.ownerID}>`, true)
        .addField("Member Count", guild.memberCount, true)
        .setFooter(client.user.username, client.config.embed.thumbnail);

        await guild.channels.cache
        .first()
        .createInvite()
        .then((invite) => embed.addField("Invite link", invite.url, true))
        .catch(() => embed.addField("Invite link", "Missing permissions", true));

    channel.send(embed);
});

Final tip:

You don’t need to check for owner ID, since it is easily accessible via his mention.

EDIT:

Replying to your comment, I did some debugging myself and I kept getting UKNOWN CHANNEL error. For some strange reason, creating an invite link to category channel does not work (despite documentation claiming otherwise).

New code snippet:

client.on("guildCreate", async (guild) => {
    let channel = new Discord.WebhookClient(
        client.config.webhook.id,
        client.config.webhook.token
    );
    const embed = new Discord.MessageEmbed()
        .setColor(client.config.embed.color)
        .setThumbnail(guild.iconURL({ dynamic: true }))
        .setTitle("New Server!")
        .addField("Server Name", guild.name, true)
        .addField("Server ID", guild.id, true)
        .addField("Owner ID", guild.ownerID, true)
        .addField("Owner Mention", `<@${guild.ownerID}>`, true)
        .addField("Member Count", guild.memberCount, true)
        .setFooter(client.user.username, client.config.embed.thumbnail);

        await guild.channels.cache
        .filter(channel => channel.type === "text") //added this line, should work like a charm
        .first()
        .createInvite()
        .then((invite) => embed.addField("Invite link", invite.url, true))
        .catch(() => embed.addField("Invite link", "Missing permissions", true));

    channel.send(embed);
});

Leave a Comment

tech