Discord.py 随机选择错误
Discord.py random choice bug
代码:
# Random Choice
@client.command(aliases=["rand_c"])
async def random_choice(ctx, python_list):
await ctx.send(random.choice(python_list))
当我键入正确的 Python 列表时出现奇怪的错误(["Cats"、"Dogs"、"No pet"]):
discord.ext.commands.errors.UnexpectedQuoteError: Unexpected quote mark, '"', in non-quoted string
它在常规 Python 中工作正常,但为什么在 discord.py 中不行?
您命令的所有输入最初都被视为字符串。您需要提供一个转换器函数来告诉命令如何处理该字符串:
from ast import literal_eval
@client.command(aliases=["rand_c"])
async def random_choice(ctx, *, python_list: literal_eval):
await ctx.send(str(python_list))
代码:
# Random Choice
@client.command(aliases=["rand_c"])
async def random_choice(ctx, python_list):
await ctx.send(random.choice(python_list))
当我键入正确的 Python 列表时出现奇怪的错误(["Cats"、"Dogs"、"No pet"]):
discord.ext.commands.errors.UnexpectedQuoteError: Unexpected quote mark, '"', in non-quoted string
它在常规 Python 中工作正常,但为什么在 discord.py 中不行?
您命令的所有输入最初都被视为字符串。您需要提供一个转换器函数来告诉命令如何处理该字符串:
from ast import literal_eval
@client.command(aliases=["rand_c"])
async def random_choice(ctx, *, python_list: literal_eval):
await ctx.send(str(python_list))