0

I need to make tag author in this line This is your random {} pick,

token = "xxxxxxxxxxxxxxxxxxxxxxxx"
prefix = "?"

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

bot = commands.Bot(command_prefix=prefix)
bot.remove_command("help")

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

answers = ["apple", "ball", "cat", "dog", "elephant", "frog", "gun"]

@bot.command()  
async def choose(k : int):
    """Chooses between multiple choices."""
    if 0 <= k <= 10:
        await bot.say("This is your random {} pick, {}".format(k, ctx.message.author.mention))
        embed = discord.Embed(description='\n'.join(random.choices(answers, k=k)))
        await bot.say(embed=embed)
    else:
        await bot.say("Invalid number")

bot.run(token)

getting error like this while command used.

Ignoring exception in command choose
Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 50, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "E:\Test.py", line 25, in choose
    await bot.say("This is your random {} pick, {}".format(k, ctx.message.author.mention))
NameError: name 'ctx' is not defined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 374, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 54, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'ctx' is not defined
Demotry
  • 869
  • 4
  • 21
  • 40

2 Answers2

2

Python identifiers can't begin with a number. What you can do is specify the name of the command to be something other than the name of the coroutine.

@bot.command(pass_context=True)  
async def choose(ctx, k: int):
    """Chooses between multiple choices."""
    if 0 <= k <= 10:
        await bot.say("This is your random {} pick, {}".format(k, ctx.message.author.mention))
        embed = discord.Embed(description='\n'.join(random.choices(answers, k=k)))
        await bot.say(embed=embed)
    else:
        await bot.say("Invalid number")
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • Thank you bro it worked any tips how to mention author in this code `await bot.say("This is your random {} pick".format(k))` – Demotry Jun 07 '18 at 15:08
  • You can use format to insert more than one value into a string `"This is your random {} pick, {}".format(k, ctx.message.author.mention)`. You can also use f-strings `f"This is your random {k} pick, {ctx.message.author.mention}"` – Patrick Haugh Jun 07 '18 at 15:19
  • I tried this but getting ctx error. this question is from https://stackoverflow.com/questions/50722715/select-random-xx-answer-python-bot you replyed. – Demotry Jun 07 '18 at 15:31
  • Could you edit the full error you're seeing into the body of your question? – Patrick Haugh Jun 07 '18 at 15:37
  • Could you post the full code for the command and any error handlers? It looks like you're missing a `pass_context=True` somewhere – Patrick Haugh Jun 07 '18 at 16:41
  • "`ctx` is not defined". You need to pass in `ctx` using `pass_context`. – Patrick Haugh Jun 07 '18 at 17:25
0

Well this is easy!

async def (ctx,k : int):

Just add ctx and you're done! Also

bot.say

Will give you error instead use

ctx.send