Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Take an argument with spaces as a single string from Discord input

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you as always for taking your time! Any suggestions at all are appreciated.

Example of my problem. Only the word 'Transport' is processed.

Second example shows my current solution. Encapsulating the input directly works, but it's tedious and prone to errors.

>Solution :

There’s a "consume rest" argument option. You can read more here.

@commands.command()
async def wiki(self, ctx, *, wiki):
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading