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

Update counting.py #267

Closed
Closed
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
31 changes: 19 additions & 12 deletions cogs/commands/counting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
from discord import User
from discord.ext import commands
from discord.ext.commands import Bot, BucketType, Cog, Context, cooldown
from discord.errors import NotFound
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, is_decimal, get_name_string

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,39 @@ 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_player replaced by last_message, as it contains reference to last player, as well as extra info for checking correctness
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
# Otherwise, try and retreive the last message
try:
await ctx.fetch_message(last_message.id)
# If the last message was retreived, no error occured, so this was a genuine mistake
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 last message count be found, an error is thrown, and the message was deleted - name/shame
ctx.send(f"Oops! {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