'How do I accept a string as an argument? (discord.py rewrite)

I am trying to make a discord.py command that accepts a string as a positional argument.

How do I accept a string from a Discord message (and, by extension, booleans and integers) as an argument?

Example Code

# -- snip --

@bot.command()
async def echo(ctx):
  """
  Example command to return the input of this command.
  """
  # argument name should be called `arg`

  await ctx.respond(arg)
   


Solution 1:[1]

If you want to pass a positional parameter you can do so like that:

async def test(ctx, arg):
    await ctx.send(arg)

The user can then provide an argument when invoking the command: $test foo, which has the output foo when you want to pass an argument with whitespace in between, you can either just quote the argument: $test "foo bar", which has the output foo bar or modify your command to handle an undetermined number of parameters:

@bot.command()
async def test(ctx, *args)

To work with Integers and Boolean values you can make use of some little python tricks to do so. to convert a String that only has Integers in it you can just convert the String to an Integer with int() - if the string should be converted to a bool you can work with comparison like:

s = "Foo"
#<User puts in a value for s>#
s == "Foo" #Returns a boolean value (True if the string matches, False if it does not)

Solution 2:[2]

It's literally the first things that appears when reading the introduction to commands, here's the link

Also here's an example

@bot.command()
async def foo(ctx, arg):
    await ctx.send(arg)

# To invoke
# !foo hello
# >>> hello

Solution 3:[3]

Have you looked at other questions regarding getting messages from the discord bot?

edit: Updated answer given update to question

Solution 4:[4]

Its pretty Simple If you want to take an argument without spaces,

@bot.command()
async def combinestring(ctx, arg: str):
    #your code

If you want to take an argument with spaces,

@bot.command()
async def combinestring(ctx, *, arg):
    #your code

Done.

Solution 5:[5]

A example would be something like a reminder command. For that, you can do the following:

@client.command()
async def reminder(ctx, time: int, *, message: str):

The time will be a integer, and the message will be a string.

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 1111moritz
Solution 2 Łukasz Kwieciński
Solution 3
Solution 4 Frombull
Solution 5 Sarvesh Jagtap