Ori_

Discord bot token detection.

Aug 3rd, 2025 (edited)
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. import re
  2. import base64
  3. import discord
  4.  
  5. # Starts with 'MTE' and matches a MTE23.6.38 format.
  6. STRICT_REGEX = re.compile(r"(?P<user_id>MTE[\w-]{23})\.(?P<salt>[\w-]{6})\.(?P<signature>[\w-]{38})")
  7.  
  8. # As a fallback, any 26.6.38 token structure
  9. FALLBACK_REGEX = re.compile(r"(?P<user_id>[\w-]{26})\.(?P<salt>[\w-]{6})\.(?P<signature>[\w-]{38})")
  10.  
  11. async def detect_and_validate_token(message: discord.Message) -> str | None:
  12.     match = STRICT_REGEX.search(message.content)
  13.     if not match:
  14.         match = FALLBACK_REGEX.search(message.content)
  15.         if not match:
  16.             return None
  17.     user_id_encoded = match.group("user_id")
  18.  
  19.     try:
  20.         # Make sure padding is correct for base64.b64decode and then decode.
  21.         padded = user_id_encoded + '=' * (-len(user_id_encoded) % 4)
  22.         user_id_bytes = base64.urlsafe_b64decode(padded)
  23.         user_id_str = user_id_bytes.decode('utf-8')
  24.  
  25.         if not user_id_str.isdigit():
  26.             return None
  27.         user_id = int(user_id_str)
  28.  
  29.     except Exception:
  30.         return None  # IF Base64 decode failed.
  31.  
  32.     try:
  33.         # Validation
  34.         user = await message.client.fetch_user(user_id)
  35.         if user.bot:
  36.             return match.group(0)  # Return the fully matched discord bot token.
  37.     except discord.NotFound:
  38.         pass
  39.     except discord.HTTPException:
  40.         pass
  41.  
  42.     return None
  43.  
  44.  
  45.  
  46. @client.event
  47. async def on_message(message: discord.Message):
  48.     if message.author.bot:
  49.         return
  50.  
  51.     token = await detect_and_validate_token(message)
  52.     if token:
  53.         await message.delete()
  54.         await message.channel.send("🚫 A discord bot token was found and deleted.") # You can put anything here.
Add Comment
Please, Sign In to add comment