@@ -75,6 +75,17 @@ def did_you_mean(word, possibilities):
7575 return None
7676
7777
78+ async def wait_for_stop_sign (message : discord .Message , * , replace_with : str ):
79+ def check (reaction , user ):
80+ return reaction .message .id == message .id and str (reaction .emoji ) == "🛑"
81+
82+ try :
83+ await bot .wait_for ("reaction_add" , check = check )
84+ finally :
85+ logger .info (f"replacing message with: { replace_with } " )
86+ await message .edit (content = replace_with )
87+
88+
7889# -----------------------------------------------------------------------------
7990
8091
@@ -370,12 +381,8 @@ async def create_meeting():
370381 },
371382 headers = {"Authorization" : f"Bearer { ZOOM_JWT } " },
372383 )
373- if resp .status >= 400 :
374- logger .error ("could not create meeting" )
375- return None
376- else :
377- data = await resp .json ()
378- return data
384+ resp .raise_for_status ()
385+ return await resp .json ()
379386
380387
381388ZOOM_TEMPLATE = """**Join URL**: {join_url}
@@ -393,23 +400,20 @@ async def zoom_command(ctx: Context):
393400 # Send initial message then edit it so that we don't get an annoying Zoom embed
394401 message = await ctx .send ("Creating meeting..." )
395402 logger .info ("creating zoom meeting" )
396- data = await create_meeting ()
397- if not data :
403+ try :
404+ data = await create_meeting ()
405+ except Exception :
406+ logger .exception ("could not create Zoom meeting" )
398407 await message .edit (
399408 content = "🚨 _Could not create Zoom meeting. That's embarrassing._"
400409 )
401- return
402- content = ZOOM_TEMPLATE .format (join_url = data ["join_url" ], passcode = data ["password" ])
403- await message .edit (content = content , suppress = True )
404-
405- def check (reaction , user ):
406- return reaction .message .id == message .id and str (reaction .emoji ) == "🛑"
410+ else :
411+ content = ZOOM_TEMPLATE .format (
412+ join_url = data ["join_url" ], passcode = data ["password" ]
413+ )
414+ await message .edit (content = content , suppress = True )
407415
408- try :
409- await bot .wait_for ("reaction_add" , check = check )
410- finally :
411- logger .info ("scrubbing zoom info" )
412- await message .edit (content = ZOOM_CLOSED_MESSAGE )
416+ await wait_for_stop_sign (message , replace_with = ZOOM_CLOSED_MESSAGE )
413417
414418
415419@zoom_command .error
@@ -433,26 +437,17 @@ async def zoom_error(ctx, error):
433437
434438
435439def pretty_uuid () -> str :
436- uid = str (uuid .uuid4 ())
437- return base64 .urlsafe_b64encode (uuid .UUID (uid ).bytes ).decode ().replace ("=" , "" )
440+ return base64 .urlsafe_b64encode (uuid .uuid4 ().bytes ).decode ().replace ("=" , "" )
438441
439442
440443@bot .command (name = "meet" , aliases = ("jitsi" ,), help = "Start a Jitsi Meet meeting" )
441444async def meet_command (ctx : Context ):
442- # Send initial message then edit it so that we don't get an annoying Zoom embed
443445 join_url = f"https://meet.jit.si/{ pretty_uuid ()} "
444446 content = MEET_TEMPLATE .format (join_url = join_url )
445447 logger .info ("sending jitsi meet info" )
446448 message = await ctx .send (content = content )
447449
448- def check (reaction , user ):
449- return reaction .message .id == message .id and str (reaction .emoji ) == "🛑"
450-
451- try :
452- await bot .wait_for ("reaction_add" , check = check )
453- finally :
454- logger .info ("scrubbing jitsi meet info" )
455- await message .edit (content = MEET_CLOSED_MESSAGE )
450+ await wait_for_stop_sign (message , replace_with = MEET_CLOSED_MESSAGE )
456451
457452
458453# -----------------------------------------------------------------------------
@@ -464,14 +459,11 @@ async def create_watch2gether(video_url: Optional[str]) -> str:
464459 if payload :
465460 payload ["share" ] = video_url
466461 resp = await client .post ("https://w2g.tv/rooms/create.json" , json = payload )
467- if resp .status >= 400 :
468- logger .error ("could not create watch2gether room" )
469- return None
470- else :
471- data = await resp .json ()
472- stream_key = data ["streamkey" ]
473- url = f"https://w2g.tv/rooms/{ stream_key } "
474- return url
462+ resp .raise_for_status ()
463+ data = await resp .json ()
464+ stream_key = data ["streamkey" ]
465+ url = f"https://w2g.tv/rooms/{ stream_key } "
466+ return url
475467
476468
477469WATCH2GETHER_HELP = """Create a new watch2gether room
@@ -487,16 +479,16 @@ async def create_watch2gether(video_url: Optional[str]) -> str:
487479
488480WATCH2GETHER_TEMPLATE = """{url}
489481🚀 Watch videos together!
490- *As the room creator, you may react to this message with 🛑 to close the room.*
482+ *React to this message with 🛑 to close the room.*
491483"""
492484
493485WATCH2GETHER_WITH_URL_TEMPLATE = """{url}
494486🚀 Watch videos together!
495487Queued video: {video_url}
496- *As the room creator, you may react to this message with 🛑 to close the room.*
488+ *React to this message with 🛑 to close the room.*
497489"""
498490
499- WATCH2GETHER_CLOSE_MESSAGE = "✨ _watch2gether room closed_"
491+ WATCH2GETHER_CLOSED_MESSAGE = "✨ _watch2gether room closed_"
500492
501493
502494@bot .command (
@@ -508,37 +500,29 @@ async def watch2gether_command(ctx: Context, video_url: str = None):
508500 # Send initial message then edit it so that we don't get an embed
509501 message = await ctx .send ("Creating watch2gether room..." )
510502 logger .info ("creating watch2gether meeting" )
511- url = await create_watch2gether (video_url )
512- if not url :
503+ try :
504+ url = await create_watch2gether (video_url )
505+ except Exception :
506+ logger .exception ("could not create watch2gether room" )
513507 await message .edit (
514508 content = "🚨 _Could not create watch2gether room. That's embarrassing._"
515509 )
516510 return
517- template = WATCH2GETHER_WITH_URL_TEMPLATE if video_url else WATCH2GETHER_TEMPLATE
518- content = template .format (url = url , video_url = video_url )
519- await message .edit (
520- content = content ,
521- suppress = True ,
522- allowed_mentions = discord .AllowedMentions (users = True ),
523- )
524-
525- def check (reaction , user ):
526- return (
527- reaction .message .id == message .id
528- and str (reaction .emoji ) == "🛑"
529- and user .id in (ctx .author .id , bot .owner_id )
511+ else :
512+ template = WATCH2GETHER_WITH_URL_TEMPLATE if video_url else WATCH2GETHER_TEMPLATE
513+ content = template .format (url = url , video_url = video_url )
514+ await message .edit (
515+ content = content ,
516+ suppress = True ,
517+ allowed_mentions = discord .AllowedMentions (users = True ),
530518 )
531519
532- try :
533- await bot .wait_for ("reaction_add" , check = check )
534- finally :
535- logger .info ("scrubbing watch2gether info" )
536- await message .edit (content = WATCH2GETHER_CLOSE_MESSAGE )
520+ await wait_for_stop_sign (message , replace_with = WATCH2GETHER_CLOSED_MESSAGE )
537521
538522
539523# -----------------------------------------------------------------------------
540524
541- CODENAMES_CLOSE_MESSAGE = "✨ _Codenames game ended_"
525+ CODENAMES_CLOSED_MESSAGE = "✨ _Codenames game ended_"
542526
543527
544528def make_teams (players ):
@@ -579,7 +563,7 @@ def check(reaction, user):
579563 future .cancel ()
580564 if str (reaction .emoji ) == "🛑" :
581565 logger .info ("scrubbing codenames info" )
582- await message .edit (content = CODENAMES_CLOSE_MESSAGE )
566+ await message .edit (content = CODENAMES_CLOSED_MESSAGE )
583567 break
584568 elif str (reaction .emoji ) in ("👍" , "🔄" ):
585569 if str (reaction .emoji ) == "🔄" :
@@ -641,13 +625,13 @@ async def catchphrase_command(ctx: Context, category: str = None):
641625# Allow cleaning up Zoom, watch2gether, etc. rooms after bot restarts
642626# Need to use on_raw_reaction_add to handle messages that aren't in the cache
643627
644- CLOSE_MESSAGE_MAP = {
628+ CLOSED_MESSAGE_MAP = {
645629 r"Could not create Zoom" : ZOOM_CLOSED_MESSAGE ,
646630 r"zoom\.us" : ZOOM_CLOSED_MESSAGE ,
647631 r"meet\.jit\.si" : MEET_CLOSED_MESSAGE ,
648- r"Could not create watch2gether" : WATCH2GETHER_CLOSE_MESSAGE ,
649- r"Watch videos together" : WATCH2GETHER_CLOSE_MESSAGE ,
650- r"Codenames" : CODENAMES_CLOSE_MESSAGE ,
632+ r"Could not create watch2gether" : WATCH2GETHER_CLOSED_MESSAGE ,
633+ r"Watch videos together" : WATCH2GETHER_CLOSED_MESSAGE ,
634+ r"Codenames" : CODENAMES_CLOSED_MESSAGE ,
651635}
652636
653637
@@ -666,7 +650,7 @@ async def on_raw_reaction_add(payload: discord.RawReactionActionEvent):
666650 if message .id == cached_message .id :
667651 return
668652
669- for pattern , close_message in CLOSE_MESSAGE_MAP .items ():
653+ for pattern , close_message in CLOSED_MESSAGE_MAP .items ():
670654 if re .search (pattern , message .content ):
671655 logger .info (f"cleaning up room with message: { close_message } " )
672656 await message .edit (content = close_message )
0 commit comments