feat(bot): ✨ Migrate tasks to separate containers and readd support for automatic call hangup and account deletion.
This commit is contained in:
@@ -24,62 +24,9 @@ class Calls(base.Cog):
|
||||
def __init__(self, bot_instance: bot.Payphone):
|
||||
self.bot = bot_instance
|
||||
|
||||
self.hangup_calls.add_exception_type(exceptions.ConfigurationError)
|
||||
# self.hangup_calls.start()
|
||||
|
||||
def cog_unload(self) -> None:
|
||||
self.hangup_calls.cancel()
|
||||
|
||||
return super().cog_unload()
|
||||
|
||||
@tasks.loop(seconds=15)
|
||||
async def hangup_calls(self) -> None:
|
||||
"""Hangs up inactive calls.
|
||||
|
||||
Hangs up inactive calls. Calls are hung up when they either:
|
||||
- Have had no messages and more than one minute has passed since the start of the call.
|
||||
- Have had no messages for the past 15 minutes.
|
||||
|
||||
In case they are hung up, both channels are attempted to be notified. In case of failure,
|
||||
that channel is not notified and hung up silently.
|
||||
"""
|
||||
|
||||
calls = await models.Call.filter(ended_at=None).prefetch_related("channels")
|
||||
|
||||
hung_up_channel_ids = []
|
||||
|
||||
for call in calls:
|
||||
message = (
|
||||
await models.Message.filter(call=call).order_by("-id").limit(1).first()
|
||||
)
|
||||
|
||||
if (
|
||||
message is None
|
||||
and call.started_at < datetime.now(timezone.utc) - timedelta(minutes=1)
|
||||
) or (
|
||||
message is not None
|
||||
and message.created_at
|
||||
< datetime.now(timezone.utc) - timedelta(minutes=15)
|
||||
):
|
||||
for channel in call.channels:
|
||||
hung_up_channel_ids.append(channel.id)
|
||||
|
||||
try:
|
||||
await self.bot.get_partial_messageable(channel.id).send(
|
||||
"☎️ The call was hung up because nobody replied."
|
||||
)
|
||||
|
||||
except errors.DiscordException:
|
||||
pass
|
||||
|
||||
hung_up_channel_ids = [id for id in hung_up_channel_ids if id is not None]
|
||||
|
||||
if len(hung_up_channel_ids) > 0:
|
||||
await models.Channel.filter(id__in=hung_up_channel_ids).update(call_id=None)
|
||||
await self.bot.redis.srem(
|
||||
constants.CHANNELS_ONCALL_SET, *hung_up_channel_ids
|
||||
)
|
||||
|
||||
@commands.command(aliases=["c"])
|
||||
@commands.guild_only()
|
||||
@commands.cooldown(1, 15, commands.BucketType.channel)
|
||||
@@ -811,6 +758,8 @@ class Calls(base.Cog):
|
||||
|
||||
pipe = self.bot.redis.pipeline()
|
||||
|
||||
pipe.set(constants.CALL_IS_ACTIVE % call.id, "", ex=60 * 15)
|
||||
|
||||
pipe.hset(
|
||||
constants.CALL_MESSAGES_TEMPLATE % call.id,
|
||||
message.id,
|
||||
|
||||
Reference in New Issue
Block a user