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

[captcha] rewrite #176

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions captchanew/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .captcha import setup as setup
56 changes: 56 additions & 0 deletions captchanew/abc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from abc import ABC, abstractmethod
from typing import Dict, List

import discord
from discapty import Challenge
from redbot.core import Config
from redbot.core.bot import Red
from redbot.core.commands import Cog


class CogABC(ABC):
bot: Red
config: Config

queue: Dict[int, Dict[int, Challenge]]
"""
An internal queue, with:

guild_id/ (int)

├─ user_id/ (int)

│ ├─ Challenge
"""

pending_queue: Dict[int, List[discord.Member]]
"""
A list of pending challenges. The key is the guild's ID, inside, the IDs of pending users.
"""

@abstractmethod
def should_accept_challenge(self, guild: discord.Guild) -> bool:
"""
Determine if the leaving messages should be send.
It is mostly used in case of raiding event.
"""
raise NotImplementedError()

@abstractmethod
async def start_challenge_for_member(self, member: discord.Member) -> Challenge:
raise NotImplementedError()

@abstractmethod
def initiate_patch_note(self):
"""
A method to call once the cog is loaded.
Determine the logic for patchnote sending.
"""
raise NotImplementedError()


class CogMixin(type(Cog), type(ABC)): # type: ignore
"""
Allows the metaclass used for proper type detection to coexist with discord.py's metaclass.
Credit to https://github.com/Cog-Creators/Red-DiscordBot (mod cog) for mixin.
"""
21 changes: 21 additions & 0 deletions captchanew/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import discapty
import discord

from .abc import CogABC
from .config import GuildSettings
from .const import FROM_TYPE_TO_GENERATOR


class CaptchaAPI(CogABC):
def should_accept_challenge(self, guild: discord.Guild) -> bool:
guild_queue = self.queue.get(guild.id)
return len(guild_queue) < 5 if guild_queue else True

async def start_challenge_for_member(self, member: discord.Member) -> discapty.Challenge:
guild = member.guild
config = await GuildSettings.from_guild(guild)
generator = FROM_TYPE_TO_GENERATOR[config.type]()

# TODO: Remove type: ignore at d.py 2.0
challenge = discapty.Challenge(generator, f"{guild.id}-{member.id}", allowed_retries=config.retries) # type: ignore
self.queue
52 changes: 52 additions & 0 deletions captchanew/captcha.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import discord
from redbot.core import Config, commands
from redbot.core.bot import Red

from captchanew.api import CaptchaAPI

from .abc import CogABC, CogMixin
from .commands import Commands
from .meta import __patch_note_version__, __patchnote__
from .migration import run_migration
from .utils import get_config, get_log

log = get_log()


class Captcha(
commands.Cog, CaptchaAPI, Commands, CogABC, metaclass=CogMixin
): # Keep "CogABC" after all others classes (It has its importance)
def __init__(self, bot: Red) -> None:
self.bot = bot

self.config = get_config(with_defaults=True)

async def initiate_patch_note(self) -> None:
await self.bot.wait_until_red_ready()
if not self.bot.user:
raise ValueError()

notice = Config.get_conf(None, identifier=4145125452, cog_name="PredeactorNews")
notice.register_user(version="0")

async with notice.get_users_lock():
await self.bot.wait_until_red_ready()
actual_patch_note_version: str = await notice.user(self.bot.user).version() # type: ignore

if actual_patch_note_version != __patch_note_version__:
# P.N. 2
# Determine if this is the first time the user is using the cog (Not a change
# of repo, see https://github.com/fixator10/Fixator10-Cogs/pull/163)
if __patch_note_version__ == "2" and (not await self.config.was_loaded_once()):
await notice.user(self.bot.user).version.set(__patch_note_version__) # type: ignore
return

await self.bot.send_to_owners(__patchnote__)
await notice.user(self.bot.user).version.set(__patch_note_version__)


def setup(bot: Red):
cog = Captcha(bot)
bot.add_cog(cog)
bot.loop.create_task(run_migration())
bot.loop.create_task(cog.initiate_patch_note())
5 changes: 5 additions & 0 deletions captchanew/challenge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from discapty import Challenge as DiscaptyChallenge


class Challenge(DiscaptyChallenge):
pass
Loading