no worky
verifyconfig.json: { "verify_role": 1064034528374562826, "verify_channel": 1019885987603693640, "verify_guild": 1019489861754953759 }hisoka
python
3 years ago
8.3 kB
12
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
# intents = discord.Intents().all()
bot = commands.Bot(
command_prefix=commands.when_mentioned_or("-"), intents=discord.Intents.all()
)
def on_ready():
global verifyconfig
global verify_guild
global verify_channel
global verify_role
verifyconfig = json.loads(open("cogs/verifyconfig.json", "r").read())
verify_guild = bot.get_guild(verifyconfig["verify_guild"])
verify_channel = bot.get_channel(verifyconfig["verify_channel"])
verify_role = verify_guild.get_role(verifyconfig["verify_role"])
class PersistentViewBot(commands.Bot):
def __init__(self):
super().__init__(
command_prefix="-",
intents=discord.Intents.all(),
)
async def setup_hook(self) -> None:
self.add_view(VerifyButton())
class VerifyButton(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
@discord.ui.button(
label="Verify", custom_id="verify_button", style=discord.ButtonStyle.red
)
async def verify(
self,
interaction: discord.Interaction,
Button: discord.ui.Button,
):
global verifyconfig
global verify_guild
global verify_channel
global verify_role
global text
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)
)
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=f"{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",
icon_url="https://i.redacted.com/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)
@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())
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")
class CasualButton(discord.ui.View):
def __init__(self):
super().__init__()
self.add_item(
discord.ui.Button(
label="About",
url="redacted.com"
)
)
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,
)
# 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,
)
# 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,
)
# CHECKING THE USER INPUTTED TEXT AGAINST CAPTCHA
try:
# IF CODE IS CORRECT
if self.code.value == text:
print(f"{verify_role}")
await interaction.user.add_roles(verify_role)
(" Error adding role.")
await interaction.edit_original_response(
embed=successembed,
attachments=[],
view=CasualButton(),
)
print(" Correct code was submitted!")
return
else:
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,
) -> None:
self.bot = bot
@app_commands.command(name="verification_enable", description="Verification setup.")
@app_commands.describe(channel="Verification channel.", role="Verified role.")
async def verify(
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: {verify_channel}\n> Verified Role: {verify_role}",
ephemeral=True,
)
print(
f"Config: {verifyconfig} Role: {verify_role} Guild: {verify_guild} Channel: {verify_channel}"
)
async def setup(bot: commands.Bot):
await bot.add_cog(Verification(bot))
Editor is loading...