Discord.py - How to (at)mention a Discord user from Bot Command

Viewed 2648

Not really a dupe? Explaination:
This question is not looking to (at)mention a user
This question lists member.mention but does not provide a complete example

My Question
Using Discord.py (if the title was not clear enough) I am trying to figure out how to add the @mention attribute to a Discord user when they run a command.

Example:
User input: $99
Bot output: @User your quote is: Bingpot!

UPDATE: 10/24/2019
- @epic-programmer pointed out a pretty egregious copy-paste error which I have fixed :)
- That fix sets "member" as an argument for the command. What I would like to to is get the members display name, and use that in the command output (as an @mention)

Google Answer

@commands.command()
async def mention_ping(self, ctx, member : discord.Member):
    await ctx.send(f"PONG {member}")

My Code (Updated: Use @epic-programmers partial fix)

import random
import discord

from discord.ext import commands

#----
tokenfile = 'token.txt'
with open(tokenfile) as tf:
    line = tf.readline()
    TOKEN = line.rstrip()
#----

bot = commands.Bot(command_prefix='$')

@bot.command(name='99', help='Responds with a random quote from Brooklyn 99')
async def nine_nine(ctx, member : discord.Member):
    brooklyn_99_quotes = [
        'I\'m the human form of the  emoji.',
        'Bingpot!',
        (
            'Cool. Cool cool cool cool cool cool cool, '
            'no doubt no doubt no doubt no doubt.'
        ),
    ]

    response = random.choice(brooklyn_99_quotes)
    await ctx.send("{} your quote is: {}".format(member, response))

bot.run( TOKEN)

Resources I've looked at

1 Answers

The member argument is on the wrong location. Try moving it to the def statement.

@bot.command(name='99', help='Responds with a random quote from Brooklyn 99')
async def nine_nine(ctx, member : discord.Member):
    brooklyn_99_quotes = [
        'I\'m the human form of the  emoji.',
        'Bingpot!',
        (
            'Cool. Cool cool cool cool cool cool cool, '
            'no doubt no doubt no doubt no doubt.'
        ),
    ]

    response = random.choice(brooklyn_99_quotes)
    await ctx.send("{} your quote is: {}".format(member, response))

bot.run( TOKEN)

EDIT: In order to mention the current user calling the command, you can do:

from typing import Optional

@bot.command(name='99', help='Responds with a random quote from Brooklyn 99')
async def nine_nine(ctx, member : Optional[discord.Member] = None):
    member = member or ctx.author
    brooklyn_99_quotes = [
        'I\'m the human form of the  emoji.',
        'Bingpot!',
        (
            'Cool. Cool cool cool cool cool cool cool, '
            'no doubt no doubt no doubt no doubt.'
        ),
    ]

    response = random.choice(brooklyn_99_quotes)
    await ctx.send("{} your quote is: {}".format(member, response))

bot.run( TOKEN)

This code block will check for a ping, and if it does not find one, then it will default to the user calling the command.

Related