Создание фильтра чата discord.py, выдающего ошибку "await" с неверным синтаксисом - PullRequest
0 голосов
/ 06 апреля 2020

Я создаю чат-фильтр для своего бота, который будет разобщать людей, и два раза предупредит людей, прежде чем их запретить, если они скажут плохое слово. Я знаю, что есть лучшие способы сделать это, но, пожалуйста, не цитируйте их, поскольку я не хочу перекодировать весь мой бот. Вот мой фильтр чата (я заменил ругательные слова на «цензурированные»):

@bot.event
async def on_message(message):
    global warn_count
    if "censored" in message.content:
        channel = await message.author.create_dm()
        warn_count = warn_count + 1
        if warn_count == 2:
            print("Innapropriate language detected, given two warnings, message: {}, banned {}.".format(message.content,message.author)
            await channel.send("That was your second warning. You have been kicked until further notice.")
            await message.author.ban()
        else:
            print("Innapropriate language detected, by {}, message: {}, warned.".format(message.author,message.content))
            await channel.send("WARNNG: please do not {} it is against our rules. If you do this again you will be kicked until further notice. If you believe this is an error please dm e at wolverhulk13#1112")

    if "censored" in message.content:

        channel = await message.author.create_dm()
        warn_count = warn_count + 1
        if warn_count == 2:
            print("Innapropriate language detected, given two warnings, message: {}, banned {}.".format(message.content,message.author)
            await channel.send("That was your second warning. You have been kicked until further notice.")
            await message.author.ban()
        else:
            print("Innapropriate language detected, by {}, message: {}, warned.".format(message.author,message.content))
            await channel.send("WARNNG: please do not swear it is against our rules. If you do this again you will be kicked until further notice. If you believe this is an error please dm me at wolverhulk13#1112")

    if "censored" in message.content:

        channel = await message.author.create_dm()
        warn_count = warn_count + 1
        if warn_count == 2:
            print("Innapropriate language detected, given two warnings, message: {}, banned {}.".format(message.content,message.author))
            await channel.send("That was your second warning. You have been kicked until further notice.")
            await message.author.ban()
        else:
            print("Innapropriate language detected, by {}, message: {}, warned.".format(message.author,message.content))
            await channel.send("WARNNG: please do not swear it is against our rules. If you do this again you will be kicked until further notice. If you believe this is an error please dm me at wolverhulk13#1112")

    if "censored" in message.content:

        channel = await message.author.create_dm()
        warn_count = warn_count + 1
        if warn_count == 2:
            print("Innapropriate language detected, given two warnings, message: {}, banned {}.".format(message.content,message.author))
            channel.send("That was your second warning. You have been kicked until further notice.")
            await message.author.ban()
        else:
            print("Innapropriate language detected, by {}, message: {}, warned.".format(message.author,message.content))
            await channel.send("WARNNG: please do not swear it is against our rules. If you do this again you will be kicked until further notice. If you believe this is an error please dm me at wolverhulk13#1112")

    if "censored" in message.content.split(" "):

        channel = await message.author.create_dm()
        warn_count = warn_count + 1
        if warn_count == 2:
            print("Innapropriate language detected, given two warnings, message: {}, banned {}.".format(message.content,message.author))
            await channel.send("That was your second warning. You have been kicked until further notice.")
            await message.author.ban()
        else:
            print("Innapropriate language detected, by {}, message: {}, warned.".format(message.author,message.content))
            await channel.send("WARNNG: please do not swear it is against our rules. If you do this again you will be kicked until further notice. If you believe this is an error please dm me at wolverhulk13#1112")

    if "censored" in message.content.split(" "):
        warn_count = warn_count + 1
        channel = await message.author.create_dm()
        if warn_count == 2:
            print("Innapropriate language detected, given two warnings, message: {}, banned {}.".format(message.content,message.author))
            await channel.send("That was your second warning. You have been kicked until further notice.")
            await message.author.ban()
        else:
            await channel.send("WARNNG: please do not swear it is against our rules. If you do this again you will be kicked until further notice. If you believe this is an error please dm me at wolverhulk13#1112")
            print("Innapropriate language detected, by {}, message: {}, warned.".format(message.author,message.content))

    if "censored" in message.content:

        channel = await message.author.create_dm()
        warn_count = warn_count + 1
        if warn_count == 2:
            print("Innapropriate language detected, given two warnings, message: {}, banned {}.".format(message.content,message.author))
            await channel.send("That was your second warning. You have been kicked until further notice.")
            await message.author.ban()
        else:
            print("Innapropriate language detected, by {}, message: {}, warned.".format(message.author,message.content))
            await channel.send("WARNNG: please do not swear it is against our rules. If you do this again you will be kicked until further notice. If you believe this is an error please dm me at wolverhulk13#1112")

    if "censored" in message.content:

        channel = await message.author.create_dm()
        warn_count = warn_count + 1
        if warn_count == 2:
            print("Innapropriate language detected, given two warnings, message: {}, banned {}.".format(message.content,message.author))
            await channel.send("That was your second warning. You have been kicked until further notice.")
            await message.author.ban()
        else:
            print("Innapropriate language detected, by {}, message: {}, warned.".format(message.author,message.content))
            await channel.send("WARNNG: please do not swear it is against our rules. If you do this again you will be kicked until further notice. If you believe this is an error please dm me at wolverhulk13#1112")

    if "censored" in message.content:

        channel = await message.author.create_dm()
        warn_count = warn_count + 1
        if warn_count == 2:
            print("Innapropriate language detected, given two warnings, message: {}, banned {}.".format(message.content,message.author))
            await channel.send("That was your second warning. You have been kicked until further notice.")
            await message.author.ban()
        else:
            print("Innapropriate language detected, by {}, message: {}, warned.".format(message.author,message.content))
            await channel.send("WARNNG: please do not swear it is against our rules. If you do this again you will be kicked until further notice. If you believe this is an error please dm me at wolverhulk13#1112")


    if "censored" in message.content:
        channel = await message.author.create_dm()
        warn_count = warn_count + 1
        if warn_count == 2:
            print("Innapropriate language detected, given two warnings, message: {}, banned {}.".format(message.content,message.author))
            await channel.send("That was your second warning. You have been kicked until further notice.")
            await message.author.ban()
        else:
            print("Innapropriate language detected, by {}, message: {}, warned.".format(message.author,message.content))
            await channel.send("WARNNG: please do not swear it is against our rules. If you do this again you will be kicked until further notice. If you believe this is an error please dm me at wolverhulk13#1112")

    else:
        global message_counter
        message_counter = message_counter + 1
        print(message_counter)

    await bot.process_commands(message)

возвращает ошибку: File "bot.py", line 25 await channel.send("That was your second warning. You have bee--- ^ SyntaxError: invalid syntax

1 Ответ

0 голосов
/ 06 апреля 2020

Не забудьте прислать только минимальный воспроизводимый пример. Нас интересует только часть кода, который вызывает проблему, здесь это строка 25. Вам не нужно больше ничего отправлять, когда говорите об синтаксической ошибке.

Об этой синтаксической ошибке: при обнаружении синтаксиса Ошибки, python упоминает всю строку, так что упоминание о том, что в названии это await, к сожалению, тоже не помогает. Хорошая работа для отправки всего сообщения об ошибке в сообщении.

О самой ошибке:

Синтаксическая ошибка может быть найдена здесь:

if "censored" in message.content:

        channel = await message.author.create_dm()
        warn_count = warn_count + 1
        if warn_count == 2:
            print("Innapropriate language detected, given two warnings, message: {}, banned {}.".format(message.content,message.author) # close the parenthesis here
            await channel.send("That was your second warning. You have been kicked until further notice.")
            await message.author.ban()
        else:
            print("Innapropriate language detected, by {}, message: {}, warned.".format(message.author,message.content))
            await channel.send("WARNNG: please do not swear it is against our rules. If you do this again you will be kicked until further notice. If you believe this is an error please dm me at wolverhulk13#1112")

А именно, изменить эта строка:

print("Innapropriate language detected, given two warnings, message: {}, banned {}.".format(message.content,message.author)

до

print("Innapropriate language detected, given two warnings, message: {}, banned {}.".format(message.content,message.author))

Вы закрыли только оператор форматирования, но не оператор печати. ​​

...