制作一个discord.py聊天过滤器,给错误“ await”是无效的语法

问题描述 投票:0回答:1

我正在为我的不和谐机器人制作一个聊天过滤器,该过滤器会警告人们两次,然后禁止他们说错话。我知道有更好的方法可以做到这一点,但是请不要引用它们,因为我不想重新编写整个机器人程序。这是我的聊天过滤器(我将“ cussor”单词替换为“ censored”):

@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

python filter bots discord discord.py
1个回答
0
投票

请记住,仅发送最少的可复制示例。我们只对引起问题的部分代码感兴趣,这里是第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)

to

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

您只关闭了格式语句,但没有关闭打印语句。

© www.soinside.com 2019 - 2024. All rights reserved.