Reputation: 147
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.
Upvotes: 0
Views: 2467
Reputation: 99
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!
Upvotes: 2