Search code examples
javascriptdiscorddiscord.js

is there any way to generate invite links of the channels where my discord.js bot is in?


but I have a serious problem that I had a Discord channel with my friend and my Discord.js bot like bellow. But I left the channel and my friend is dead so Im unable to join the sever anymore.

const { Client, Intents } = require('discord.js')
const { prefix, token } = require("./config.json");
const ytdl = require("ytdl-core");

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]})
  
client.on('ready', () => {
    console.log(client.user.tag + "I came home!")
})


client.on('message', message => {
    // some code for message commands
   
})


client.login(token);

So I want to ask that is there anyway to make my Discord.js bot to generate an invite link of the sever it is in? Because I dont have any knowledge about Discord.js and it's really rushed so please help me with the code.

Thank you everyone for reading.


Solution

  • You will have to set that in your client.on('ready') :

    client.on("ready", async() => {
        const cache = await client.channels.cache;
        const channels = [...cache.keys()];
        const channel = await client.channels.fetch(channels[channels.length - 1]); // that depends of the differents kinds of channels, if this isnt working, try different indexes
        let invite = await channel.createInvite({
          maxAge: 10 * 60 * 1000,
          maxUses: 10,
        });
        console.log(`discord.gg/${invite.code}`)
    });
    

    This will create a new invitation. Since you are not on the server, the bot will not be able to send it to you. console.log(discord.gg/${invite.code}) is here so you can copy and paste it into your browser. Then you will be able to join the server.

    Have a good day!