98 lines
3.9 KiB
Python
98 lines
3.9 KiB
Python
import discord
|
|
from discord import app_commands, ui
|
|
import psycopg2
|
|
import os
|
|
|
|
class VerificationModal(ui.Modal, title='Server Verification'):
|
|
token_input = ui.TextInput(
|
|
label='Enter your access Token',
|
|
placeholder='e.g. 7faaefea03184a0e',
|
|
min_length=16,
|
|
max_length=16,
|
|
required=True
|
|
)
|
|
|
|
async def on_submit(self, interaction: discord.Interaction):
|
|
token = self.token_input.value
|
|
|
|
try:
|
|
# Connect using your Postgres credentials
|
|
conn = psycopg2.connect(database=os.environ.get("POSTGRES_DB"), username=os.environ.get("POSTGRES_USER"), password=os.environ.get("POSTGRES_PASSWORD"))
|
|
with conn.cursor() as cursor:
|
|
# Postgres uses %s as placeholders instead of ?
|
|
cursor.execute("SELECT id, discord_user_id FROM participant WHERE discord_token = %s", (token,))
|
|
result = cursor.fetchone()
|
|
|
|
if not result:
|
|
await interaction.response.send_message("Invalid token. Check your email.", ephemeral=True)
|
|
return
|
|
|
|
db_id, existing_user_id = result
|
|
if existing_user_id:
|
|
await interaction.response.send_message("This token is already linked to an account.", ephemeral=True)
|
|
return
|
|
|
|
# Update DB
|
|
cursor.execute("UPDATE participant SET discord_user_id = %s WHERE id = %s", (str(interaction.user.id), db_id))
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
# Role Logic
|
|
role = discord.utils.get(interaction.guild.roles, name=os.environ.get("ROLE_NAME"))
|
|
if role:
|
|
await interaction.user.add_roles(role)
|
|
await interaction.response.send_message("Verified! Welcome to the server.", ephemeral=True)
|
|
else:
|
|
await interaction.response.send_message("Verification successful, but role not found. Contact staff.", ephemeral=True)
|
|
|
|
except Exception as e:
|
|
print(f"Database Error: {e}")
|
|
await interaction.response.send_message("A database error occurred. Please try again later.", ephemeral=True)
|
|
|
|
class PersistentView(ui.View):
|
|
def __init__(self):
|
|
super().__init__(timeout=None) # Keeps button active after bot restarts
|
|
|
|
@ui.button(label='Verify Now', style=discord.ButtonStyle.green, custom_id='verify_button')
|
|
async def verify_button(self, interaction: discord.Interaction, button: ui.Button):
|
|
await interaction.response.send_modal(VerificationModal())
|
|
|
|
class MyBot(discord.Client):
|
|
def __init__(self):
|
|
intents = discord.Intents.default()
|
|
intents.members = True
|
|
super().__init__(intents=intents)
|
|
self.tree = app_commands.CommandTree(self)
|
|
|
|
async def setup_hook(self):
|
|
self.add_view(PersistentView()) # Register the button listener
|
|
|
|
bot = MyBot()
|
|
|
|
# Replace the on_ready in your code with this:
|
|
@bot.event
|
|
async def on_ready():
|
|
print(f'Logged in as {bot.user.name}')
|
|
try:
|
|
# Guild ID must be an integer
|
|
guild = discord.Object(id=1453759207391367321)
|
|
bot.tree.copy_global_to(guild=guild)
|
|
synced = await bot.tree.sync(guild=guild)
|
|
print(f"Synced {len(synced)} commands to guild {guild.id}")
|
|
except Exception as e:
|
|
print(f"Sync Error: {e}")
|
|
|
|
@bot.tree.command(name="setup_welcome", description="Post the verification message in this channel")
|
|
@app_commands.checks.has_permissions(administrator=True)
|
|
async def setup_welcome(interaction: discord.Interaction):
|
|
embed = discord.Embed(
|
|
title="Welcome to the Server!",
|
|
description="Please click the button below to verify with your participant token and gain full access.",
|
|
color=discord.Color.blue()
|
|
)
|
|
await interaction.channel.send(embed=embed, view=PersistentView())
|
|
await interaction.response.send_message("Setup complete!", ephemeral=True)
|
|
|
|
bot.run(os.environ.get("TOKEN"))
|