from typing import Final
import os
from dotenv import load_dotenv
from discord import Intents, Client, Message
from responses import get_response
load_dotenv()
TOKEN: Final[str] = os.getenv('DISCORD_TOKEN')
intents : Intents = Intents.default()
intents.message_content = True #NOQA
client: Client = Client(intents=intents)
async def send_message(message: Message, user_message: str) -> None:
if not user_message:
print("Message was empty because intents were probably not enabled")
return
if is_private := user_message[0] == "?":
user_message = user_message[1:]
try:
response: str = get_response(user_message)
await message.author.send(response) if is_private else await message.channel.send(response)
except Exception as e:
print(e)
# handling startup
@client.event
async def on_ready() -> None:
print(f"{client.user} is now running!")
@client.event
async def on_message(message: Message) -> None:
if message.author == client.user:
return
username: str = str(message.author)
user_message: str = message.content
channel: str = str(message.channel)
print(f"[{channel}] {username}: {user_message}")
await send_message(message, user_message)
#MAIN ENTRY POINT
def main() -> None:
client.run(token=TOKEN)
if __name__ == "__main__":
main()
#π Python code cant import function from different python file in the same folder
30 messages Β· Page 1 of 1 (latest)
@rigid remnant
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
Traceback (most recent call last):
File "c:\Users\Public.DESKTOP-G9TTK92\Documents\discord\main.py", line 5, in <module>
from responses import get_response
ImportError: cannot import name 'get_response' from 'responses' (c:\Users\Public.DESKTOP-G9TTK92\Documents\discord\responses.py)
Ive tried to find a solution and may suck at finding it, but I couldnt figure out why it cant import it
VSCode auto suggests both the file and function so it does ecognize that it exists
def get_response(user_input: str) -> str:
lowered: str = user_input.lower()
if lowered == "":
return "Speak up"
elif "hello" in lowered:
return "Hi"
That is literally the only thing in the file?
I would make sure that you're looking at the code that you think you are, and that all the code is saved.
Its a small code because I havent added much functionality to it, but it has everything I have written down
And isn't missing anything Ive had written down
I'd say the code you're think you're running isn't saved, or is a different file from what's actually being run.
The only other possible cause I can see is apparently mismatched OS newline characters can cause this somehow, but VSCode should catch that.
The three main causes of that error from what I can see:
- A circular import
- The name being imported is wrong
- Using the wrong newline character.
If that's the entire content of responses, 1 is out, so that leaves 2 and 3, which could both be caused by running the wrong file or not saving the code.
OMG Im so sorry saving somehow fixed it, I thought python didnt need the files to be manually saved so I didnt do that
The language has nothing to do with that. That's decided entirely by the editor. Some editors autosave, some don't.
VSCode shows a blue dot in the tab if the code is unsaved.
Btw
It's also just a good habit to obsessively hit ctrl+s while writing. I hit ctrl+s so often I accidentally do it on webpages sometimes after typing.
And you're welcome.
Yeah i will remember to do that, I do that when programming in php during school because it doesnt save automatically on my school pc, will make sure to make that my habit now
Thanks again!
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.
π Python code cant import function from different python file in the same folder