Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a specific amount of invite links that can only be used once for a Discord server in Python

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!

like image 882
FedeCuci Avatar asked Oct 26 '25 06:10

FedeCuci


1 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)
like image 116
FedeCuci Avatar answered Oct 29 '25 09:10

FedeCuci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!