Home > front end >  Take an argument with spaces as a single string from Discord input
Take an argument with spaces as a single string from Discord input

Time:05-05

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.

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.

CodePudding user response:

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

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

CodePudding user response:

Add a * before the wiki argument to make it accept everything, like this:

@commands.command()
async def wiki(self, ctx, *, wiki):
    #...
  • Related