Automating an invite to a discord channel

Calvin Froedge
2 min readSep 27, 2020

I’m working on a membership site where a private Discord channel is part of the required feature set. I wanted to be able to generate a one time invite code for each user. This was surprisingly challenging.

The first step is registering a Discord server…this can be done from within the Discord app.

The second is creating a discord bot, assigning permissions, and adding the bot to the server. DiscordPY had a great step by step guid for this on their docs page:

Finally, I used discord.js to create a single method call for this interaction:

const Discord = require('discord.js');const client = new Discord.Client();const guildId = 'GUILD_ID_FROM_URL'; //First param from discord webconst channelId = 'CHANNEL_ID_FROM_URL'; //Second param from discord web
const getInviteLink = async ()=>{ const p = new Promise((resolve, reject)=>{ client.on('ready', async () => { try { const guild = await client.guilds.fetch(guildId); const channel = await guild.channels.cache.get(channelId); const invite = await channel.createInvite({ maxUses: 1 }); resolve(`${invite.code}`); } catch(e){ reject(e); } }); client.login('YOUR_DISCORD_BOT_TOKEN') }); return p;}getInviteLink().then(link => console.log(`${link}`))

Enjoy! You can use this method to generate a single use Discord link. Consider incorporating this into your sign up flow to generate a single unique user signup link at account creation.

Calvin Froedge

Software developer, investor, energy markets analyst.