#๐ why do i get this error AttributeError: 'str' object has no attribute 'year'
50 messages ยท Page 1 of 1 (latest)
@runic mantle
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.
Oh is it datetime.datetime.strftime
yeah
Tldr it returns a string
timeatamp_str is a string, and strings don't have a year attribute, like the error says.
Did you mean to use ctx.created_at?
yeah cause it gets the date of a discord message
but i just need to get the year month day hour minute and second
I mean did you intend to use ctx.created_at where the error is happening instead of timestamp_str.
Why are you using strftime to create a string, then creating other strings after that?
wdym?
You're creating one string at the top that has the date and time, then you create a string that only has date, and then another that only has time.
It seems weird to create three different strings like this.
oh its because i want one string with date (year month day) and another one with time (hour minute second)
Yes. Why are you creating the string at the top though that has the date and time?
is there another way to do it?
Why are you creating it? I can't tell you how to fix it if I don't know why you're creating that string in the first place.
i was creating it to get the date and time
i created it to turn 2024-05-10 10:20:56.020000+00:00 into 2024-05-10 10:20:56
and i then have the 2 string date and time to get the date 2024-05-10 in the date string and time 2024-05-10 10:20:56 in the time string
Get rid of timestamp_str, and replace all uses of it with ctx.created_at.
like this? {ctx.created_at.year}
I think you've misunderstood how formatting dates works. Once timestamp_str is created, you can't easily just extract the year out, because it's a string.
Yes.
ok
another question how can i match this
i keep getting an error ```Traceback (most recent call last):
File "C:\Users\pinhe\discordbot test.venv\Lib\site-packages\discord\app_commands\commands.py", line 827, in _do_call
return await self._callback(self.binding, interaction, **params) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\pinhe\discordbot test\official bot\CogsFolder\AdminAppCommandsCog.py", line 36, in get_message
print(type(int(date)))
^^^^^^^^^
ValueError: invalid literal for int() with base 10: '2024-5-11'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\pinhe\discordbot test.venv\Lib\site-packages\discord\app_commands\tree.py", line 1248, in _call
await command._invoke_with_namespace(interaction, namespace)
File "C:\Users\pinhe\discordbot test.venv\Lib\site-packages\discord\app_commands\commands.py", line 853, in _invoke_with_namespace
return await self._do_call(interaction, transformed_values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\pinhe\discordbot test.venv\Lib\site-packages\discord\app_commands\commands.py", line 846, in _do_call
raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command 'get-msg' raised an exception: ValueError: invalid literal for int() with base 10: '2024-5-11'```
this is the error part
if hour is None or minute is None or second is None:
async with asqlite.connect('database.db') as conn: # connect to database
async with conn.cursor() as cursor:
date = f"{year}-{month}-{day}"
print(type(int(date)))
fetch_server_data = """SELECT message FROM msg_logging WHERE user_id = ? AND server_id = ? AND date = ?"""
await cursor.execute(fetch_server_data,(user.id, interaction.guild.id, int(date)))
data = await cursor.fetchall()
print(data)
if data:
await interaction.response.send_message(data[0])
await conn.close()
await cursor.close()```
this is the part thats giveing error ```date = f"{year}-{month}-{day}"
print(type(int(date)))
fetch_server_data = """SELECT message FROM msg_logging WHERE user_id = ? AND server_id = ? AND date = ?"""
await cursor.execute(fetch_server_data,(user.id, interaction.guild.id, int(date)))
data = await cursor.fetchall()
Why are you doing int(date)?
cause its a string and in my database i specified it had to be a int
bacause i pass an int to the database
so im doing the date = f"{year}-{month}-{day}" to make it like the date column
int doesn't "turn things into integers", on a string it specifically parses base 10 positional notation
(or other bases if you put in a second argument)
2024-5-11 is not base 10 positional notation so applying it doesn't make sense here
if your database is expecting an integer, then you need to work out what format of integer it's actually expecting
my guess is that what this actually is, is the number 1715449775
in other words, a unix timestamp
so that's probably a fairly reasonable guess
but also like, why did you make the time column an integer if you don't know how you're encoding times as integers?
does anything else in your code interact with this database and either put a time in it or obtain a time from it?
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.