Skip to content

Counting game #82

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
78 changes: 78 additions & 0 deletions cogs/counting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import asyncio
import os
import random
import time
from datetime import datetime, timezone

import akinator as ak
import disnake
import requests
from discord_together import DiscordTogether
from disnake import Embed, ApplicationCommandInteraction, Member
from disnake.ext import commands
from disnake.utils import utcnow
from dotenv import load_dotenv

from utils.CONSTANTS import morse
from utils.assorted import renderBar
from utils.bot import OGIROID
from utils.http import HTTPSession
from utils.shortcuts import errorEmb


class CountingGame(commands.Cog):
# The idea of this game is that one user types 1 and the next user types 2 and so on.
# If someone types a number that is not the next number in the sequence, they lose.
# The game ends when someone types the wrong number or when the game is stopped with the stop command.
# It's only in one channel, say for example #counting with the ID 1234567890.
# The game is always started and no commands are needed to start it.
# Also, the game is always running, so if someone types 1, the next person to type a number will be 2.

def __init__(self, bot: OGIROID):
self.bot = bot
self.count = 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont store state in memory. We have a database, use it

self.channel = None

@commands.Cog.listener()
async def on_ready(self):
self.channel = self.bot.get_channel(986722646135824465)
# Restore the history from the Discord channel
async for message in self.channel.history(limit=10):
if message.author.bot:
continue
if message.content.isdigit():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strip the message

self.count = int(message.content)

@commands.Cog.listener()
async def on_message(self, message: disnake.Message):
if message.channel == self.channel:
if message.author.bot or not message.content.isdigit():
return
messages = await self.channel.history(limit=1).flatten()
last_message = messages[1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just... use the message arg passed into the function

print(f"{last_message.author.name}: {last_message.content}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove debug prints

if last_message.author == message.author and last_message.content.isdigit():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate isdigit calls

embed = Embed(
title="Counting Game",
description=f"{message.author.mention}, you can't do this alone!",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no reason to not support an author sending multiple consecutive msgs

color=0xFF0000,
)
await self.channel.send(embed=embed)
self.count = 1
elif message.content == str(self.count):
self.count += 1
# Add checkmark reaction
await message.add_reaction("✅")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uh- I would have this be set for a channel. If the content isn't a num. delete it. No reactions

else:
embed = Embed(
title="Counting Game",
description=f"{message.author.mention} lost the game! Someone type 1 to start a new game.",
color=0xFF0000,
)
await self.channel.send(embed=embed)
self.count = 1
# TODO Implement leaderboard with db
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo ;-;



def setup(bot):
bot.add_cog(CountingGame(bot))