py
unknown
python
3 years ago
11 kB
13
Indexable
# Config
# Length of Captcha
length = 6
import contextlib
import json
import os
import random
import string
from typing import Union
import discord
import interactions
from discord import Option
from discord import option
from captcha.image import ImageCaptcha
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
bot = discord.Bot()
bot = commands.Bot(
command_prefix=commands.when_mentioned_or("!"),
case_insensitive=True,
intents=discord.Intents.all(),
)
config = None
verify_channel = None
verify_role = None
verify_guild = None
# Captcha Configuration
@bot.event
async def on_ready():
global config
global verify_channel
global verify_role
global verify_guild
print("Config loading...")
try:
config = json.loads(open("config.json", "r").read())
except:
print("No config found! Run =setup in your server!")
else:
verify_guild = bot.get_guild(config["guild"])
verify_channel = bot.get_channel(config["channel"])
verify_role = verify_guild.get_role(config["role"])
print("Loaded config successfully!")
print(f"{bot.user} is online!")
# Testing Add Button - This Works - (kinda)
class MyView(discord.ui.View): # First "Verify" Button attached here
@discord.ui.button(label="Verify", style=discord.ButtonStyle.red)
@bot.event
async def on_button_callback(member, button, interaction):
global config
global verify_channel
global verify_role
global verify_guild
# if member.guild.id == int(config["guild"]):
# await member.add_roles(verify_role) --> I'm pretty sure this was wrong
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)
print(text)
image.write(text, f"captchas\\{file_name}.png")
file = discord.File(f"captchas//{file_name}.png", filename=f"{file_name}.png")
embed = discord.Embed(
title="Verification",
description="This server is using Captcha Verification\n to protect their server.\n\nPlease type out the letters you see in\nthe captcha below.\n\n**Note:** The captcha is **case-sensitive.**",
color=0x000001,
)
embed.set_footer(text="Captcha Verification by /bran#6666")
embed.set_image(url=f"attachment://{file_name}.png")
await interaction.response.send_message(
content=member, embed=embed, file=file, ephemeral=True, view=MyView2()
)
# Wait for user to reply
async def on_submit(self, interaction: discord.Interaction):
for x in range(3):
try:
modal.captchacode = await modal.wait(
"thecode", check=wait_for_reply, timeout=30
)
except:
try:
await interaction.user.kick(
reason="Verification Timeout.", ephemeral=True
)
except:
pass
break
else:
if modal.captchacode == text:
await interaction.user.add_roles(verify_role)
for x in del_msgs:
await x.delete()
await rpy.delete()
return
else:
if x != 2:
msg = await verify_channel.send(
f"{interaction.user.mention} Invalid, you have {2 - x} attempts left.",
ephemeral=True,
)
try:
await interaction.user.kick(
reason="Too many attempts.", ephemeral=True
)
except:
pass
for x in del_msgs:
try:
await x.delete()
except:
continue
# Button Slash Command!
@bot.slash_command(name="button-fuckery") # Create a slash command
async def button(ctx):
buttembed = discord.Embed(
title="This is a test embed.",
description="Trying to attach a new button drove me crazy!",
color=0x000001,
)
buttembed.set_footer(text="/bran#6666")
await ctx.respond(
# "Button will show below here!",
embed=buttembed,
view=MyView(),
) # Send a message with our View class that contains the button
class MyView2(discord.ui.View):
@discord.ui.button(label="Input Code", row=0, style=discord.ButtonStyle.secondary)
async def button_callback(self, button, interaction):
await interaction.response.send_modal(MyModal(title="Verification"))
# Modal
class MyModal(discord.ui.Modal):
def __init__(self, *args, **kwargs) -> None:
super().__init__(
*args,
**kwargs,
)
self.add_item(
discord.ui.InputText(
label="Captcha Code",
)
)
async def callback(self, interaction: discord.Interaction):
async def on_submit(member, interaction: discord.Interaction):
for x in range(3):
try:
modal.captchacode = await modal.wait(
"thecode", check=wait_for_reply, timeout=30
)
except:
try:
await interaction.user.kick(
reason="Verification Timeout.", ephemeral=True
)
except:
pass
break
else:
if modal.captchacode == text:
await interaction.user.add_roles(verify_role)
for x in del_msgs:
await x.delete()
await rpy.delete()
return
else:
if x != 2:
msg = await verify_channel.send(
f"{interaction.user.mention} Invalid, you have {2 - x} attempts left.",
ephemeral=True,
)
try:
await interaction.user.kick(
reason="Too many attempts.", ephemeral=True
)
except:
pass
for x in del_msgs:
try:
await x.delete()
except:
continue
embed = discord.Embed(title="Modal Results")
embed.add_field(name="Submitted Code:", value=self.children[0].value)
# embed.add_field(name="Long Input", value=self.children[1].value)
await interaction.response.send_message(embeds=[embed])
# Captcha Channel Setup
@bot.event
async def on_channel_create(channel):
global config
global verify_channel
global verify_role
global verify_guild
if channel.id == int(config["channel"]):
try:
overwrites = {
verify_role: discord.PermissionOverwrite(
read_messages=False, send_messages=False, add_reactions=False
)
}
await channel.edit(overwrites=overwrites)
except:
pass
# Setup Captcha Verification Settings - Already Enabled
@bot.command()
@commands.guild_only()
@commands.has_permissions(administrator=True)
async def setup(ctx):
if os.path.exists("config.json"):
await ctx.send("config.json already exists!")
return
global config
msg = await ctx.send("Setting up guild...")
role = await ctx.guild.create_role(name="Verifying")
for channel in ctx.guild.channels:
try:
overwrites = {
role: discord.PermissionOverwrite(
read_messages=False, send_messages=False, add_reactions=False
)
}
await channel.edit(overwrites=overwrites)
except:
pass
overwrites = {
ctx.guild.default_role: discord.PermissionOverwrite(
read_messages=False,
send_messages=False,
),
role: discord.PermissionOverwrite(
read_messages=True,
send_messages=True,
),
}
channel = await ctx.guild.create_text_channel(
name="verify-here", overwrites=overwrites, slowmode_delay=10
)
con_json = {"role": role.id, "channel": channel.id, "guild": ctx.guild.id}
config = con_json
conf = open("config.json", "a")
conf.write(json.dumps(con_json))
conf.close()
await msg.edit(content="Finished Setup!")
# Setup Captcha Verification Settings - Previously Disabled
@bot.command()
@commands.guild_only()
@commands.has_permissions(administrator=True)
async def perms_setup(ctx):
if not os.path.exists("config.json"):
await ctx.send("config.json doesn't exists!")
return
global config
msg = await ctx.send("Rechecking perms...")
for channel in ctx.guild.channels:
try:
overwrites = {
verify_role: discord.PermissionOverwrite(
read_messages=False, send_messages=False, add_reactions=False
)
}
await channel.edit(overwrites=overwrites)
except:
pass
overwrites = {
ctx.guild.default_role: discord.PermissionOverwrite(
read_messages=False,
send_messages=False,
),
verify_role: discord.PermissionOverwrite(
read_messages=True,
send_messages=True,
),
}
await msg.edit("Finished Setup!")
bot.run(TOKEN)
Editor is loading...