How to make Discord bot tag specific users in messages

I’m building a Discord bot that needs to tag users other than the person who sent the command. I keep running into issues when trying to make the bot mention someone.

if message.content.startswith("+joke"):
    target_user = bot.get_user_info(user_id)
    await bot.send_message(message.channel, target_user.mention + ' got pranked!')

Every time I test this I get an error:

AttributeError: 'generator' object has no attribute 'mention'

This error shows up whether I include a user mention in the command or not. My imports are:

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import random

What’s the right way to get user objects and mention them in bot responses?

That generator error’s from using an outdated method. I ran into the same thing when I started with discord.py - the API’s changed a lot between versions. Don’t bother parsing user IDs manually. Just let Discord handle mentions directly in your command:

if message.content.startswith("+joke"):
    parts = message.content.split()
    if len(parts) > 1 and message.mentions:
        target_user = message.mentions[0]
        await message.channel.send(f'{target_user.mention} got pranked!')

When someone types “+joke @username”, the bot automatically parses the mention and you grab it through message.mentions. Way more reliable than extracting user IDs manually and fetching user objects.

yeah, get_user_info is gone now in the newer discord.py. try using fetch_user() if they’re not cached, or just get from guild members with message.guild.get_member(user_id). the guild method’s way better for mentions too.

Your problem is get_user_info() returns a generator, not a user object. Use get_user() instead, though it’s deprecated in newer discord.py versions. Here’s how to fix it:

if message.content.startswith("+joke"):
    target_user = bot.get_user(user_id)
    if target_user:
        await message.channel.send(f'{target_user.mention} got pranked!')
    else:
        await message.channel.send('User not found')

Make sure user_id is an integer, not a string. If you’re parsing it from message content, convert it with int(user_id). Also consider switching to the commands framework with @bot.command() decorators - it’s way cleaner.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.