QNA > C > Come Memorizzare L'input Dell'utente Usando Il Mio Bot Discord.py

Come memorizzare l'input dell'utente usando il mio bot discord.py

Questa guida è stata fatta per usare il discord.py originale, non la riscrittura. Non dovresti usare la riscrittura in ogni caso, è stupida :)

Dipende da cosa intendi per input dell'utente.

Immaginiamo che tu voglia dire che vuoi prendere ciò che l'utente dice, come in un questionario o qualcosa del genere.

Perciò diciamo che prima farai la domanda,

  1. await client.say('Conferma questa è la risposta che vuoi usando `Y` o `n`. 

Ora abbiamo bisogno di costruire su questo per far aspettare la risposta. Make sure you pass the context through in the command, which is done with

  1. @commands.command(pass_context=True) 
  2. async def [commandname](ctx, [any other args]): 

Ora che il nostro bot sa tutto sul comando originale, possiamo anche sapere chi ha scritto il comando. This helps, ass we’ll use

  1. response = await client.wait_for_message(author=ctx.message.author, timeout=60) 

You can modify this command to be however you wish, but ctx.message.author tells the bot that you’re waiting for a message from the author (the person who wrote) of the message. Quindi diciamo che l'utente risponde, ora dovremo fargli pensare a questo.

  1. if response.clean_content (resto della tua dichiarazione if): 

Il clean_content gli permette di analizzarlo come se fosse una normale stringa.

Se volete che non sia sensibile alle maiuscole, dovete usare response.clean_content.lower() così il messaggio viene convertito in minuscolo. (i.e., if user responds with cApItALs it will now be capitals).

This might be a little easier to see in a command, so,

  1. @commands.command(pass_content=True) 
  2. async def yesorno(ctx): 
  3. await client.say('Discord, yes or no?') 
  4. response = client.wait_for_message(author=ctx.message.author, timeout=30) 
  5. if response.clean_content.lower() == 'yes': 
  6. await client.say('You said yes.') 
  7. elif response.clean_content.lower() == 'no': 
  8. await client.say('You said no.') 
  9. else: 
  10. await client.say("That isn't a valid response.") 

Now storing them as things in a json would follow through live everything else that stores with json, with the thing that’s being stored being response.clean_content. I’m not too skilled in this so sorry :(

Di Molton

Come fare un'applicazione di elaborazione testi con python :: Puoi suggerire qualche editore di giochi per cellulari?
Link utili