0

我使用discord.pyPython v3.9.0制作了一个 Hello Bot ,我不希望我的机器人读取来自机器人的任何消息。我怎么做?

我试图在 Stack Overflow 上查看其他问题,但它们都是至少 5 年前的问题。

我已经拥有它,因此它不会读取从自身发送的消息。顺便说一句,我的命令前缀是 '

这是我的代码:

import os
import discord

# * Clear the screen
def clear():
    if os.name == 'nt':
        os.system('cls')
    else:
        os.system('clear')
# * Code
clear()

client = discord.Client()
@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

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

    if message.content.startswith("'hello"):
        await message.channel.send("Hello!")

    if message.content.startswith("'Hello"):
        await message.channel.send("Hello!")

    if message.content.startswith("'HELLO"):
        await message.channel.send("Hello!")

    if message.content.startswith("'Hi"):
        await message.channel.send("Hello!")

    if message.content.startswith("'hi"):
        await message.channel.send("Hello!")

    if message.content.startswith("'HI"):
        await message.channel.send("Hello!")

    if message.content.startswith("'hey"):
        await message.channel.send("Hello!")

    if message.content.startswith("'Hey"):
        await message.channel.send("Hello!")

    if message.content.startswith("'Greetings"):
        await message.channel.send("Hello!")

    if message.content.startswith("'greetings"):
        await message.channel.send("Hello!")
    
    if message.content.startswith("'howdy"):
        await message.channel.send("We're not cowboys!")

    if message.content.startswith("'Howdy"):
        await message.channel.send("We're not cowboys!")

    if message.content.startswith("'Bye"):
        await message.channel.send("Bye!")

    if message.content.startswith("'bye"):
        await message.channel.send("Bye!")

    if message.content.startswith("'BYE"):
        await message.channel.send("Bye!")

# * Runs the code
client.run("my_token")
4

1 回答 1

1

您可以简单地使用discord.Member.bot. 它返回TrueFalse取决于用户是否是机器人。此外,您可以使用str.lower()而不是检查所有大写和小写字母的可能性。

@client.event
async def on_message(message):
    if message.author.bot:
        return
    if message.content.lower().startswith("'hello"):
        await message.channel.send("Hello!")
    if message.content.lower().startswith("'hi"):
        await message.channel.send("Hello!")
    if message.content.lower().startswith("'hey"):
        await message.channel.send("Hello!")
于 2020-11-15T20:24:00.820 回答