0

I'm doing a command to my discord bot, which sends back a response depending on the users response to the command. I have a while loop, where I go through all possible answers and if none of them are true, then go to else statement. However, every time the loop runs, it returns the answer depending by the first statement (if answer is high) and doesn't actually seem to care about the other elif statements. Does this have something to do with the message.content parts, since they don't seem to work at all? Here's the loop:

EDIT: added the whole event

@commands.Cog.listener()
    async def on_message(self, message):
        if '.highlow' in message.content:
            randomnumber = random.randint(0,100)
            hintnumber = random.randint(0,100)
            channel = message.channel
            await channel.send('Satunnainen luku on valittu väliltä ``0-100``.\n'
                            f'Vihjeesi on ``{hintnumber}``.\n'
                            'Vastaa kirjoittamalla ``high``, ``low`` tai ``vihje``.')

            def check(message):
                return message.content == 'high' or 'low' or 'vihje' and message.channel == channel

            msg = await self.client.wait_for('message', check=check)

            while True:
                if message.author != self.client.user:
                    if 'high' in message.content:
                        if randomnumber > hintnumber:
                            await channel.send(f'Satunnainen luku oli ``{randomnumber}``, joten voitit pelin!'.format(msg))
                            break
                        elif randomnumber < hintnumber:
                            await channel.send(f'Satunnainen luku oli ``{randomnumber}``, joten hävisit pelin!'.format(msg))
                            break
                    elif 'low' in message.content:
                        if randomnumber < hintnumber:
                            await channel.send(f'Satunnainen luku oli ``{randomnumber}``, joten voitit pelin!'.format(msg))
                            break
                        elif randomnumber > hintnumber:
                            await channel.send(f'Satunnainen luku oli ``{randomnumber}``, joten hävisit pelin!'.format(msg))
                            break
                    elif 'vihje' in message.content:
                        if randomnumber == hintnumber:
                            await channel.send(f'Satunnainen luku oli ``{randomnumber}``, joten voitit pelin!'.format(msg))
                            break
                        elif randomnumber != hintnumber:
                            await channel.send(f'Satunnainen luku oli ``{randomnumber}``, joten hävisit pelin!'.format(msg))
                            break
                    else:
                        await channel.send('Tuo ei ole oikea vaihtoehto!')
                        break

The command is in a cog.

tontsa28
  • 73
  • 1
  • 8

1 Answers1

1

The problem is in check(). Your conditional looks incorrect.

message.content == 'high' or 'low' or 'vihje' and message.channel == channel will always be True

It looks to me like you want check() to return True when message.content is one of high, low or vihje, and when message.channel is channel. Unfortunately, your syntax for the first condition is incorrect, it will always evaluate to True because of the truthiness of Python strS. The effect of this is that check() can return True even when message.content is 'pinkalicious' or even None. Rewrite check() as:

def check(message):
    return message.content in {'high', 'low', 'vihje'} and message.channel == channel

Your conditional is actually evaluated as:

(message.content == 'high') or ('low') or ('vihje') and (message.channel == channel)

Since non-empty strS evaluate to True, your conditional is this:

(message.content == 'high') or (True) or (True) and (message.channel == channel)

# Which reduces to
True

If you want to see the truthiness of Python strings for yourself, run the following snippet:

if '':
    print('empty string is True')
else:
    print('empty string is False')

if 'foo':
    print('non-empty string is True')
else:
    print('non-empty string is False')

if 'false':
    print('"false" is True')
else:
    print('"false" is False')
Michael Ruth
  • 2,938
  • 1
  • 20
  • 27
  • I changed the check function to what you suggested, however now I'm getting this error: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions What does this mean exactly? – tontsa28 Mar 03 '21 at 10:19
  • @tontsa28 The error should be self-explanatory: The bot or the user is missing permissions. Have a look at the following: [Error code 50013](https://discord.com/developers/docs/topics/opcodes-and-status-codes#json-json-error-codes) – Dominik Mar 03 '21 at 11:50
  • @Dominik yeah it is self-explanatory, I was just wondering if the permissions are missing from the user or the bot. Managed to fix it, the bot was missing permissions on a private channel. – tontsa28 Mar 03 '21 at 14:29