Use Python to Generate Discord Invite ULRs en masse



When creating invitations to Discord servers – or "guilds" how they seem to called internally – you can configure some limits. For example, you can limit the number of times a link can be used to join a server or you can configure an expiry date. The default setting for the time limit is 7 days btw, which is why most links are expired when you come back to them after some time. I was in the situation that I wanted to invite a limited number of people to a Discord server and each should only receive a link that can only be used once. So in some sense, I wanted to create personalized invite links, which doesn't seem to be a use case that's supported out of the box. # The Plan Maybe I'm doing it wrong but the only way I could find to create invite links through the UI was to clicking the small person next to the cogwheel icon in the same line as the channel name. There you can either select people to invite (which isn't useful for me) or you can copy a link (and also configure it as described in the intro). But every time I did this, the generated link was the same. So the workflow then would be: create a link, send it to a person, wait for them to accept, create a new link, and so on. This is obviously quite annoying because we'd have to wait for every single person to join before we can invite the next one. Let's automate this! The only reasonable way to interact with Discord programmatically seem to be "Applications" and a very well supported framework called discord.py has lots of examples tailored to Discord Bots (which seem to be a special case of Applications). So the plan now is to 1. create a Bot, 2. invite it to the server in question, 3. and write a Python script that creates as many invites as we need. Let's get the script out of the way first: # The Python Script After pip install discord.py the following Python script will do exactly what we want:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import discord

CHANNELS = ['gibson']
PEOPLE = ['Zero Cool', 'Crash Override', 'Acid Burn', 'Cereal Killer', 'Lord Nikon', 'The Phantom Phreak']


class InviteClient(discord.Client):
    async def on_ready(self):
        for guild in self.guilds:
            for channel in guild.channels:
                if channel.name not in CHANNELS:
                    continue
                for person_name in PEOPLE:
                    invite = await channel.create_invite(
                        reason=f'#{channel} {person_name}',
                        max_uses=1,
                        max_age=60 * 60 * 24 * 7,
                    )
                    print(f'{person_name} -> {guild.name}/{channel.name}: {invite.url}')


client = InviteClient(intents=discord.Intents.default())
client.run('MTAzNjc4NDA3OTIwODcyMjQ2Mg.Gp9_PZ.rIk1cIPCuSqsiXG5DVkxWXwkw5P-DqL-T4t9-w')
Let's next click through some UIs to create a bot application. # Create a Bot This is straight forward by clicking through the UI: * Visit https://discord.com/developers/applications and click "New Application" at the top right. * Choose a fitting name for the application and turn it into a bot by clicking on the "Bot" menu item and confirm the warning. * Then click "Reset Token" to replace the token which is passed to the run method in the the above script. # Invite Bot to Server Inviting a Bot to a server seems to be surprisingly admin-y task>: You first need to decide what permissions the bot needs on your server. This is encoded in an integer like in the 90s: navigate to the "Bot" menu of your application on https://discord.com/developers/applications again and scroll down to "Bot permissions". This form does not control the permission of your bot, it just helps to calculate the integer. In our case, we only need the "Create Instant Invite" permission, which translates to the integer 1. Then we need to create the actual invite link. For this, we need three things: 1. A client_id which can be found on the "General Information" menu item under "Application Id", it's a long integral number. 2. The desired permissions, which we just determined to be 1. 3. A scope, which is bot applications.commands. This all leads to the glorious invite link:
https://discord.com/api/oauth2/authorize?client_id=CLIENT_ID&permissions=1&scope=bot%20applications.commands
Replace CLIENT_ID and visite the site. There you can select a server and confirm granting the permissions to the bot application.

Leave a Reply

Your email address will not be published. Required fields are marked *