Mayer_Szabolcs
Mayer_Szabolcs

Reputation: 3

How to ask the bot to send back more words and not just the first [discord.py]

i just want to make a google search thing... I have the restof the code to do that i would change the input tag to the message tag or someting

@bot.command(aliases=['a'])
async def on_message(ctx, messages):
  await ctx.send(messages)

what the bot id doing to that code

(the code what i wanted to be)

@bot.command(aliases=['google'])
async def on_message(ctx, message):
  googleinput = message
  pgoogleinput = googleinput.replace(" ", "+")
  await ctx.send("https://www.google.com/search?q="+pgoogleinput)

Upvotes: 0

Views: 47

Answers (2)

Harukomaze
Harukomaze

Reputation: 462

you need to add * before the argument you want to consume fully. basically, * collects all the positional arguments in a tuple. So the code will look like:

@bot.command(aliases=['a'])
async def on_message(ctx, *, messages):
  await ctx.send(messages)

Similarly, for second code, it will look like:

@bot.command(aliases=['google'])
async def on_message(ctx, *, message):
  googleinput = message
  pgoogleinput = googleinput.replace(" ", "+")
  await ctx.send("https://www.google.com/search?q="+pgoogleinput)

These are mentioned in the python documentation.

Upvotes: 0

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15728

You can simply pass the message as a keyword-only arguments, thanks to this discord.py will parse all the arguments as one string

@bot.command(aliases=['google'])
async def on_message(ctx, *, message):
  googleinput = message
  pgoogleinput = googleinput.replace(" ", "+")
  await ctx.send("https://www.google.com/search?q="+pgoogleinput)

Upvotes: 1

Related Questions