Migrate CTC2, closes #93
This commit is contained in:
parent
1726f008b9
commit
da2a64becd
1 changed files with 30 additions and 43 deletions
|
@ -3,16 +3,17 @@ import re
|
|||
from datetime import datetime, timedelta
|
||||
|
||||
import aiohttp
|
||||
from ButtonPaginator import Paginator
|
||||
from discord import Member, User
|
||||
from discord.ext import commands
|
||||
from discord_slash import SlashContext, cog_ext
|
||||
from discord_slash.model import ButtonStyle
|
||||
from dis_snek import InteractionContext, Snek
|
||||
from dis_snek.ext.paginators import Paginator
|
||||
from dis_snek.models.discord.embed import EmbedField
|
||||
from dis_snek.models.discord.user import Member, User
|
||||
from dis_snek.models.snek.application_commands import slash_command
|
||||
from dis_snek.models.snek.command import cooldown
|
||||
from dis_snek.models.snek.cooldowns import Buckets
|
||||
|
||||
from jarvis.db.models import Guess
|
||||
from jarvis.utils import build_embed
|
||||
from jarvis.utils.cachecog import CacheCog
|
||||
from jarvis.utils.field import Field
|
||||
|
||||
guild_ids = [578757004059738142, 520021794380447745, 862402786116763668]
|
||||
|
||||
|
@ -26,7 +27,7 @@ invites = re.compile(
|
|||
class CTCCog(CacheCog):
|
||||
"""J.A.R.V.I.S. Complete the Code 2 Cog."""
|
||||
|
||||
def __init__(self, bot: commands.Bot):
|
||||
def __init__(self, bot: Snek):
|
||||
super().__init__(bot)
|
||||
self._session = aiohttp.ClientSession()
|
||||
self.url = "https://completethecodetwo.cards/pw"
|
||||
|
@ -34,24 +35,21 @@ class CTCCog(CacheCog):
|
|||
def __del__(self):
|
||||
self._session.close()
|
||||
|
||||
@cog_ext.cog_subcommand(
|
||||
base="ctc2",
|
||||
name="about",
|
||||
description="CTC2 related commands",
|
||||
guild_ids=guild_ids,
|
||||
@slash_command(
|
||||
name="ctc2", sub_cmd_name="about", description="CTC2 related commands", scopes=guild_ids
|
||||
)
|
||||
@commands.cooldown(1, 30, commands.BucketType.channel)
|
||||
async def _about(self, ctx: SlashContext) -> None:
|
||||
@cooldown(bucket=Buckets.USER, rate=1, interval=30)
|
||||
async def _about(self, ctx: InteractionContext) -> None:
|
||||
await ctx.send("See https://completethecode.com for more information")
|
||||
|
||||
@cog_ext.cog_subcommand(
|
||||
base="ctc2",
|
||||
name="pw",
|
||||
description="Guess a password for https://completethecodetwo.cards",
|
||||
guild_ids=guild_ids,
|
||||
@slash_command(
|
||||
name="ctc2",
|
||||
sub_cmd_name="pw",
|
||||
sub_cmd_description="Guess a password for https://completethecodetwo.cards",
|
||||
scopes=guild_ids,
|
||||
)
|
||||
@commands.cooldown(1, 2, commands.BucketType.user)
|
||||
async def _pw(self, ctx: SlashContext, guess: str) -> None:
|
||||
@cooldown(bucket=Buckets.USER, rate=1, interval=2)
|
||||
async def _pw(self, ctx: InteractionContext, guess: str) -> None:
|
||||
if len(guess) > 800:
|
||||
await ctx.send(
|
||||
(
|
||||
|
@ -89,14 +87,14 @@ class CTCCog(CacheCog):
|
|||
await ctx.send("Nope.", hidden=True)
|
||||
_ = Guess(guess=guess, user=ctx.author.id, correct=correct).save()
|
||||
|
||||
@cog_ext.cog_subcommand(
|
||||
base="ctc2",
|
||||
name="guesses",
|
||||
description="Show guesses made for https://completethecodetwo.cards",
|
||||
guild_ids=guild_ids,
|
||||
@slash_command(
|
||||
name="ctc2",
|
||||
sub_cmd_name="guesses",
|
||||
sub_cmd_description="Show guesses made for https://completethecodetwo.cards",
|
||||
scopes=guild_ids,
|
||||
)
|
||||
@commands.cooldown(1, 2, commands.BucketType.user)
|
||||
async def _guesses(self, ctx: SlashContext) -> None:
|
||||
@cooldown(bucket=Buckets.USER, rate=1, interval=2)
|
||||
async def _guesses(self, ctx: InteractionContext) -> None:
|
||||
exists = self.check_cache(ctx)
|
||||
if exists:
|
||||
await ctx.defer(hidden=True)
|
||||
|
@ -119,7 +117,7 @@ class CTCCog(CacheCog):
|
|||
name = "Correctly" if guess["correct"] else "Incorrectly"
|
||||
name += " guessed by: " + user
|
||||
fields.append(
|
||||
Field(
|
||||
EmbedField(
|
||||
name=name,
|
||||
value=guess["guess"] + "\n\u200b",
|
||||
inline=False,
|
||||
|
@ -140,18 +138,7 @@ class CTCCog(CacheCog):
|
|||
)
|
||||
pages.append(embed)
|
||||
|
||||
paginator = Paginator(
|
||||
bot=self.bot,
|
||||
ctx=ctx,
|
||||
embeds=pages,
|
||||
timeout=60 * 5, # 5 minute timeout
|
||||
only=ctx.author,
|
||||
disable_after_timeout=True,
|
||||
use_extend=len(pages) > 2,
|
||||
left_button_style=ButtonStyle.grey,
|
||||
right_button_style=ButtonStyle.grey,
|
||||
basic_buttons=["◀", "▶"],
|
||||
)
|
||||
paginator = Paginator.create_from_embeds(self.bot, *pages, timeout=300)
|
||||
|
||||
self.cache[hash(paginator)] = {
|
||||
"guild": ctx.guild.id,
|
||||
|
@ -161,9 +148,9 @@ class CTCCog(CacheCog):
|
|||
"paginator": paginator,
|
||||
}
|
||||
|
||||
await paginator.start()
|
||||
await paginator.send(ctx)
|
||||
|
||||
|
||||
def setup(bot: commands.Bot) -> None:
|
||||
def setup(bot: Snek) -> None:
|
||||
"""Add CTCCog to J.A.R.V.I.S."""
|
||||
bot.add_cog(CTCCog(bot))
|
||||
|
|
Loading…
Add table
Reference in a new issue