why no work?
hisoka
python
3 years ago
8.5 kB
10
Indexable
length = 6
import asyncio
import contextlib
import json
import logging
import os
import platform
import random
import sqlite3
import string
import time
import traceback
from typing import Union
import discord
from captcha.image import ImageCaptcha
from colorama import Back, Fore, Style
from discord import app_commands, ui
from discord.ext import commands
class VerifyButton(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
# THE VERIFY BUTTON
@discord.ui.button(
label="Verify", custom_id="verify_button", style=discord.ButtonStyle.red
)
async def verify(
self,
interaction: discord.Interaction,
Button: discord.ui.Button,
):
# CHECK THE VERIFICATION CONFIG FILE
global text
# GENERATING THE CAPTCHA CODE
text = "".join(
random.choice(
string.ascii_uppercase + string.digits + string.ascii_lowercase
)
for _ in range(length)
)
file_name = "".join(
random.choice(
string.ascii_uppercase + string.digits + string.ascii_lowercase
)
for _ in range(20)
)
# GENERATE THE CAPTCHA IMAGE
image = ImageCaptcha(width=280, height=90)
captcha = image.generate(text)
image.write(text, f"captchas\\{file_name}.png")
file = discord.File(f"captchas//{file_name}.png", filename="{file_name}.png")
# PRINT CAPTCHA CODE INTO CONSOLE
print(f"Captcha Code: {text}")
# CAPTCHA EMBED
captchaembed = discord.Embed(
title="Verification",
description=f"`{interaction.guild.name}` is using **Casualties Verification**\n to protect their server.\n\nTo proceed, press `Input Code`.\n\n**Note:** This captcha is **case-sensitive.**",
color=0x000001,
)
captchaembed.set_footer(
text="Verification by /bran#6666",
icon_url="https://i.imgur.com/4DuUUVU.png",
)
captchaembed.set_image(url=f"attachment://{file_name}.png")
# SEND CAPTCHA EMBED WHEN VERIFY BUTTON IS CLICKED
await interaction.response.send_message(
embed=captchaembed,
file=file,
ephemeral=True,
view=InputCodeButton(),
)
class InputCodeButton(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
# THIS MUST BE IN LINE WITH THE ABOVE DEF
@discord.ui.button(
label="Input Code",
row=0,
style=discord.ButtonStyle.blurple,
custom_id="InputCode",
)
# SEND MODAL
async def button_callback(self, interaction, button):
await interaction.response.send_modal(VerifyModal())
# SHOW "INPUT CODE" BUTTON AS DISABLED
class DisabledButton(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
@discord.ui.button(
label="Input Code",
row=0,
style=discord.ButtonStyle.gray,
disabled=True,
)
# THIS ISN'T ACTUALLY BEING USED!
async def button_callback(self, interaction, button):
await interaction.response.send_message("This is a test DisabledButton")
# URL BUTTON - DISPLAYED ON VERIFICATION SUCCESS
class CasualButton(discord.ui.View):
def __init__(self):
super().__init__()
self.add_item(
discord.ui.Button(
label="About",
url="https://casualties.io",
)
)
class VerifyModal(discord.ui.Modal, title="Verification"):
code = ui.TextInput(
label="Captcha",
placeholder="Input Captcha Code",
style=discord.TextStyle.short,
)
# MODAL SUBMIT WILL TRIGGER THIS
async def on_submit(self, interaction: discord.Interaction):
await interaction.response.edit_message(view=DisabledButton())
# SUCCESS EMBED
successembed = discord.Embed(
title="Verification Success!",
description=f"Congrats <@{interaction.user.id}>, you're human!\n\nAccess to `{interaction.guild.name}` has been granted.",
color=0x8FCE00,
)
successembed.set_footer(
text="Verification by /bran#6666",
icon_url="https://i.imgur.com/4DuUUVU.png",
)
# FAIL EMBED
failembed = discord.Embed(
title="Incorrect Code!",
description="The code you submitted was incorrect, please try again.\n\n**Note**: This captcha is **case-sensitive.**",
color=0xFF4040,
)
failembed.set_footer(
text="Verification by /bran#6666",
icon_url="https://i.imgur.com/4DuUUVU.png",
)
# ERROR EMBED
errorembed = discord.Embed(
title="Verification Error!",
description=f"An error has occured while attempting to verify in `{interaction.guild.name}`, please try again.\n\n **Note:** Error could be caused by high volume. Please remain patient.",
color=0xFFC45F,
)
errorembed.set_footer(
text="Verification by /bran#6666",
icon_url="https://i.imgur.com/4DuUUVU.png",
)
# CHECKING THE USER INPUTTED TEXT AGAINST CAPTCHA
try:
# IF CODE IS CORRECT
if self.code.value == text:
print(f"{self.verifyrole}")
await interaction.member.add_roles(self.verifyrole)
(" Error adding role.")
await interaction.edit_original_response(
embed=successembed,
attachments=[],
view=CasualButton(),
)
print(" Correct code was submitted!")
return
await interaction.edit_original_response(
embed=failembed,
attachments=[],
view=None,
)
print(" Incorrect code has been submitted.")
return
except Exception:
await interaction.edit_original_response(
embed=errorembed,
attachments=[],
view=None,
)
print(" Captcha Code on_submit error")
return
# VERIFY SLASH COMMAND
class Verification(commands.Cog):
def __init__(self, bot: commands.bot):
self.bot = bot
@app_commands.command(name="verification_enable", description="Verification setup.")
@app_commands.describe(channel="Verification channel.", role="Verified role.")
async def verifysetup(
self,
interaction: discord.Interaction,
channel: discord.TextChannel,
role: discord.Role,
):
captchaembed = discord.Embed(
title="Security Checkpoint",
description=f"Bots are not permitted in `{interaction.guild.name}`.\n\n To prove you are human, please complete the verification process.",
color=0x000001,
)
captchaembed.set_footer(
text="Verification by /bran#6666",
icon_url="https://i.imgur.com/4DuUUVU.png",
)
# SEND INITIAL VERIFICATION EMBED
await interaction.channel.send(
content="", embed=captchaembed, view=VerifyButton()
)
await interaction.response.send_message(
content=f"Verification enabled!\n\nTo *disable*, please delete the embedded message.\n\n**Setup Information:**\n> Verification Channel: {self.verifychannel}\n> Verified Role: {self.verifyrole}",
ephemeral=True,
)
async def cog_load(self):
asyncio.create_task(self.load_my_data())
async def load_my_data(self):
await self.bot.wait_until_ready()
# do your assign logic
self.verifyconfig = json.loads(open("cogs/verifyconfig.json", "r").read())
self.verifyguild = self.bot.get_guild(self.verifyconfig["verifyguild"])
self.verifychannel = self.bot.get_channel(self.verifyconfig["verifychannel"])
self.verifyrole = self.bot.get_role(self.verifyconfig["verifyrole"])
async def setup(bot: commands.Bot):
await bot.add_cog(Verification(bot))
Editor is loading...