簡體   English   中英

python 找不到“__main__”模塊

[英]python can't find “__main__” module

當我嘗試運行我的代碼時,我收到此錯誤 \AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\python.exe: can't find '__main__' module in ''

我不知道如何解決它。 我看過與我類似的帖子,但他們的修復似乎不起作用。 這是我所有的代碼

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')

非常簡單的修復。 您已包含兩個 client.events。 discord.py 不允許這樣做,人們最終會遇到最隨機的錯誤。 我也有這個問題有一段時間了。 您需要指定 discord 機器人正在尋找什么。 這是調整后的代碼,您可以根據需要進行更改。 在代碼的末尾,您需要添加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 function 中刪除它,那里不需要它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM