Discord.py机器人自动回复(它检测到自己的消息)

时间:2020-09-27 19:18:03

标签: python discord.py

@client.event
async def on_message(message):
    content = message.content
    author = message.author
    print("{}: {}".format(author, content))
    if message.content.startswith(".play"):
        pass
    elif message.content.startswith(".echo"):
        content = message.content
        #
        """
        msg = message.content.split()
        await message.delete()
        output = ""
        for word in msg[1:]:
            output += word
            output += " "
        if "gay" in output:
            await message.channel.send("Only the gay person is godlessarp")
        else:
            await message.channel.send(output)
        #
        """
        import random as r
        words = ["..."]

        word = r.choice(words)
        user_input = ""
        i = 0
        guesses = 8
        hint = ""
        for i in range(7):
            await message.channel.send("Guess the word!")
            user_input = content
            if guesses > 0:
                if user_input == word:
                    await message.channel.send("YOU FOUND THE WORD!")
                    break
                elif user_input != word:
                    try:
                        await message.channel.send("Incorrect!")
                        hint += word[i]
                        await message.channel.send("Here is the hint: " + hint)
                        i += 1
                        guesses -= 1
                        user_input = ""
                        print(i)
                        print(guesses)
                    except:
                        await message.channel.send("Game Over!")
            elif guesses == 0:
                await message.channel.send("Game Over!")
    elif "<@!753572933594775663>" in content:
        await message.channel.send("```If you need help please mail ariyurektuna@gmail.com```")
        await message.channel.send("```Or you can Dm the high rank members!```")

    elif message.content == ".play":
        pass

当我编写.echo时,它实际上是在玩游戏(它会自己回答)我试图在循环开始时重置内容,但它不起作用。有时它会停止??。另一个问题是我尝试了将此游戏代码放在.play子句上,但是当我在聊天室中键入内容时,它没有响应,也没有给出错误。因此,我评论了.echo代码,并将其替换为游戏代码。现在它可以工作,但仍然是相同的循环...我找不到防止这种情况的方法,请帮忙!

1 个答案:

答案 0 :(得分:3)

如果您不希望漫游器自行回复,则应添加

@client.event
async def on_message(message):
    if message.author.bot:
        return

这,因此如果消息作者是机器人,它将忽略消息。