Extend close time logic to differentiate between the claimant and other users#1470
Conversation
This allows us to configure the idle time allowed for claiments seperate from tohers.
As we have complicated this logic, we now don't specify exactly how long until the channel will close.
laundmo
left a comment
There was a problem hiding this comment.
I was there in VC watching this be implemented and tested, all seems good to me.
| claimant_last_message_time += timedelta(minutes=constants.HelpChannels.idle_minutes_claimant) | ||
|
|
||
| # The further away closing time is what we should use. | ||
| closing_time = max(claimant_last_message_time, last_message_time) |
There was a problem hiding this comment.
This breaks the priority that deleted_idle_minutes has because idle_minutes_claimant is configured to be greater than deleted_idle_minutes by default.
There was a problem hiding this comment.
If _message.is_empty(channel) returns True, this indicates that there isn't a message in the channel. So both cache's would be empty. This would mean we enter the if statement and return before this code is hit.
There was a problem hiding this comment.
That is incorrect because the cache doesn't get cleared when the channel goes dormant. is_empty doesn't mean the channel has no messages at all; it basically means the channel's latest message is the bot's embed.
There was a problem hiding this comment.
I added code to clear both caches when the channel is unclaimed in a recent commit, so would that mean it's now correct?
https://github.com/python-discord/bot/pull/1470/files#diff-c31098978b8d838eadb52b2bf23494072cc66686e738d7f99c4f59e6495bfe6fR380-R381
There was a problem hiding this comment.
Maybe, but is there a way to fix it without relying on the cache being cleared?
There was a problem hiding this comment.
How do you feel about returning early, within the if await _message.is_empty(channel): block?
|
Has it been decided to "take the hit" when the bot is offline and can't cache messages that get sent? |
|
@MarkKoz might need to add a condition to the old method fallback, that checks for bot uptime smaller than idle_minutes_claimant so that there is at least that much time after a restart after which channels wont be unduly closed. that is, if i understood correctly what you were referring to. |
Issue with this breaking other functionality have been brought up.
What I was referring to is the cache not having the latest times due to the bot going offline and more messages being sent in channels. Upon going online again, the bot may prematurely close channels due to the outdated cache leading to the perception that the channels have been inactive. I don't understand what you're proposing. |
|
@MarkKoz as it stands currently, it will fall back to the old behavior if the cache isn't available (empty/ returns None) I am proposing extending the fallback conditions to always use the old method for a while after startup, as it does not rely on cache. This way, instead of channels being closed prematurely they might take a bit longer to close. |
The unanswered cache was previously just a boolen of whether a non-claimant every replied to a help channel. With us now needing to know the time at which a non-claimant messaged in a given channel, we infer the answered status from this cache instead.
Also updates the doc string to reflect this new behaviour.
Yes, that could work. It'd have to reschedule it for the maximum time. |
What about clearing the caches on init, so the bot uses the fallback on startup, until the caches have had time to populate? |
| if claimed_timestamp: | ||
| claimed = datetime.utcfromtimestamp(claimed_timestamp) | ||
| return datetime.utcnow() - claimed | ||
| claimed = datetime.fromtimestamp(claimed_timestamp) |
There was a problem hiding this comment.
discord.py still returns times in UTC; they're just naïve. It uses utcfromtimestamp. Sorry if my previous comment was misleading.
…ession has yet to be answered.
datetime.min cannot be converted to a timestamp as it's pre-epoch. Instead wait until we actuall need it and then create the correct datetime object depending on teh cache contents.
|
I'm trying this out with the following config: idle_minutes_claimant: 2
idle_minutes_others: 1
deleted_idle_minutes: 1I simply claim a channel, and it correctly schedules it to close in 2 minutes. However, when the closing task runs, it calculates a wrong time. The log timestamps on the left are in UTC-7. Converted to UTC the times would be around |
Previously we were using `utcfromtimestamp()` which would compensate the timestamp when converting to UTC even though the timestamp itself was in UTC: >>> datetime.utcnow() datetime.datetime(2021, 3, 26, 22, 8, 47, 441603) >>> a = datetime.utcnow().timestamp() 1616821624.207364 >>> a = datetime.utcfromtimestamp(a) datetime.datetime(2021, 3, 27, 5, 7, 4, 207364) By switching to `fromtimestamp()` this avoids that behaviour.
It has some API changes, so it's best to update now before the project starts using the library more.
Fix issues converting timestamps to datetimes and vice-versa. The main culprit id `datetime.timestamp()`, which always assumes naïve objects are in local time. That behaviour conflicts with discord.py, which returns naïve objects in UTC rather than local time. Switching from `utcfromtimestamp` to `fromtimestamp` was incorrect since the latter also assumes the timestamp is in local time.
jb3
left a comment
There was a problem hiding this comment.
Code looks good and functionality works, just a few documentation things.
| """ | ||
| Report stats for a completed help session channel `channel_id`. | ||
|
|
||
| Set `is_auto` to True if the channel was automatically closed or False if manually closed. |
There was a problem hiding this comment.
This docstring needs updating with closed_on and the potential values.
Closes #1451
This extends the closing logic for help channels to closed based on the claimant's last message time and last message in general.
This results in the help channel closing time being reduced if only others are chatting, and the claimant has already left.
Thanks to laundmo, Darr, Griff, LX and Hemlock for the group coding on this PR :D