Pulldown
Pulldown

Reputation: 57

Discord Bot (Python) Trouble with Inputs

I wrote a bot for a school thing, I want people who own the discord to be able to add things to the FAQ list, which I am storing in JSON.

Currently I have it as such:

@bot.command(aliases = ['faqadd'])
async def addfaq(ctx, question = None, *, answer = None):

However the problem with this is that when a person add to it as such:

.faqadd This is a question? This is an answer.

The bot will only use "This" for the question and "is a question? This is an answer." will be the answer. Is there an easy way to just simply take in two groups of strings or should I just split it up by the question mark?

Upvotes: 0

Views: 40

Answers (2)

Ali Hakan Kurt
Ali Hakan Kurt

Reputation: 1037

discord.py is splitting your message content from space after clearing command and prefix from content and parsing arguments. So if you type .faqadd This is a question? This is an answer, the arguments will be This and is a question? This is an answer. If you want to prevent this, you can use quotation marks and the problem will be solved: .faqadd "This is a question?" "This is an answer."

Have a nice day.

Upvotes: 1

Silvio Mayolo
Silvio Mayolo

Reputation: 70387

The discord.ext.command interface is set up such that you have two options. One is to use positional arguments and require the user to place quotes around the arguments.

@bot.command(aliases = ['faqadd'])
async def addfaq(ctx, question = None, answer = None):

Then the user types

.faqadd "This is a question?" "This is an answer."

The other, as you've already noticed, is to take a single keyword-only argument and do the parsing yourself.

@bot.command(aliases = ['faqadd'])
async def addfaq(ctx, *, arg = None):

Upvotes: 1

Related Questions