繁体   English   中英

提及作者 Python Bot

[英]Mention Author Python Bot

我需要在这一行中创建标签作者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 标识符不能以数字开头 您可以做的是将命令的名称指定为协程名称以外的名称。

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

嗯,这很容易!

async def (ctx,k : int):

只需添加 ctx 就完成了!

bot.say

会给你错误而不是使用

ctx.send

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM