Files
.gitignore
bot.py
requirements.txt
Code
3 295
bot.py
import discord
from discord.ext import commands
import os
import json
import re
from datetime import datetime
from dotenv import load_dotenv
load_dotenv()
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
VERIFIED_ROLE_NAME = "Verified"
VERIFICATIONS_FILE = "verifications.json"
def load_verifications():
if os.path.exists(VERIFICATIONS_FILE):
with open(VERIFICATIONS_FILE, 'r') as f:
return json.load(f)
return {}
def save_verifications(data):
with open(VERIFICATIONS_FILE, 'w') as f:
json.dump(data, f, indent=4)
def is_valid_email(email):
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
return re.match(pattern, email) is not None
def is_valid_minecraft_username(username):
pattern = r'^[a-zA-Z0-9_]{3,16}$'
return re.match(pattern, username) is not None
@bot.event
async def on_ready():
print(f'Bot is ready! Logged in as {bot.user}')
print(f'Bot is in {len(bot.guilds)} guild(s)')
for guild in bot.guilds:
verified_role = discord.utils.get(guild.roles, name=VERIFIED_ROLE_NAME)
if not verified_role:
print(f'Warning: "{VERIFIED_ROLE_NAME}" role not found in {guild.name}')
@bot.command(name='verify')
async def verify(ctx):
if isinstance(ctx.channel, discord.DMChannel):
await ctx.send("Please use this command in a server, not in DMs!")
return
verifications = load_verifications()
user_id = str(ctx.author.id)
if user_id in verifications:
await ctx.send("You are already verified! Use `!checkverification` to see your details.")
return
embed = discord.Embed(
title="Minecraft Account Verification",
description="Please check your DMs to complete the verification process!",
color=discord.Color.blue()
)
await ctx.send(embed=embed)
try:
dm_channel = await ctx.author.create_dm()
welcome_embed = discord.Embed(
title="Welcome to Minecraft Verification!",
description="I'll need some information to verify your Minecraft account.",
color=discord.Color.green()
)
welcome_embed.add_field(
name="Step 1",
value="Please enter the email address associated with your Minecraft account:",
inline=False
)
await dm_channel.send(embed=welcome_embed)
def check(m):
return m.author == ctx.author and isinstance(m.channel, discord.DMChannel)
email_msg = await bot.wait_for('message', check=check, timeout=300.0)
email = email_msg.content.strip()
if not is_valid_email(email):
error_embed = discord.Embed(
title="Invalid Email",
description="The email address you provided is not valid. Please try again using `!verify`",
color=discord.Color.red()
)
await dm_channel.send(embed=error_embed)
return
step2_embed = discord.Embed(
title="Step 2",
description="Please enter your Minecraft username (3-16 characters, letters, numbers, and underscores only):",
color=discord.Color.blue()
)
await dm_channel.send(embed=step2_embed)
username_msg = await bot.wait_for('message', check=check, timeout=300.0)
minecraft_username = username_msg.content.strip()
if not is_valid_minecraft_username(minecraft_username):
error_embed = discord.Embed(
title="Invalid Username",
description="The Minecraft username you provided is not valid. Usernames must be 3-16 characters and contain only letters, numbers, and underscores. Please try again using `!verify`",
color=discord.Color.red()
)
await dm_channel.send(embed=error_embed)
return
verification_data = {
"email": email,
"minecraft_username": minecraft_username,
"discord_username": str(ctx.author),
"discord_id": user_id,
"guild_id": str(ctx.guild.id),
"verified_at": datetime.now().isoformat()
}
verifications[user_id] = verification_data
save_verifications(verifications)
verified_role = discord.utils.get(ctx.guild.roles, name=VERIFIED_ROLE_NAME)
if verified_role:
try:
await ctx.author.add_roles(verified_role)
role_status = f"You have been given the **{VERIFIED_ROLE_NAME}** role!"
except discord.Forbidden:
role_status = "I don't have permission to assign roles. Please contact an administrator."
else:
role_status = f"The **{VERIFIED_ROLE_NAME}** role doesn't exist. Please contact an administrator."
success_embed = discord.Embed(
title="Verification Complete!",
description="Your information has been recorded successfully.",
color=discord.Color.green()
)
success_embed.add_field(name="Email", value=email, inline=False)
success_embed.add_field(name="Minecraft Username", value=minecraft_username, inline=False)
success_embed.add_field(name="Role Status", value=role_status, inline=False)
await dm_channel.send(embed=success_embed)
await ctx.send(f"{ctx.author.mention} Your verification is complete!")
except discord.Forbidden:
await ctx.send(f"{ctx.author.mention} I couldn't send you a DM. Please enable DMs from server members and try again.")
except TimeoutError:
try:
timeout_embed = discord.Embed(
title="Verification Timeout",
description="You took too long to respond. Please start over with `!verify`",
color=discord.Color.red()
)
await dm_channel.send(embed=timeout_embed)
except:
pass
@bot.command(name='checkverification')
async def check_verification(ctx):
verifications = load_verifications()
user_id = str(ctx.author.id)
if user_id not in verifications:
await ctx.send("You are not verified yet. Use `!verify` to start the verification process.")
return
data = verifications[user_id]
embed = discord.Embed(
title="Your Verification Details",
color=discord.Color.blue()
)
embed.add_field(name="Email", value=data['email'], inline=False)
embed.add_field(name="Minecraft Username", value=data['minecraft_username'], inline=False)
embed.add_field(name="Verified At", value=data['verified_at'], inline=False)
try:
await ctx.author.send(embed=embed)
if not isinstance(ctx.channel, discord.DMChannel):
await ctx.send("Check your DMs for your verification details!")
except discord.Forbidden:
await ctx.send("I couldn't send you a DM. Please enable DMs from server members.")
@bot.command(name='unverify')
@commands.has_permissions(administrator=True)
async def unverify(ctx, member: discord.Member):
verifications = load_verifications()
user_id = str(member.id)
if user_id not in verifications:
await ctx.send(f"{member.mention} is not verified.")
return
del verifications[user_id]
save_verifications(verifications)