added discord bot
This commit is contained in:
1
discord/.gitignore
vendored
Normal file
1
discord/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
config.py
|
||||
3
discord/config.py.example
Normal file
3
discord/config.py.example
Normal file
@@ -0,0 +1,3 @@
|
||||
TOKEN =
|
||||
ROLE_NAME = "Participant"
|
||||
DATABASE_FILE =
|
||||
88
discord/discord_bot.py
Normal file
88
discord/discord_bot.py
Normal file
@@ -0,0 +1,88 @@
|
||||
import discord
|
||||
from discord import app_commands, ui
|
||||
import sqlite3
|
||||
from config import TOKEN, ROLE_NAME, DATABASE_FILE
|
||||
|
||||
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
|
||||
|
||||
conn = sqlite3.connect(DATABASE_FILE)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT id, discord_user_id FROM participant WHERE discord_token = ?", (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 and Role
|
||||
cursor.execute("UPDATE participant SET discord_user_id = ? WHERE id = ?", (str(interaction.user.id), db_id))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
role = discord.utils.get(interaction.guild.roles, name=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)
|
||||
|
||||
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(TOKEN)
|
||||
Reference in New Issue
Block a user