繁体   English   中英

未知数 arguments 所以一个或多个 discord.py

[英]Unknown number of arguments so one or more discord.py

@bot.command()
async def hellothere(ctx, *, msg):
await ctx.send(ctx + msg)

当我尝试“hello hi hi hi hi”时它不起作用; 我希望它可以打印回“hi hi hi hi”,或者如果我输入了 5 个 hi,它会发回 5 个 hi

你可能会遇到这样的错误:

TypeError: unsupported operand type(s) for +: 'commands.context.Context' and 'str'

而不是添加Context ( ctx ) 和msg只需发送msg

@bot.command()
async def hellothere(ctx, *, msg):
    await ctx.send(msg)

同样正如@yungmaz13 所说,您的缩进不正确,尽管我认为这只是一个复制错误。

要接受任意数量的 arguments,必须将*参数命名为 在这里, *只是一个语法标记,将所需的位置参数ctx与仅关键字参数msg分开。 msg是可选的,但如果存在,则必须是关键字参数,而不是位置参数:

hellothere("hello")   # Good
hellothere("hello", msg="world")  # Good
hellothere("hello", "world")  # Bad

暂无
暂无

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

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