0
votes

I'm trying to create a Discord bot that'll create an invite to the first channel of a Guild when it's added to the aforementioned guild and sending it to the console.

My code (it doesn't work):

client.on("guildCreate", guild => {
    const channel = Array.from(guild.channels).sort((a, b) => a.calculatedPosition - b.calculatedPosition)[0];
    channel.createInvite({
            unique: true
        })
        .then(invite => {
            console.log(`Joined to: ${guild.name} Invite: https://discord.gg/${invite.code}`);

        })
});
1

1 Answers

0
votes
// Listeing to the guildCreate event.
client.on("guildCreate", guild => {
    // Filtering the channels to get only the text channels.
    const Channels = guild.channels.cache.filter(channel => channel.type == "text");

    // Creating an invite.
    Channels.first().createInvite({
        maxUses: 1,
        unique: true
    }).then(invite => {
        console.log(`[INVITE] I've created an invite for ${guild.id}:${guild.name} - ${invite.url}`);
    });
});