python找不到“ __main__”模块

时间:2020-05-21 03:55:22

标签: python discord

当我尝试运行代码时,出现此错误\ AppData \ Local \ Microsoft \ WindowsApps \ PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0 \ python.exe:在''中找不到'__main__'模块

我不知道如何解决它。我看过类似的文章,但他们的修复程序似乎无效。这是我的所有代码

import discord

client = discord.client()

@client.event  
async def on_ready():
    print(f"we have logged in as {client.user}")

client.run("my bot token would be here")

@client.event
async def on_message(message):  # event that happens per any message.

    # each message has a bunch of attributes. Here are a few.
    # check out more by print(dir(message)) for example.
    print(f"{message.channel}: {message.author}: {message.author.name}: {message.content}")


client.run(token)  # recall my token was saved!

@client.event
async def on_message(message):  # event that happens per any message.
    print(f"{message.channel}: {message.author}: {message.author.name}: {message.content}")
    if str(message.author) == "hello" in message.content.lower():
        await message.channel.send('hi')

1 个答案:

答案 0 :(得分:0)

非常简单的修复。您已经包含了两个client.events。 discord.py不允许这样做,并且人们最终会获得最多的随机错误。我也有一段时间了。您需要指定discord bot寻找什么。这是您可以根据需要更改的调整后的代码。 在代码末尾,您需要添加await client.process_commands(message),因为这将确保漫游器考虑所有可能性,并且您的命令以及事件都将起作用。

import discord

client = discord.client()

@client.event  
async def on_ready():
    print(f"we have logged in as {client.user}")

client.run("my bot token would be here")

@client.event
async def on_message(message):  # event that happens per any message.

    # each message has a bunch of attributes. Here are a few.
    # check out more by print(dir(message)) for example.

    if " " in message.content: #Basically it'll work if the message contains " "
        print(f"{message.channel}: {message.author}: {message.author.name}: {message.content}")

    if str(message.author) == "hello" in message.content.lower(): #This is good because you are specifying what the bot should be looking for ("hello")
        await message.channel.send('hi')
    await client.process_commands(message)


client.run(token)  # recall my token was saved!

另一种可能性:client.run应该在代码的末尾,并且只能在末尾。从on_ready函数中删除它,在此不需要它。