Skip to content

Instantly share code, notes, and snippets.

@drakantas
Last active May 26, 2022 17:28
Show Gist options
  • Save drakantas/ec2c524b95a688b1618d7cc810d490c4 to your computer and use it in GitHub Desktop.
Save drakantas/ec2c524b95a688b1618d7cc810d490c4 to your computer and use it in GitHub Desktop.
Get Discord invite links from a message using regex
from __future__ import print_function
import re
DISCORD_INVITE = r'discord(?:\.com|app\.com|\.gg)[\/invite\/]?(?:[a-zA-Z0-9\-]{2,32})'
messages = (
"Hey, join my guild and get awesome prizes. https://discord.gg/0cDvIgU2voWn4BaD",
"Yo. Don't join that dude's guild, these guilds are even more awesome https://discord.gg/discord-api "
"https://discord.gg/discord-developers",
"There's no invites in this message."
)
def main():
for message in messages:
print(message + '\nINVITES FOUND:', get_invites(message), '\n')
# Returns a tuple of invite links if there's any, or None in case there is not.
# No type annotations because I want this to run anywhere.
def get_invites(message):
regex = re.compile(DISCORD_INVITE)
invites = regex.findall(message)
return invites or None
if __name__ == '__main__':
main()
@kpodp0ra
Copy link

kpodp0ra commented Nov 9, 2020

Hi @GrJT, please use my corrected regex: discord(?:\.com|app\.com|\.gg)/(?:invite/)?([a-zA-Z0-9\-]{2,32}).
I tested it and its actually works. (@drakantas you can update this gist with my regex too)

@XPMai
Copy link

XPMai commented Mar 6, 2021

Hi @GrJT, please use my corrected regex: discord(?:\.com|app\.com|\.gg)/(?:invite/)?([a-zA-Z0-9\-]{2,32}).
I tested it and its actually works. (@drakantas you can update this gist with my regex too)

"Your" regex wouldn't solve @GrJT's problem either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment