Added ability to regenerate captchas.

This commit is contained in:
Vegard Berg 2022-08-04 14:34:45 +02:00
parent 246e011549
commit 64c138110c
1 changed files with 75 additions and 0 deletions

View File

@ -15,6 +15,9 @@ from naff import (
Member,
File,
SlashCommandChoice,
Button,
ButtonStyles,
Embed,
)
from naff.api import events
from naff.models.discord.enums import Permissions
@ -312,6 +315,12 @@ class Gatekeep(Extension):
File(bio_image, file_name="captcha.png"),
File(bio_audio, file_name="captcha.wav"),
],
components=Button(
label="Regenerate",
emoji="🔃",
style=ButtonStyles.GRAY,
custom_id=f"gatekeep-captcha-regenerate:{member.id}",
),
)
return
@ -322,7 +331,73 @@ class Gatekeep(Extension):
File(bio_image, file_name="captcha.png"),
File(bio_audio, file_name="captcha.wav"),
],
components=Button(
label="Regenerate",
emoji="🔃",
style=ButtonStyles.GRAY,
custom_id=f"gatekeep-captcha-regenerate:{member.id}",
),
)
@listen(events.Button)
async def on_regenerate_button(self, button: events.Button):
ctx = button.context
member = ctx.author
if (
not ctx.custom_id.startswith("gatekeep-captcha-regenerate:")
or not len(ctx.custom_id.split(":")) == 2
):
return
if ctx.author.id != int(ctx.custom_id.split(":")[1]):
await ctx.send("You are not the owner of this captcha.", ephemeral=True)
return
gkc: GatekeepCaptchasModel
gkc = GatekeepCaptchasModel.get_or_none(
guild_id=ctx.guild.id, user_id=ctx.author.id
)
if gkc is None:
await ctx.send("You have no captcha to regenerate.", ephemeral=True)
return
image = ImageCaptcha(width=240, height=90)
audio = AudioCaptcha()
captcha_text = "".join([random.choice(string.digits) for _ in range(7)])
gkc.captcha = captcha_text
gkc.save()
bio_image = BytesIO()
bio_audio = BytesIO()
image.write(captcha_text, bio_image, format="png")
bio_audio.write(audio.generate(captcha_text))
bio_image.seek(0)
bio_audio.seek(0)
msg = await ctx.send(
f"The server *{member.guild.name}* requires users to complete a captcha to join.\n"
"You can complete it by sending the command `/captcha <text>` in the server",
files=[
File(bio_image, file_name="captcha.png"),
File(bio_audio, file_name="captcha.wav"),
],
components=Button(
label="Regenerate",
emoji="🔃",
style=ButtonStyles.GRAY,
custom_id=f"gatekeep-captcha-regenerate:{member.id}",
),
)
if msg is not None:
await ctx.message.delete()
def setup(client: Client):