How to extract user ID from Discord mentions in a bot command?

I’m working on a Discord bot that creates user profile displays. I’m having trouble with a command where users type !showcard @username and I need to get the actual user ID from the mention.

When I slice the message content to remove the command part (first 9 characters), what’s left is something like <@123456789012345678> instead of just the numbers. I tried using regex to strip out the symbols:

user_mention = str(message.content[9:])
clean_id = re.sub("[!@#$%^&*()[]{};:,./<>?\|`~-=_+]", " ", user_mention)
print(clean_id)

But some characters remain and I just want the numeric ID for my database query. There must be a cleaner approach to handle Discord user mentions. What’s the proper method to extract user IDs from these mention strings?

Hit this exact problem building a moderation bot last year. Cleanest fix is using discord.py’s converter system - skip the manual parsing entirely.

Don’t slice message content, let the command handler do it:

@bot.command(name='showcard')
async def show_card(ctx, user: discord.Member):
    user_id = user.id
    # Now you have the clean numeric ID

This handles all mention formats including nicknames automatically. You get the full Member object with user.id, user.name, everything. No regex, no edge cases breaking your bot.

If you’re stuck with manual parsing, your regex replaces characters with spaces instead of removing them. Use re.sub(r'[<@!>]', '', user_mention) to strip them out. But honestly, converters save tons of headaches later.

Your regex approach is overcomplicating things. Discord mentions follow a predictable pattern - just extract the ID directly.

The format’s always <@!123456789> or <@123456789>. Strip the brackets and symbols:

user_mention = message.content[9:]
user_id = user_mention.strip('<@!>')
print(user_id)

Or use simple regex that grabs digits:

import re
user_mention = message.content[9:]
user_id = re.findall(r'\d+', user_mention)[0]

But honestly, Discord bots get messy fast once you add more features. I’ve built similar profile systems - the real challenge hits when you’re syncing data between Discord and your database, handling errors, or integrating with other services.

I switched to Latenode for this. You can set up Discord webhooks that automatically parse mentions, extract IDs, and update your database without any parsing code. Plus it handles error cases and retries automatically.

Works way better than managing bot code and regex patterns yourself.

discord.py handles mention parsing for you. Just use message.mentions[0].id to get the user ID directly - no regex needed. Way cleaner than string manipulation and catches edge cases automatically.

Your regex is creating spaces instead of removing characters, and you’re overcomplicating the pattern. Discord mentions are simple - they’re either <@123456789> or <@!123456789>. Easy fix: user_id = user_mention.strip('<@!>') strips all those characters at once. But don’t hardcode that slice at position 9 - that’s asking for trouble. I built a trading card bot where users constantly broke commands with weird spacing. Use match = re.search(r'<@!?(\d+)>', message.content) then user_id = match.group(1) if there’s a match. Finds mentions anywhere in the message, grabs just the numbers, handles both formats, and works even when users add random spacing or text before the mention.

Been dealing with mention parsing for years in my moderation tools. Your regex replaces characters with spaces instead of removing them, and you’re targeting too many symbols. Quick fix without changing your command structure: user_id = re.search(r'\d+', user_mention).group() - pulls just the number from whatever mention format Discord sends.

Here’s what I learned the hard way: hardcoding that slice at position 9 breaks when users add extra spaces or use different command formats. I’ve watched bots crash because someone typed !showcard @user with double spaces. Check if the mention exists first with if '<@' in message.content before extracting.

Also, some Discord clients send mentions with exclamation marks, others don’t. Your parsing needs to handle both <@123> and <@!123> formats.

Had the same problem building a leaderboard for our gaming server. Your regex replaces characters with spaces instead of removing them, and you’re targeting way more symbols than needed.

Discord mentions only have angle brackets, @ symbols, and sometimes exclamation marks. Just use user_id = re.sub(r'[<@!>]', '', user_mention) to strip those characters. Way simpler.

But honestly? Skip the hardcoded slicing completely. What if someone adds extra spaces or uses a different prefix? I’ve watched bots crash because users don’t type commands perfectly. Parse the mention straight from the message with re.search(r'<@!?(\d+)>', message.content) - finds the mention anywhere and grabs the ID in one go. Works with both mention formats and doesn’t break when spacing gets weird.

Yeah, manual parsing works but becomes a pain once your bot grows past basic stuff.

I built an employee recognition bot for our company Discord. Started with regex and manual ID extraction, then added bulk processing, database syncing, error handling. The code turned into a maintenance nightmare.

Switched to Latenode instead. Built a workflow that listens for Discord messages, pulls user IDs from any mention format, hits our database, and spits out profile cards. No more regex debugging.

Edge cases are where it really shines. Multiple user mentions, weird formatting - Latenode handles it all without extra validation. Built-in retry logic when database calls crap out too.

For just a showcard command, manual parsing is fine. But if you’re planning bulk operations or integrations with other tools, automation platforms handle the mess way better.