【问题标题】:Mention Author Python Bot提及作者 Python Bot
【发布时间】:2018-11-17 04:50:33
【问题描述】:

我需要在这一行添加标签作者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)

使用命令时出现这样的错误。

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

【问题讨论】:

  • 因为python语法中函数不能以数字开头,就这么简单

标签: python python-3.x discord discord.py


【解决方案1】:

Python identifiers can't begin with a number。您可以做的是将命令的名称指定为协程名称以外的其他名称。

@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")

【讨论】:

  • 谢谢兄弟,它提供了任何提示如何在此代码中提及作者await bot.say("This is your random {} pick".format(k))
  • 您可以使用格式在字符串"This is your random {} pick, {}".format(k, ctx.message.author.mention) 中插入多个值。你也可以使用 f-strings f"This is your random {k} pick, {ctx.message.author.mention}"
  • 我试过这个,但得到 ctx 错误。这个问题来自你回复的stackoverflow.com/questions/50722715/…
  • 能否将您看到的完整错误编辑到问题正文中?
  • 你能发布命令的完整代码和任何错误处理程序吗?看起来您在某处缺少pass_context=True
【解决方案2】:

这很简单!

async def (ctx,k : int):

只需添加 ctx 即可! 还有

bot.say

会给你错误而不是使用

ctx.send

【讨论】:

    猜你喜欢
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2021-05-12
    • 2019-06-09
    • 2019-11-19
    • 1970-01-01
    • 2018-06-28
    相关资源
    最近更新 更多