G
G
Goshujin2021-02-15 02:14:45
Python
Goshujin, 2021-02-15 02:14:45

The bot ignores the trigger if there is a space in the message, how to fix it?

I want to make triggers to which the bot will respond with a message, it worked, but if the message for which there is a space, then the bot does not see it, how to fix it?

GoodNight = ['Доброй ночи']
BotMen = ['Бот', 'Bot']

@Bot.event
async def on_message(message):
    if message.author == Bot.user:
        return
    else:
        content = message.content.split()
        for word in content:
            if word in GoodNight:
                await message.channel.send('Ты уже уходишь или это ночное приветствие? %s' % Bot.get_emoji(755856809436250253))
            if word in BotMen:
                await message.channel.send('Да, я вас слушаю %s' % Bot.get_emoji(810645595089535087))
    await Bot.process_commands(message)

6029aed4dbe0d410941836.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
David on the headstock, 2021-02-15
@Goshujin

The best solution would be:

goodNight = ['доброй ночи']
botMen = ['бот', 'bot']
# пишем все строки в нижнем регистре 
# (маленькими буквами) для корректного сравнения в будущем


@Bot.event
async def on_message(message, txt: message.content, send: message.channel.send): # упрощаем себе жизнь
    if message.author == Bot.user:
        return
    else:
        content = " ".join(txt.lower().split()) 
        """
        строкой выше мы из " ДоБрой НочИ " делаем "доброй ночи"
        то есть убираем лишние пробелы в начале и в конце
        и приводим к нижнему регистру для корректного сравнения"""
        if content in goodNight:
            await send('Ты уже уходишь или это ночное приветствие? %s' % Bot.get_emoji(755856809436250253))
        if content in botMen:
            await send('Да, я вас слушаю %s' % Bot.get_emoji(810645595089535087))
    await Bot.process_commands(message)

I don't know what framework you are writing on, but I suspect that you can write return "Good night" in it instead of await message.channel.send('Good night') . And yes, as noted before me, variables are not named with a capital letter, only classes. I recommend taking a small course for the very beginners in python. I think it will be useful for you.

S
SH1SH4, 2021-02-15
@SH1SH4

Dude, it's simple, your split splits the list into "Good" and "night", and then there is a check whether it is in the line, but whether it is in the list, and the line "Kind" is not in the list, there is only the line " Good night, I hope I explained it clearly. In short, in this case, you can remove the square brackets in the GoodNight variable (by the way, variables with a capital letter are not called in python) and everything will work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question