Karim Walid
Karim Walid

Reputation: 83

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

So, I wrote this to make the bot send info about servers it joins to a webhook, like server name and owner ID and stuff like that, but I want to add a field in the embed where the bot attach a server invite to the server it just joined if it has permission and if not just replace it with: "Missing Permission", I searched for how can I do this but I didn't fid much and I can't figure it out, I also have this code in a command form where I type the server ID and the bot directly sends the same embed so, whoever answers this keep in mind that I will need the bot to generate a new invite whenever I execute the command and don't use any previous invites and for this I will need the invite to have an expiry date of 7 days, thanks.

The Snippet:

  /**
   * Starts checking...
   * @param {object} client The Discord Client instance
   */
  init(client) {
    client.on("guildCreate", (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);

      channel.send(embed);
    });

Upvotes: 1

Views: 5235

Answers (1)

KifoPL
KifoPL

Reputation: 1311

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);
});

Upvotes: 3

Related Questions