fix(timers): More cautious channel ratelimit.

This commit is contained in:
2023-09-12 16:12:29 +03:00
parent 5eba79da82
commit 72ecc2e639

View File

@@ -671,6 +671,7 @@ class Timer:
if repost: if repost:
await self.send_status(delete_last=False, with_notify=False) await self.send_status(delete_last=False, with_notify=False)
@log_wrap(action='Update Channel Name')
async def _update_channel_name(self): async def _update_channel_name(self):
""" """
Submit a task to update the voice channel name. Submit a task to update the voice channel name.
@@ -678,15 +679,19 @@ class Timer:
Attempts to ensure that only one task is running at a time. Attempts to ensure that only one task is running at a time.
Attempts to wait until the next viable channel update slot (via ratelimit). Attempts to wait until the next viable channel update slot (via ratelimit).
""" """
if self._voice_update_task and not self._voice_update_task.done(): if self._voice_update_lock.locked():
# Voice update request already submitted # Voice update is already running
# Note that if channel editing takes a long time,
# and the lock is waiting on that,
# we may actually miss a channel update in this period.
# Erring on the side of less ratelimits.
return return
async with self._voice_update_lock: async with self._voice_update_lock:
if self._last_voice_update: if self._last_voice_update:
to_wait = ((self._last_voice_update + timedelta(minutes=5)) - utc_now()).total_seconds() to_wait = ((self._last_voice_update + timedelta(minutes=5)) - utc_now()).total_seconds()
if to_wait > 0: if to_wait > 0:
self._voice_update_task = asyncio.create_task(asyncio.sleep(to_wait)) self._voice_update_task = asyncio.create_task(asyncio.sleep(to_wait), name='timer-voice-wait')
try: try:
await self._voice_update_task await self._voice_update_task
except asyncio.CancelledError: except asyncio.CancelledError:
@@ -701,8 +706,18 @@ class Timer:
if new_name == self.channel.name: if new_name == self.channel.name:
return return
try:
logger.debug(f"Requesting channel name update for timer {self}")
await self.channel.edit(name=new_name)
except discord.HTTPException:
logger.warning(
f"Voice channel name update failed for timer {self}",
exc_info=True
)
finally:
# Whether we fail or not, update ratelimit marker
# (Repeatedly sending failing requests is even worse than normal ratelimits.)
self._last_voice_update = utc_now() self._last_voice_update = utc_now()
await self.channel.edit(name=self.channel_name)
@log_wrap(action="Stop Timer") @log_wrap(action="Stop Timer")
async def stop(self, auto_restart=False): async def stop(self, auto_restart=False):