(rooms): Add interactive lock.
Add an interactive lock so users cannot have multiple menus open simultaneously.
This commit is contained in:
@@ -2,6 +2,7 @@ import re
|
|||||||
import datetime
|
import datetime
|
||||||
import discord
|
import discord
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import contextlib
|
||||||
from cmdClient.checks import in_guild
|
from cmdClient.checks import in_guild
|
||||||
|
|
||||||
from meta import client
|
from meta import client
|
||||||
@@ -38,6 +39,26 @@ def time_format(time):
|
|||||||
time.timestamp() + 3600,
|
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(
|
@module.cmd(
|
||||||
name="rooms",
|
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')
|
valid = valid and (re.search(multiselect_regex, msg.content) or msg.content.lower() == 'c')
|
||||||
return valid
|
return valid
|
||||||
|
|
||||||
|
with ensure_exclusive(ctx):
|
||||||
try:
|
try:
|
||||||
message = await ctx.client.wait_for('message', check=check, timeout=60)
|
message = await ctx.client.wait_for('message', check=check, timeout=60)
|
||||||
except asyncio.TimeoutError:
|
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')
|
valid = valid and (re.search(multiselect_regex, msg.content) or msg.content.lower() == 'c')
|
||||||
return valid
|
return valid
|
||||||
|
|
||||||
|
with ensure_exclusive(ctx):
|
||||||
try:
|
try:
|
||||||
message = await ctx.client.wait_for('message', check=check, timeout=60)
|
message = await ctx.client.wait_for('message', check=check, timeout=60)
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
|
|||||||
Reference in New Issue
Block a user