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