![](/img/trans.png)
[英]how to import __future__ for keyword-only argument of python 3.0?
[英]how to get length of keyword-only argument?
通常 - 我試圖制作if len(arg) < 1:
沒有工作(僅當有多個 arg 工作時),當我制作(ctx, *args)
而不是(ctx *, arg)
它幾乎工作但也添加了更多字符(只是它沒有拋出test
而是('test'),
不知何故)。 如何使它工作? 沒有添加符號並且沒有搞亂刪除所有符號? 完整的當前代碼在這里:
@bot.command()
async def cowsay(ctx, *, arg):
if len(arg) < 1:
url = 'https://icanhazdadjoke.com/'
joke = requests.get(url, headers={"Accept":'application/json '}).json()
await ctx.send(f" \_\_\_\_\_\_\_\_\_\_\_\n < {joke['joke']} > \n ------------- \n \\ ^\_\_^ \n \\ (oo)\\_\_\_\_\_\_\_ \n (\_\_)\\ )\\/\\ \n \|\|-------w \| \n \|\| \|\|" )
else:
await ctx.send(f" \_\_\_\_\_\_\_\_\_\_\_\n < {arg} > \n ------------- \n \\ ^\_\_^ \n \\ (oo)\\_\_\_\_\_\_\_ \n (\_\_)\\ )\\/\\ \n \|\|-------w \| \n \|\| \|\|" )
PS:discord 官方沒有說明如何處理*, arg
如下所示: https : //discordpy.readthedocs.io/en/latest/ext/commands/commands.html#keyword-only-arguments
如果您想要做的是檢測是否有任何 arg 傳遞給命令,您只需檢查args
長度:
@bot.command()
async def cowsay(ctx, *args):
if not args:
url = 'https://icanhazdadjoke.com/'
joke = requests.get(url, headers={"Accept":'application/json '}).json()
await ctx.send("string_without_args")
else:
await ctx.send(f"string_with_{args}")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.