gpt4 book ai didi

Python 找不到 "__main__"模块

转载 作者:行者123 更新时间:2023-12-04 17:28:16 25 4
gpt4 key购买 nike

当我尝试运行我的代码时出现此错误:\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\python.exe: can't find '\_\_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')

最佳答案

非常简单的修复。您包含了两个 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 函数中删除它,那里不需要它。

关于Python 找不到 "__main__"模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61926967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com