on_message(message)重构此函数可将其认知复杂度从25降低到允许的15。 | Python Discord.py

时间:2020-09-20 09:04:43

标签: python discord.py

我正在制作一个discord.py discord机器人,并希望将3个命令与on_message命令一起使用,这些是我不希望使用前缀的命令。我在网上到处都看过,但是找不到这个问题的答案。

这是我的错误: Screenshot of error in VScode

on_message(message)
Refactor this function to reduce its Cognitive Complexity from 25 to the 15 allowed.

这是我的代码:

@client.event
async def on_message(message):
    #Swear filter
    if message.author.bot: return
    if "herobrine" in message.content.lower() or "bob" in message.content.lower():
        await message.delete()
        consequence = random.randint(0,1)
        if consequence == 0:#Frick or heck meme
            await message.channel.send("Sir, one more frick or heck and i'll show you what sharpness IV looks like.")
            #await message.channel.send(file=discord.File('C:\\Users\\max\\OneDrive\\Desktop\\Code\\Python\\BigBrian\\BigBrian_3\\Version3_1\\swear.jpg'))
            output_war(message.author.name, "Swear_Word_Used")
        elif consequence == 1:#Shut bird meme
            await message.channel.send("Shut.")
            #await message.channel.send(file=discord.File('C:\\Users\\max\\OneDrive\\Desktop\\Code\\Python\\BigBrian\\BigBrian_3\\Version3_1\\shut.png'))
            output_war(message.author.name, "Swear_Word_Used")
        else:
            await client.process_commands(message)
    else:
        #Hello Command
        if message.author.bot: return
    if message.content.lower() == "hello" or message.content.lower() == "hi" or message.content.lower() == "helo" or message.content.lower() == "bonjour":
        hour = datetime.now().hour
        if hour < 9:
            #If its morning
            await message.channel.send(f"Good Morning {message.author.name} :wave:")
            output(message.author.name, "Good Morning")
            await client.process_commands(message)
        elif hour > 16:
            #If its night
            await message.channel.send(f"Good evening {message.author.name} :wave:")
            output(message.author.name, "Good evening")
            await client.process_commands(message)
        elif hour >= 10 and hour <= 15:
            #If its just day
            await message.channel.send(f"Hello, {message.author.name} :wave:")
            output(message.author.name, "Hello")
            await client.process_commands(message)
        else:
            #F in the chat
            if message.author.bot: return
            if message.content.lower() == "f" or message.content.lower() == "F" or message.content.lower() == "ƒ":
                await message.channel.send(message + "in the chat!")
                output(message.author.name, "F")
                await client.process_commands(message)

我正在使用discord.py重写来完成所有这些工作,因此,如果有人可以帮助的话,那就太好了;谢谢!

0 个答案:

没有答案