|
3 | 3 | from decimal import Decimal |
4 | 4 |
|
5 | 5 | from discord import User |
| 6 | +from discord.errors import NotFound |
6 | 7 | from discord.ext import commands |
7 | 8 | from discord.ext.commands import Bot, BucketType, Cog, Context, cooldown |
8 | 9 | from sqlalchemy.exc import SQLAlchemyError |
9 | 10 |
|
10 | 11 | from models import db_session |
11 | 12 | from models.counting import CountingRun, CountingUser |
12 | 13 | from models.user import User as UserModel |
13 | | -from utils import get_database_user_from_id, is_decimal |
| 14 | +from utils import get_database_user_from_id, get_name_string, is_decimal |
14 | 15 |
|
15 | 16 | LONG_HELP_TEXT = """ |
16 | 17 | Starts a counting game where each player must name the next number in the sequence until someone names an invalid number |
@@ -63,33 +64,38 @@ def check_dec(m): |
63 | 64 | # Dict for users to correct replies |
64 | 65 | # NB no points for the first user - the initial message cannot be wrong |
65 | 66 | players = dict() |
66 | | - # Used to make sure someone else replies |
67 | | - last_player = msg.author |
| 67 | + # last_message replaces last_player |
| 68 | + last_message = msg |
68 | 69 |
|
69 | 70 | while self.currently_playing: |
70 | 71 | # Wait for the next numeric message sent by a different person in the same channel |
71 | 72 | def check_dec_player(m): |
72 | | - return check_dec(m) and m.author != last_player |
| 73 | + return check_dec(m) and m.author != last_message.author |
73 | 74 |
|
74 | 75 | msg = await self.bot.wait_for("message", check=check_dec_player) |
75 | | - last_player = msg.author |
76 | 76 | value = Decimal(msg.content) |
77 | 77 | if msg.author.id not in players: |
78 | 78 | players[msg.author.id] = 0 |
79 | 79 | if value == count + step: |
80 | 80 | # If the number is correct, increase the count and length. |
81 | 81 | count += step |
82 | 82 | length += 1 |
| 83 | + last_message = msg |
83 | 84 | players[msg.author.id] += 1 |
84 | 85 | await msg.add_reaction("✅") |
85 | 86 | else: |
86 | | - # Otherwise, break the chain. |
87 | | - await msg.add_reaction("❌") |
88 | | - await ctx.send( |
89 | | - f"Gone wrong at {count}! The next number was {count + step}.\n" |
90 | | - f"This chain lasted {length} consecutive messages." |
91 | | - ) |
92 | | - break |
| 87 | + try: |
| 88 | + # Try to fetch last message, if this causes an error then the message doesnt exist and has been deleted |
| 89 | + await ctx.fetch_message(last_message.id) |
| 90 | + await msg.add_reaction("❌") |
| 91 | + await ctx.send( |
| 92 | + f"Gone wrong at {count}! The next number was {count + step}.\n" |
| 93 | + f"This chain lasted {length} consecutive messages." |
| 94 | + ) |
| 95 | + break |
| 96 | + except NotFound: |
| 97 | + # If the message has been deleted then say so, and continue the game |
| 98 | + await ctx.send(f"Oops. It seems {get_name_string(last_message)} deleted their message. The next number is {count + step}") |
93 | 99 |
|
94 | 100 | # Save this run to the database |
95 | 101 | ended_at = datetime.utcnow() |
|
0 commit comments