(rooms): Add interactive lock.

Add an interactive lock so users cannot have multiple menus open simultaneously.
This commit is contained in:
2021-09-28 14:45:25 +03:00
parent 8e86da4abf
commit 906dc70e3a

View File

@@ -2,6 +2,7 @@ import re
import datetime
import discord
import asyncio
import contextlib
from cmdClient.checks import in_guild
from meta import client
@@ -38,6 +39,26 @@ def time_format(time):
time.timestamp() + 3600,
)
user_locks = {} # Map userid -> ctx
@contextlib.contextmanager
def ensure_exclusive(ctx):
"""
Cancel any existing exclusive contexts for the author.
"""
old_ctx = user_locks.pop(ctx.author.id, None)
if old_ctx:
[task.cancel() for task in old_ctx.tasks]
user_locks[ctx.author.id] = ctx
try:
yield
finally:
new_ctx = user_locks.get(ctx.author.id, None)
if new_ctx and new_ctx.msg.id == ctx.msg.id:
user_locks.pop(ctx.author.id)
@module.cmd(
name="rooms",
@@ -101,6 +122,7 @@ async def cmd_rooms(ctx):
valid = valid and (re.search(multiselect_regex, msg.content) or msg.content.lower() == 'c')
return valid
with ensure_exclusive(ctx):
try:
message = await ctx.client.wait_for('message', check=check, timeout=60)
except asyncio.TimeoutError:
@@ -239,6 +261,7 @@ async def cmd_rooms(ctx):
valid = valid and (re.search(multiselect_regex, msg.content) or msg.content.lower() == 'c')
return valid
with ensure_exclusive(ctx):
try:
message = await ctx.client.wait_for('message', check=check, timeout=60)
except asyncio.TimeoutError: