Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python can't find "__main__" module

Tags:

python

discord

When I try to run my code I get this error: \AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\python.exe: can't find '\_\_main\_\_' module in ''

I have no idea how to fix it. I have looked at similar posts to mine but their fixes did not seem to work. Here is all my code:

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')
        
like image 425
NoHaxJustEncrypted Avatar asked Dec 08 '25 08:12

NoHaxJustEncrypted


1 Answers

Very simple fix. You have included two client.events. discord.py does not allow for this and people end up getting the most random errors. I have had this issue for a while as well. You need to specify what the discord bot is looking for. here is the adjusted code that you can change based on your needs. At the end of your code, you need to add await client.process_commands(message) as that will make sure the bot considers all possibilities, and your commands, as well as events, will work.

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!

another possibility: The client.run should be at the end of your code and at the end only. remove it from the on_ready function, it is not needed there.

like image 112
Sathwik Doddi Avatar answered Dec 09 '25 22:12

Sathwik Doddi