Skip to content

Commit b20ff78

Browse files
committed
Hunting - fix listener
1 parent 527bf0c commit b20ff78

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

hunting/hunting.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ async def _wait_for_bang(self, guild: discord.Guild, channel: discord.TextChanne
393393
bang = ["💥", "\N{COLLISION SYMBOL}"]
394394
salute = ["🫡", "\N{SALUTING FACE}"]
395395
animal = random.choice(list(self.animals.keys()))
396-
await channel.send(self.animals[animal])
396+
animal_message = await channel.send(self.animals[animal])
397397

398398
def bang_mcheck(m: discord.Message):
399399
if m.guild != guild or m.channel != channel or not m.content:
@@ -420,18 +420,28 @@ def salute_rcheck(r: discord.Reaction, u: discord.Member):
420420
# Wait for whatever comes first, a message with bang or a reaction with bang emoji
421421
# Use asyncio.FIRST_COMPLETED to return the first completed future
422422
futures: list[asyncio.Future] = []
423-
futures.append(asyncio.ensure_future(self.bot.wait_for("message", check=bang_mcheck, timeout=timeout)))
424-
futures.append(asyncio.ensure_future(self.bot.wait_for("reaction_add", check=bang_rcheck, timeout=timeout)))
423+
futures.append(asyncio.ensure_future(self.bot.wait_for("message", check=bang_mcheck)))
424+
futures.append(asyncio.ensure_future(self.bot.wait_for("reaction_add", check=bang_rcheck)))
425425
if animal == "eagle":
426-
futures.append(asyncio.ensure_future(self.bot.wait_for("message", check=salute_mcheck, timeout=timeout)))
427-
futures.append(
428-
asyncio.ensure_future(self.bot.wait_for("reaction_add", check=salute_rcheck, timeout=timeout))
429-
)
426+
futures.append(asyncio.ensure_future(self.bot.wait_for("message", check=salute_mcheck)))
427+
futures.append(asyncio.ensure_future(self.bot.wait_for("reaction_add", check=salute_rcheck)))
428+
429+
if not conf["bang_words"]:
430+
await animal_message.add_reaction("\N{COLLISION SYMBOL}")
430431

431-
done, pending = await asyncio.wait(futures, return_when=asyncio.FIRST_COMPLETED)
432-
for future in pending:
433-
future.cancel()
432+
try:
433+
done, pending = await asyncio.wait(futures, return_when=asyncio.FIRST_COMPLETED, timeout=timeout)
434+
except asyncio.TimeoutError:
435+
return await channel.send(f"The {animal} flew away!")
436+
try:
437+
for future in pending:
438+
future.cancel()
439+
except Exception as e:
440+
log.info(f"Failed to cancel pending futures: {e}")
441+
if not done:
442+
return await channel.send(f"The {animal} flew away!")
434443
res = done.pop().result()
444+
435445
if isinstance(res, discord.Message):
436446
author: discord.Member = res.author
437447
saluted = False

0 commit comments

Comments
 (0)