0
I’m starting programming, and I’m doing a bot on Discord using python (Discord.py)
I need to create a command for the bot to return the availability of fruit within the list.
class Frutas(commands.Cog): # cog
def __init__(self, client): # setup pra cog.
self.client = client
@commands.command(aliases=['f']) # comando para escolher frutas.
async def fruta(self, ctx, *, fruta):
responses = [ # frutas disponíveis
'Maçã vermelha',
'Maçã verde',
'Banana amarela'
]
if fruta in responses: # se a fruta solicitada existe em "responses":
await ctx.channel.send(responses.index) # (aqui deveria mandar a fruta correspondente à lista.)
else: # se não:
await ctx.channel.send(f'não temos essa fruta na loja ({fruta})') # retorne que não temos a fruta.
def setup(client):
client.add_cog(Frutas(client)) # cog
with this code, it returns me this
I wanted to put ! f apple and make the bot identify the "Red Apple" on the list and return it as a message of my choice, but I don’t know how to make him identify it...
I have also tested without the index, but then it sends the list with all the fruits, and I still need to type "! f Red apple" instead of "! f apple"
but how would I turn the fruitAlvo into the fruit the user chooses? (! f (fruit)
– balas
The target fruit is just an example string that I used to explain the code. But, whereas you have the exact string that is contained in a string list, you can use listDeString.index('string'), which will return the index the string is in.
– Henrique Hott
What I found confusing is that in your question there are two problems: You want to know how to get the possible answers to the user input (since the apple input can be both red and green apple), and the problem with the content. For the first problem, you can use the break command inside if you only want one answer, and for the second the previous comment.
– Henrique Hott
If my code was helpful, please consider evaluating it.
– Henrique Hott