Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated formatting/fixed counting bug #268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions cogs/commands/counting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
from decimal import Decimal

from discord import User
from discord.errors import NotFound
from discord.ext import commands
from discord.ext.commands import Bot, BucketType, Cog, Context, cooldown
from sqlalchemy.exc import SQLAlchemyError

from models import db_session
from models.counting import CountingRun, CountingUser
from models.user import User as UserModel
from utils import get_database_user_from_id, is_decimal
from utils import get_database_user_from_id, get_name_string, is_decimal

LONG_HELP_TEXT = """
Starts a counting game where each player must name the next number in the sequence until someone names an invalid number
Expand Down Expand Up @@ -63,33 +64,38 @@ def check_dec(m):
# Dict for users to correct replies
# NB no points for the first user - the initial message cannot be wrong
players = dict()
# Used to make sure someone else replies
last_player = msg.author
# last_message replaces last_player
last_message = msg

while self.currently_playing:
# Wait for the next numeric message sent by a different person in the same channel
def check_dec_player(m):
return check_dec(m) and m.author != last_player
return check_dec(m) and m.author != last_message.author

msg = await self.bot.wait_for("message", check=check_dec_player)
last_player = msg.author
value = Decimal(msg.content)
if msg.author.id not in players:
players[msg.author.id] = 0
if value == count + step:
# If the number is correct, increase the count and length.
count += step
length += 1
last_message = msg
players[msg.author.id] += 1
await msg.add_reaction("✅")
else:
# Otherwise, break the chain.
await msg.add_reaction("❌")
await ctx.send(
f"Gone wrong at {count}! The next number was {count + step}.\n"
f"This chain lasted {length} consecutive messages."
)
break
try:
# Try to fetch last message, if this causes an error then the message doesnt exist and has been deleted
await ctx.fetch_message(last_message.id)
await msg.add_reaction("❌")
await ctx.send(
f"Gone wrong at {count}! The next number was {count + step}.\n"
f"This chain lasted {length} consecutive messages."
)
break
except NotFound:
# If the message has been deleted then say so, and continue the game
await ctx.send(f"Oops. It seems {get_name_string(last_message)} deleted their message. The next number is {count + step}")

# Save this run to the database
ended_at = datetime.utcnow()
Expand Down
Loading