Skip to content

Commit e4720ff

Browse files
authored
Merge pull request #268 from Anonyyymous/master
Updated formatting/fixed counting bug
2 parents 48dca64 + a75d1b3 commit e4720ff

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

cogs/commands/counting.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
from decimal import Decimal
44

55
from discord import User
6+
from discord.errors import NotFound
67
from discord.ext import commands
78
from discord.ext.commands import Bot, BucketType, Cog, Context, cooldown
89
from sqlalchemy.exc import SQLAlchemyError
910

1011
from models import db_session
1112
from models.counting import CountingRun, CountingUser
1213
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
1415

1516
LONG_HELP_TEXT = """
1617
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):
6364
# Dict for users to correct replies
6465
# NB no points for the first user - the initial message cannot be wrong
6566
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
6869

6970
while self.currently_playing:
7071
# Wait for the next numeric message sent by a different person in the same channel
7172
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
7374

7475
msg = await self.bot.wait_for("message", check=check_dec_player)
75-
last_player = msg.author
7676
value = Decimal(msg.content)
7777
if msg.author.id not in players:
7878
players[msg.author.id] = 0
7979
if value == count + step:
8080
# If the number is correct, increase the count and length.
8181
count += step
8282
length += 1
83+
last_message = msg
8384
players[msg.author.id] += 1
8485
await msg.add_reaction("✅")
8586
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}")
9399

94100
# Save this run to the database
95101
ended_at = datetime.utcnow()

0 commit comments

Comments
 (0)