How to create a specific amount of invite links that can only be used once for a Discord server in Python
Question:
I am new to working with Discord servers and I would like to make a private Discord server where only users that I invite can join. I read about a few ways that this can be achieved, but none of them are really what I have in mind. I was thinking about creating a Discord application that generated a specific amount of invite links to my server which can only be used once.
This means that if I want to invite 50 people to my Discord server, I would create 50 invite links that can only be used once so that I make sure that only the people I invite will join. I would like to put all of these links in an external text file so that I will later be able to work with them and eventually send them to people by email. In other words, I don’t need to create a bot, but rather just use Python and the discord.py module to achieve all this outside of Discord.
I saw this on the discord.py documentation which looks something like what I need, but I don’t really understand how that would work.
I can almost only find tutorials on how to create bots, on the Discord server itself, but that is not what I need. Would anyone be able to help me out?
Thank you very much in advance!
Answers:
import discord
token = 'bot_token_goes_here'
client = discord.Client()
number_of_links = input('How many links do you want to create? ')
@client.event
async def on_ready():
g = client.guilds[guild_number goes here] # Choose the guild/server you want to use
c = g.get_channel(channel_id_goes_here) # Get channel ID
invites = await discord.abc.GuildChannel.invites(c) # list of all the invites in the server
while len(invites) < int(number_of_links):
print('CREATING INVITES')
for i in range(int(number_of_links)): # Create as many links as needed
i = await discord.abc.GuildChannel.create_invite(c, max_uses=1, max_age=0, unique=True) # Create the invite link
break
print('Finished. Exiting soon...')
exit()
client.run(token)
I made some quick modifications to FedeCuci’s script, with the following differences:
- Script asks for the total number of new invites you want, rather than depending on previously created invites
- Script outputs the new invites to console
- Does not throw an exception
- Compatible with new intents API
- Some additional debugging output
For either script, you will need to install discord.py, then run the script with python3
. Don’t forget to update the token & ID’s as needed, and setup/join the bot to your target server via Discord’s developer portal. It will need permissions for Create Instant Invite
and Read Messages/View Channels
from discord.utils import get
import discord
token = 'bot-token-goes-here' # <-- fill this in
intents = discord.Intents.default()
intents.invites = True
client = discord.Client(intents=intents)
guild_id = guild-id-goes-here # <-- fill this in
number_of_links = input('How many links do you want to create? ')
@client.event
async def on_ready():
print('Bot is up and running.')
print(f'Logged in as {client.user}')
g = client.get_guild(int(guild_id))
print(f'Guild: {g.name}')
c = g.get_channel(channel-id-goes-here) # <-- fill this in
print(f'Channel: #{c}')
invites = []
print('CREATING INVITES')
for i in range(int(number_of_links)):
invite = await g.text_channels[0].create_invite(max_uses=1, max_age=0, unique=True)
print(f'{invite}')
print('Finished. Exiting soon...')
await client.close()
client.run(token)
I am new to working with Discord servers and I would like to make a private Discord server where only users that I invite can join. I read about a few ways that this can be achieved, but none of them are really what I have in mind. I was thinking about creating a Discord application that generated a specific amount of invite links to my server which can only be used once.
This means that if I want to invite 50 people to my Discord server, I would create 50 invite links that can only be used once so that I make sure that only the people I invite will join. I would like to put all of these links in an external text file so that I will later be able to work with them and eventually send them to people by email. In other words, I don’t need to create a bot, but rather just use Python and the discord.py module to achieve all this outside of Discord.
I saw this on the discord.py documentation which looks something like what I need, but I don’t really understand how that would work.
I can almost only find tutorials on how to create bots, on the Discord server itself, but that is not what I need. Would anyone be able to help me out?
Thank you very much in advance!
import discord
token = 'bot_token_goes_here'
client = discord.Client()
number_of_links = input('How many links do you want to create? ')
@client.event
async def on_ready():
g = client.guilds[guild_number goes here] # Choose the guild/server you want to use
c = g.get_channel(channel_id_goes_here) # Get channel ID
invites = await discord.abc.GuildChannel.invites(c) # list of all the invites in the server
while len(invites) < int(number_of_links):
print('CREATING INVITES')
for i in range(int(number_of_links)): # Create as many links as needed
i = await discord.abc.GuildChannel.create_invite(c, max_uses=1, max_age=0, unique=True) # Create the invite link
break
print('Finished. Exiting soon...')
exit()
client.run(token)
I made some quick modifications to FedeCuci’s script, with the following differences:
- Script asks for the total number of new invites you want, rather than depending on previously created invites
- Script outputs the new invites to console
- Does not throw an exception
- Compatible with new intents API
- Some additional debugging output
For either script, you will need to install discord.py, then run the script with python3
. Don’t forget to update the token & ID’s as needed, and setup/join the bot to your target server via Discord’s developer portal. It will need permissions for Create Instant Invite
and Read Messages/View Channels
from discord.utils import get
import discord
token = 'bot-token-goes-here' # <-- fill this in
intents = discord.Intents.default()
intents.invites = True
client = discord.Client(intents=intents)
guild_id = guild-id-goes-here # <-- fill this in
number_of_links = input('How many links do you want to create? ')
@client.event
async def on_ready():
print('Bot is up and running.')
print(f'Logged in as {client.user}')
g = client.get_guild(int(guild_id))
print(f'Guild: {g.name}')
c = g.get_channel(channel-id-goes-here) # <-- fill this in
print(f'Channel: #{c}')
invites = []
print('CREATING INVITES')
for i in range(int(number_of_links)):
invite = await g.text_channels[0].create_invite(max_uses=1, max_age=0, unique=True)
print(f'{invite}')
print('Finished. Exiting soon...')
await client.close()
client.run(token)