Mention user that made the command with Discord.py

1

I'm trying to use Discord.py to program bots for Discord but I found the slight problem that I can not find any way to mention the user who "called" or performed the command.

They told me that it was possible using ctx.message.author but I could not make it work.

Code I try to use:

import discord
from discord.ext import commands
...
client = commands.Bot(command_prefix='!', description='Ejemplo')
...
@client.command()
async def prueba(texto : str):
    print('Comando de prueba ejecutado, texto recibido: ' + texto)
    await client.say(ctx.message.author + ':ok_hand:')
    
asked by Lemon 27.04.2017 в 09:05
source

1 answer

1

@ lois6b was too close. You have to pass the context to the command so you can use it and then format the text using variables:

# Importa Discord.py
import discord
# Importa los Comandos de Discord.py
from discord.ext import commands
...
# Crea el cliente aka Bot
client = commands.Bot(command_prefix='!', description='Ejemplo')
...
# Define la sección como comando
# pass_context=True le envía el contexto
@client.command(pass_context=True)
# ctx = contexto
# texto = texto despues del comando
async def prueba(ctx, texto : str):
    # Escribe en la consola el texto recibido
    print('Comando de prueba ejecutado, texto recibido: ' + texto)
    # Almacena el nombre del autor en la variable "usuario"
    usuario = ctx.message.author
    # Formatea el texto en donde 0=usuario y "mention" es la mención a este
    await client.say('{0.mention} :ok_hand:'.format(usuario))
    
answered by 27.04.2017 / 09:37
source