S. Coughing
S. Coughing

Reputation: 186

How do I check if an invite is invalid?

I saw this question on stackoverflow: Testing if discord invite link is invalid?, but it's a year old and some of the modules don't exist in Python 3.

I've tried doing (the invite here is invalid): I expected a 404 not found error:

import urllib.request
content = urllib.request.urlopen('https://discord.gg/UWxaV8m').read()
print(content)

But, that gives me an error: urllib.error.HTTPError: HTTP Error 403: Forbidden; are there other ways of doing this? Also, looking at the error: it looks like I'm not allowed to access that invite because I'm a bot user? idk

Upvotes: 2

Views: 2049

Answers (3)

laugher
laugher

Reputation: 1

If you're not planning to this programmatically, just use curl.

curl "https://discordapp.com/api/invite/invite-code-goes-here"

Sample Output Mockup:

{"code": "invite-code-goes-here", "guild": {"id": "123456789012345678", "name": "Discord Server Name", "splash": null, "banner": null, "description": null, "icon": "0a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q", "features": [], "verification_level": 0, "vanity_url_code": null, "nsfw": false, "nsfw_level": 0}, "channel": {"id": "123456789012345678", "name": "general", "type": 0}, "inviter": {"id": "876543210987654321", "username": "person-who-created-invite", "avatar": "0a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p", "discriminator": "6295", "public_flags": 0}}

Upvotes: 0

DirtyBit
DirtyBit

Reputation: 16792

Using a url from the discord request page and adding a user-agent in the header, I was able to get the response:

import urllib.request
url = 'https://discord.com/api/v6/invites/UWxaV8m?with_counts=true'
user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
headers={'User-Agent':user_agent,}
request=urllib.request.Request(url,None,headers) #The assembled request
response = urllib.request.urlopen(request)
data = response.read() # The data u need
print(data)

OUTPUT:

b'{"code": "UWxaV8m", "guild": {"id": "599754844676554783", "name": "Thicc Mario Hub | 10 Boosts", "splash": "1d76b7a1ecceffa42552abc88ea5d6ad", "banner": "7036d8cd5e36e91fe5c87478c5eae37a", "description": null, "icon": "a_3355e47f1d336ad04737afc2ba08fe62", "features": ["ANIMATED_ICON", "INVITE_SPLASH"], "verification_level": 2, "vanity_url_code": null}, "channel": {"id": "599761618720653313", "name": "\\u2554\\u27a4\\u300e\\ud83d\\udcdc\\u300frules", "type": 0}, "inviter": {"id": "429840967470678037", "username": "Thicc Mario", "avatar": "dd33989a2b86a43f53280174addd3a21", "discriminator": "8611", "public_flags": 256}, "approximate_member_count": 1662, "approximate_presence_count": 373}'

Upvotes: 2

Diggy.
Diggy.

Reputation: 6944

If you're wanting the user to input an invite via a command argument, it will throw an exception if the invite is invalid (a discord.NotFound error, to be exact):

@bot.command()
async def inv(ctx, invite: discord.Invite):
    await ctx.send("That invite is valid!")
@inv.error
async def inv_error(ctx, error):
    if isinstance(error, commands.BadArgument):
        if isinstance(error.original, discord.NotFound):
            await ctx.send("That invite is invalid or has expired.")

Or you can use fetch_invite():

try:
    invite = await bot.fetch_invite(URL_OR_ID_HERE)
    await ctx.send("That's a valid invite!")
except:
    await ctx.send("Sorry, that invite was invalid or has expired.")

There's no need for any other modules - just the discord library is capable of telling you information about the invite.


References:

Upvotes: 2

Related Questions