创建discord.py聊天筛选器时,给出错误“wait”是无效语法

2024-04-27 04:19:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的discord机器人制作一个聊天过滤器,如果人们说脏话,它会在禁止之前警告人们两次。我知道有更好的方法可以做到这一点,但请不要引用他们,因为我不想重新编码我的整个机器人。这是我的聊天过滤器(我已将脏话替换为“审查”):

@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


Tags: yousendmessageifcountchanneldmcontent
1条回答
网友
1楼 · 发布于 2024-04-27 04:19:28

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

您只关闭了format语句,而没有关闭print语句

相关问题 更多 >