ActuallyBlaze
ActuallyBlaze

Reputation: 1

playing around discord.py and ended up in the first problem i cant solve on my own

well, i've been trying to make my first discord bot and i watched this video to help me do a logging script that shows in the terminal, but the responses.py the guy did in the video made the bot always send a message if u didnt send a message that was refered in the responses.py

here's the current code

def get_response(user_input: str) -> str:
    lowered: str = user_input.lower()

if lowered == '':
    return 'Estás muito silencioso, manda alguma coisa.'
elif 'ola' in lowered:
    return 'Olá!'
elif 'como estas' in lowered:
    return 'Bem, obrigado'
elif 'adeus' in lowered:
    return 'Adeus..'
elif 'lança um dado' in lowered:
    return f'Calhou {randint(1, 6)}.'
else:
    return None`

i tried just using "return" or "return None" but it always gives me an error saying "400 Bad Request (error code: 50006): Cannot send an empty message", no matter what i did i also tried calling the function again at the else but it ended up giving me another error, im out of ideas, been stuck on this for the past 1h30m

Upvotes: -1

Views: 42

Answers (1)

keventhen4
keventhen4

Reputation: 443

If you would like to return None or return, you simply need to make sure that, as your error says, you aren't sending an empty message. In this case you don't want to send a message at all, which is perfect!

To prevent sending an empty message, we just need to check if you're returning None or not when you get your response with get_response().

Here's code from the video from a part of main.py but with the if check:

# STEP 2: MESSAGE FUNCTIONALITY
async def send_message(message: Message, user_message: str) -> None:
    if not user_message:
        print('(Message was empty because intents were not enabled probably)')
        return

    if is_private := user_message[0] == '?':
        user_message = user_message[1:]

    try:
        response: str = get_response(user_message)
        if response is not None: #Check if the response is not None to send message, otherwise don't do anything
            await message.author.send(response) if is_private else await message.channel.send(response)
    except Exception as e:
        print(e)

Upvotes: 0

Related Questions