How do I put multiple words in one argument discord.py

◇◆丶佛笑我妖孽 提交于 2021-01-07 01:19:31

问题


I am working on a bot that uses discord.py, and I want to have a command that lets you set the game the bot is playing, but I don't know how to make an argument that allows spaces.

I have tried to make 2 arguments, but then if you want one word it will show up as an error.


@client.command()
async def game(gameplay):
    #do things

I want the argument "gameplay" to have multiple words in it. Can someone please help?


回答1:


@client.command()
async def game(ctx, *args):
    # args contains all arguments written after the command i.e !game game i want to play
    # print(" ".join(args[:])) will print "game i want to play"

As you can see in the example, *args will contain everything written after the command. ctx will be the context. Hope this helps.




回答2:


@client.command()
async def game(ctx, *, args):
    ctx.send(args)


来源:https://stackoverflow.com/questions/55665255/how-do-i-put-multiple-words-in-one-argument-discord-py

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!