Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import base64
- import discord
- # Starts with 'MTE' and matches a MTE23.6.38 format.
- STRICT_REGEX = re.compile(r"(?P<user_id>MTE[\w-]{23})\.(?P<salt>[\w-]{6})\.(?P<signature>[\w-]{38})")
- # As a fallback, any 26.6.38 token structure
- FALLBACK_REGEX = re.compile(r"(?P<user_id>[\w-]{26})\.(?P<salt>[\w-]{6})\.(?P<signature>[\w-]{38})")
- async def detect_and_validate_token(message: discord.Message) -> str | None:
- match = STRICT_REGEX.search(message.content)
- if not match:
- match = FALLBACK_REGEX.search(message.content)
- if not match:
- return None
- user_id_encoded = match.group("user_id")
- try:
- # Make sure padding is correct for base64.b64decode and then decode.
- padded = user_id_encoded + '=' * (-len(user_id_encoded) % 4)
- user_id_bytes = base64.urlsafe_b64decode(padded)
- user_id_str = user_id_bytes.decode('utf-8')
- if not user_id_str.isdigit():
- return None
- user_id = int(user_id_str)
- except Exception:
- return None # IF Base64 decode failed.
- try:
- # Validation
- user = await message.client.fetch_user(user_id)
- if user.bot:
- return match.group(0) # Return the fully matched discord bot token.
- except discord.NotFound:
- pass
- except discord.HTTPException:
- pass
- return None
- @client.event
- async def on_message(message: discord.Message):
- if message.author.bot:
- return
- token = await detect_and_validate_token(message)
- if token:
- await message.delete()
- 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