1

So as the title suggests I'm trying to make my bot respond to a certain message. So after a user sends an image, it'll respond with a compliment, then if the user RESPONDS TO THAT MESSAGE with a message such as "thanks" the bot will then go on to respond with a different message, but I'm trying to make it respond to multiple versions of "thanks" using if statements. This is the code so far:

if '.png' in message.content or '.jpg' in message.content or '.jpeg' in message.content:
            await bot.send_message(message.channel, "woah, very nice!")
            msg = await bot.wait_for_message(author=message.author)
            if msg:
                if msg.content == 'thanks':
                    print("test") 
                    await bot.send_message(message.channel, "hey, gotta compliment nice images right?")

i'm not sure if I'm doing this right, the python shell doesn't print test and the the bot doesn't respond in the server.

brandone
  • 94
  • 1
  • 4
  • 14
  • 1
    I reopened the question because the duplicate I found [was also you](https://stackoverflow.com/questions/52173801/making-a-bot-respond-to-an-image-using-discord-py/52173996#52173996). It seems like the code there was working. What changed? – Patrick Haugh Oct 15 '18 at 18:36
  • 1
    You can include the desired `content` in `wait_for_message`: `await bot.wait_for_message(author=message.author, content="thanks")` – Patrick Haugh Oct 15 '18 at 18:38
  • 1
    i edited the question again, if that doesn't help then idk how to word it differently – brandone Oct 15 '18 at 18:38
  • 1
    oh i read the docs about that, sorry I didn't make it clear in the question (i'll go do so now), but is there a way to make it respond to multiple message using that method? – brandone Oct 15 '18 at 18:39
  • 1
    No, for that you would write a function that takes a message and validates the content (returning `True` or `False`), then pass that function as `check=` – Patrick Haugh Oct 15 '18 at 18:41
  • What do you mean by responds to multiple messages? So if the user says one thing the bot has one response, but if they say something else the bot has a different response? – Patrick Haugh Oct 15 '18 at 18:42
  • oh... sorry I don't understand '^_^ can you explain it in a more... "i'm new to python" way? – brandone Oct 15 '18 at 18:42
  • yes that's what I mean – brandone Oct 15 '18 at 18:42
  • well sort of. so say if I said "good morning" to the bot, it would respond with "same to you", then if I said "thanks" I need to make it so it responds with "no problem" – brandone Oct 15 '18 at 18:44
  • but it would take many different versions of "thanks", such as "thx", "thankyou" and "how nice of you" – brandone Oct 15 '18 at 18:45

1 Answers1

3

We can write a function that checks for many different variations, then pass that function to wait_for_message as our check argument.

thanks = ['thanks', 'thank you', 'thx']

def thanks_check(message):
    content = message.content.lower()
    return any(t in content for t in thanks)

@bot.event
async def on_message(message):
    content = message.content.lower()
    if '.png' in content or '.jpg' in content or '.jpeg' in content:
                await bot.send_message(message.channel, "woah, very nice!")
                print("Waiting for test")
                msg = await bot.wait_for_message(author=message.author, check=thanks_check)
                if msg:
                    print("test") 
                    await bot.send_message(message.channel, "hey, gotta compliment nice images right?")
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • sadly that didn't work. It didn't print "test" in the shell or send a message. No errors thrown up either – brandone Oct 15 '18 at 18:57
  • @brandone let's make sure that we're actually waiting for the message. Could you add a `print` before the `wait_for_message`? – Patrick Haugh Oct 15 '18 at 19:05
  • oh it's not printing anything... so does that mean the program is just ignoring everything past the "woah, very nice!"? – brandone Oct 15 '18 at 20:09
  • the if statement for the png jpg and stuff is in an @bot.event function containing A LOT of other stuff, so not sure if that might be causing the problem – brandone Oct 15 '18 at 20:27
  • Very possible. Try writing a small test bot to understand this functionality before integrating it with your existing code. – Patrick Haugh Oct 15 '18 at 20:34
  • and it responded on the server as well – brandone Oct 15 '18 at 21:16
  • and i figured out the problem. The image I was testing with was classed as url, instead of png. Wow, i'm so sorry. It works fine now, thanks a lot! – brandone Oct 15 '18 at 21:20
  • but just a small question. How would you make it so after a set amount of time, if the user doesn't respond, then the bot would ignore any thanks messages afterwards. So say if I were to send an image, the bot responds with "woah very nice", then instead of me responding with "thanks" I just ignore it and I don't say anything. How would I make it so it would give up on any sort of response. – brandone Oct 15 '18 at 21:31
  • right now it waits for a response as soon as I put in a image and then pretty much waits forever until I shut the bot down – brandone Oct 15 '18 at 21:32
  • You can pass a `timeout` argument to `wait_for_message` – Patrick Haugh Oct 15 '18 at 21:37
  • ahh ok, didn't even realise that was a thing, thanks – brandone Oct 15 '18 at 21:49
  • also is it possible to make it so that if u say thanks twice like "thanks thx" it still responds but only once? – brandone Oct 15 '18 at 21:59
  • That should be the current behaviour. – Patrick Haugh Oct 15 '18 at 21:59
  • and can you explain the "t in content for t in thanks" part of the define function plz? – brandone Oct 15 '18 at 22:01
  • See: https://stackoverflow.com/questions/6531482/how-to-check-if-a-string-contains-an-element-from-a-list-in-python – Patrick Haugh Oct 15 '18 at 22:04
  • Question, I used this method for another purpose. But the bot will respond to the word "no". But if a user responds with the word "know" or any other words which contain "no". The bot responds. How do I prevent this from happening? – brandone Oct 19 '18 at 18:04
  • Instead `t in content`, we need to split content up into words first. One way to do that would be `content = message.content.lower().split()`. Note that punctuation could mess this up still: `"no."` and `"no"` are two different strings. You could do `content = list(map(lambda s: s.strip('?.!'), message.content.lower().split()))` to try and strip the punctuation as well. – Patrick Haugh Oct 19 '18 at 18:53