Probably a very easy question to answer, but I want to send a message to a user after a certain time. For this I read his ID from a JSON. Problem with this? You cannot append a send
to a str
or this error is then spit out.
I have already tried to convert the whole thing, but without success. Can someone give me a hint here?
The code:
JavaScript
x
14
14
1
@commands.Cog.listener()
2
async def on_ready(self):
3
period = 10 # 86400.0
4
while 3:
5
with open('work_data/statues.json') as j:
6
u_data = json.load(j)
7
for user in list(u_data.keys()): # read user, ID comes out if you do print(user)
8
if dt.datetime.now().timestamp() - u_data[user][0]['last_seen'] >= period:
9
await user.send(f"{user}, you haven't shown up at work for 24 hours and have been fired.") # part that doesn't work
10
await asyncio.sleep(2)
11
u_data.pop(user)
12
write_json(u_data)
13
await asyncio.sleep(0.1)
14
The JSON:
JavaScript
1
8
1
{
2
"30288509151923XXXX": [ # Last 4 numbers removed because privacy.
3
{
4
# rest not relevant
5
}
6
]
7
}
8
The error message:
JavaScript
1
8
1
Ignoring exception in on_ready
2
Traceback (most recent call last):
3
File "C:UsersMikePycharmProjectsMikeBotvenvlibsite-packagesdisnakeclient.py", line 505, in _run_event
4
await coro(*args, **kwargs)
5
File "C:UsersMikePycharmProjectsMikeBotcogswork.py", line 308, in on_ready
6
await user.send(f"{user}, you haven't shown up at work for 24 hours and have been fired.")
7
AttributeError: 'str' object has no attribute 'send'
8
Advertisement
Answer
It looks like you are using disnake
instead of discord.py
.
You can have a look at their docs. Here we learn something about fetch_user, get_user or getch_user which will first check the cache and then fetch the user if needed.
In your example, you can use the following:
JavaScript
1
2
1
await self.bot.getch_user(user) # asynchronous, needs to be awaited
2