【问题标题】:Making a discord.py chat filter, giving error "await" is invalid syntax制作 discord.py 聊天过滤器,给出错误“await”是无效语法
【发布时间】:2020-04-06 14:52:24
【问题描述】:

我正在为我的不和谐机器人制作一个聊天过滤器,如果人们说坏话,它会在禁止他们之前警告他们两次。我知道有更好的方法可以做到这一点,但请不要引用它们,因为我不想重新编码我的整个机器人。这是我的聊天过滤器(我已将 cuss 词替换为“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

【问题讨论】:

  • . . . banned {}.".format(message.content,message.author) print 的右括号。 VTC 是一个错字。

标签: python filter bots discord discord.py


【解决方案1】:

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

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

【讨论】:

  • 现在我收到下一个等待命令的错误(等待 channel.send):await channel.send("That was your second warning. You have been kicked until further notice.") 此错误:File "bot.py", line 37 await channel.send("That was your second warning. You have been kicked until further notice.") ^ SyntaxError: invalid syntax(箭头在等待中的“t”上,但由于 cmets 很明显不能格式化它不是:/)
猜你喜欢
  • 2021-08-21
  • 2021-07-27
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
  • 2019-04-02
  • 2019-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多