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:
@commands.Cog.listener()
async def on_ready(self):
period = 10 # 86400.0
while 3:
with open('work_data/statues.json') as j:
u_data = json.load(j)
for user in list(u_data.keys()): # read user, ID comes out if you do print(user)
if dt.datetime.now().timestamp() - u_data[user][0]['last_seen'] >= period:
await user.send(f"{user}, you haven't shown up at work for 24 hours and have been fired.") # part that doesn't work
await asyncio.sleep(2)
u_data.pop(user)
write_json(u_data)
await asyncio.sleep(0.1)
The JSON:
{
"30288509151923XXXX": [ # Last 4 numbers removed because privacy.
{
# rest not relevant
}
]
}
The error message:
Ignoring exception in on_ready
Traceback (most recent call last):
File "C:UsersMikePycharmProjectsMikeBotvenvlibsite-packagesdisnakeclient.py", line 505, in _run_event
await coro(*args, **kwargs)
File "C:UsersMikePycharmProjectsMikeBotcogswork.py", line 308, in on_ready
await user.send(f"{user}, you haven't shown up at work for 24 hours and have been fired.")
AttributeError: 'str' object has no attribute 'send'
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:
await self.bot.getch_user(user) # asynchronous, needs to be awaited