'Take an argument with spaces as a single string from Discord input [duplicate]
Good evening. In normal cases, taking an argument with spaces in a function only requires you to encapsulate the argument with quotes. This works for most uses, but this one differs since I'm using the Discord interface to process inputs.
I'm fetching a Wikipedia article based on user input:
import wikipedia
...
@commands.command()
async def wiki(self, ctx, wiki): # How would I treat the wiki parameter as a single argument?
page = wikipedia.page(wiki, auto_suggest=False, redirect=True, preload=False) # Fetch Wikipedia page.
embed = discord.Embed(title=page.title, url=page.url, description=page.summary) # Create a fancy embed.
await ctx.send(embed=embed) # Send embed in current channel.
This is not an issue that lies within Discord.py or the Wikipedia API, my question is broader. If a user tries to input Transport Security Layer, only the first word (Transport) will be queried since all spaces are treated as separate arguments for separate parameters. How would you treat the input as a single argument? I have tried formatting and replacing spaces in the code, but as of now, my only solution is to manually quote the input in Discord.
Thank you as always for taking your time! Any suggestions at all are appreciated.
Solution 1:[1]
There's a "consume rest" argument option. You can read more here.
@commands.command()
async def wiki(self, ctx, *, wiki):
Solution 2:[2]
Another way to do this is to get the entire string from ctx.message.context and just manipulate it yourself.
@commands.command()
async def wiki(self, ctx: commands.Context):
wiki = ctx[6:]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eric Jin |
| Solution 2 | Jeffrey DeLucca |


