Migrate verify, closes #105
This commit is contained in:
parent
05ee5d4fad
commit
ed101de216
1 changed files with 45 additions and 45 deletions
|
@ -1,10 +1,12 @@
|
||||||
"""J.A.R.V.I.S. Verify Cog."""
|
"""J.A.R.V.I.S. Verify Cog."""
|
||||||
|
import asyncio
|
||||||
from random import randint
|
from random import randint
|
||||||
|
|
||||||
from discord.ext import commands
|
from dis_snek import InteractionContext, Scale, Snake
|
||||||
from discord_slash import ComponentContext, SlashContext, cog_ext
|
from dis_snek.models.application_commands import slash_command
|
||||||
from discord_slash.model import ButtonStyle
|
from dis_snek.models.discord.components import Button, ButtonStyles, spread_to_rows
|
||||||
from discord_slash.utils import manage_components
|
from dis_snek.models.snek.command import cooldown
|
||||||
|
from dis_snek.models.snek.cooldowns import Buckets
|
||||||
|
|
||||||
from jarvis.db.models import Setting
|
from jarvis.db.models import Setting
|
||||||
|
|
||||||
|
@ -16,30 +18,27 @@ def create_layout() -> list:
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
label = "YES" if i == yes else "NO"
|
label = "YES" if i == yes else "NO"
|
||||||
id = f"no_{i}" if not i == yes else "yes"
|
id = f"no_{i}" if not i == yes else "yes"
|
||||||
color = ButtonStyle.green if i == yes else ButtonStyle.red
|
color = ButtonStyles.GREEN if i == yes else ButtonStyles.RED
|
||||||
buttons.append(
|
buttons.append(
|
||||||
manage_components.create_button(
|
Button(
|
||||||
style=color,
|
style=color,
|
||||||
label=label,
|
label=label,
|
||||||
custom_id=f"verify_button||{id}",
|
custom_id=f"verify_button||{id}",
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
action_row = manage_components.spread_to_rows(*buttons, max_in_row=3)
|
action_row = spread_to_rows(*buttons, max_in_row=3)
|
||||||
return action_row
|
return action_row
|
||||||
|
|
||||||
|
|
||||||
class VerifyCog(commands.Cog):
|
class VerifyCog(Scale):
|
||||||
"""J.A.R.V.I.S. Verify Cog."""
|
"""J.A.R.V.I.S. Verify Cog."""
|
||||||
|
|
||||||
def __init__(self, bot: commands.Bot):
|
def __init__(self, bot: Snake):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
|
|
||||||
@cog_ext.cog_slash(
|
@slash_command(name="verify", description="Verify that you've read the rules")
|
||||||
name="verify",
|
@cooldown(bucket=Buckets.USER, rate=1, interval=15)
|
||||||
description="Verify that you've read the rules",
|
async def _verify(self, ctx: InteractionContext) -> None:
|
||||||
)
|
|
||||||
@commands.cooldown(1, 15, commands.BucketType.user)
|
|
||||||
async def _verify(self, ctx: SlashContext) -> None:
|
|
||||||
await ctx.defer()
|
await ctx.defer()
|
||||||
role = Setting.objects(guild=ctx.guild.id, setting="verified").first()
|
role = Setting.objects(guild=ctx.guild.id, setting="verified").first()
|
||||||
if not role:
|
if not role:
|
||||||
|
@ -53,40 +52,41 @@ class VerifyCog(commands.Cog):
|
||||||
content=f"{ctx.author.mention}, please press the button that says `YES`.",
|
content=f"{ctx.author.mention}, please press the button that says `YES`.",
|
||||||
components=components,
|
components=components,
|
||||||
)
|
)
|
||||||
await message.delete(delay=15)
|
|
||||||
|
|
||||||
@cog_ext.cog_component(components=create_layout())
|
|
||||||
async def _process(self, ctx: ComponentContext) -> None:
|
|
||||||
await ctx.defer(edit_origin=True)
|
|
||||||
try:
|
try:
|
||||||
if ctx.author.id != ctx.origin_message.mentions[0].id:
|
context = await self.bot.wait_for_component(
|
||||||
return
|
messages=message, check=lambda x: ctx.author.id == x.author.id, timeout=30
|
||||||
except Exception:
|
)
|
||||||
return
|
|
||||||
correct = ctx.custom_id.split("||")[-1] == "yes"
|
correct = context.context.custom_id.split("||")[-1] == "yes"
|
||||||
if correct:
|
if correct:
|
||||||
components = ctx.origin_message.components
|
for row in components:
|
||||||
for c in components:
|
for component in row["components"]:
|
||||||
for c2 in c["components"]:
|
component["disabled"] = True
|
||||||
c2["disabled"] = True
|
setting = Setting.objects(guild=ctx.guild.id, setting="verified").first()
|
||||||
setting = Setting.objects(guild=ctx.guild.id, setting="verified").first()
|
|
||||||
role = await ctx.guild.get_role(setting.value)
|
|
||||||
await ctx.author.add_roles(role, reason="Verification passed")
|
|
||||||
setting = Setting.objects(guild=ctx.guild.id, setting="unverified").first()
|
|
||||||
if setting:
|
|
||||||
role = await ctx.guild.get_role(setting.value)
|
role = await ctx.guild.get_role(setting.value)
|
||||||
await ctx.author.remove_roles(role, reason="Verification passed")
|
await ctx.author.add_roles(role, reason="Verification passed")
|
||||||
await ctx.edit_origin(
|
setting = Setting.objects(guild=ctx.guild.id, setting="unverified").first()
|
||||||
content=f"Welcome, {ctx.author.mention}. Please enjoy your stay.",
|
if setting:
|
||||||
components=manage_components.spread_to_rows(*components, max_in_row=5),
|
role = await ctx.guild.get_role(setting.value)
|
||||||
)
|
await ctx.author.remove_roles(role, reason="Verification passed")
|
||||||
await ctx.origin_message.delete(delay=5)
|
|
||||||
else:
|
await context.context.edit_origin(
|
||||||
await ctx.edit_origin(
|
content=f"Welcome, {ctx.author.mention}. Please enjoy your stay.",
|
||||||
content=f"{ctx.author.mention}, incorrect. Please press the button that says `YES`",
|
components=components,
|
||||||
)
|
)
|
||||||
|
await context.context.message.delete(delay=5)
|
||||||
|
else:
|
||||||
|
await context.context.edit_origin(
|
||||||
|
content=(
|
||||||
|
f"{ctx.author.mention}, incorrect. "
|
||||||
|
"Please press the button that says `YES`"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
await message.delete(delay=30)
|
||||||
|
|
||||||
|
|
||||||
def setup(bot: commands.Bot) -> None:
|
def setup(bot: Snake) -> None:
|
||||||
"""Add VerifyCog to J.A.R.V.I.S."""
|
"""Add VerifyCog to J.A.R.V.I.S."""
|
||||||
bot.add_cog(VerifyCog(bot))
|
bot.add_cog(VerifyCog(bot))
|
||||||
|
|
Loading…
Add table
Reference in a new issue