Files
.gitignore
bot.py
requirements.txt
Code
3 411
bot.py
import discord
from discord.ext import commands
import os
from dotenv import load_dotenv
import json
import asyncio
load_dotenv()
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
user_sessions = {}
@bot.event
async def on_ready():
print(f'✅ Bot is online! Logged in as {bot.user}')
print(f'Bot ID: {bot.user.id}')
print('Ready to secure Microsoft accounts!')
@bot.command(name='secure')
async def secure_account(ctx):
"""Start the Microsoft account security process"""
user_id = ctx.author.id
embed = discord.Embed(
title="🔒 Microsoft Account Security Setup",
description="Let's secure your Microsoft account with recovery options!",
color=discord.Color.blue()
)
embed.add_field(
name="What we'll set up:",
value="• Recovery codes\n• Backup email address\n• Security verification",
inline=False
)
embed.set_footer(text="Reply with 'start' to begin or 'cancel' to abort")
await ctx.send(embed=embed)
def check(m):
return m.author.id == user_id and m.channel.id == ctx.channel.id
try:
msg = await bot.wait_for('message', check=check, timeout=60.0)
if msg.content.lower() == 'start':
user_sessions[user_id] = {
'step': 'email',
'data': {}
}
await ask_for_email(ctx, user_id)
elif msg.content.lower() == 'cancel':
await ctx.send("❌ Security setup cancelled.")
else:
await ctx.send("❌ Invalid response. Please use `!secure` to try again.")
except asyncio.TimeoutError:
await ctx.send("⏱️ Setup timed out. Please use `!secure` to start again.")
async def ask_for_email(ctx, user_id):
"""Ask user for their Microsoft account email"""
embed = discord.Embed(
title="📧 Step 1: Microsoft Account Email",
description="Please enter the email address of your Microsoft account:",
color=discord.Color.blue()
)
embed.set_footer(text="Type 'cancel' to abort")
await ctx.send(embed=embed)
def check(m):
return m.author.id == user_id and m.channel.id == ctx.channel.id
try:
msg = await bot.wait_for('message', check=check, timeout=120.0)
if msg.content.lower() == 'cancel':
del user_sessions[user_id]
await ctx.send("❌ Security setup cancelled.")
return
user_sessions[user_id]['data']['ms_email'] = msg.content
await ask_for_recovery_email(ctx, user_id)
except asyncio.TimeoutError:
if user_id in user_sessions:
del user_sessions[user_id]
await ctx.send("⏱️ Setup timed out.")
async def ask_for_recovery_email(ctx, user_id):
"""Ask user for their recovery email"""
embed = discord.Embed(
title="📮 Step 2: Recovery Email",
description="Please enter the email address you want to use for account recovery:",
color=discord.Color.blue()
)
embed.set_footer(text="Type 'skip' to skip this step or 'cancel' to abort")
await ctx.send(embed=embed)
def check(m):
return m.author.id == user_id and m.channel.id == ctx.channel.id
try:
msg = await bot.wait_for('message', check=check, timeout=120.0)
if msg.content.lower() == 'cancel':
del user_sessions[user_id]
await ctx.send("❌ Security setup cancelled.")
return
if msg.content.lower() != 'skip':
user_sessions[user_id]['data']['recovery_email'] = msg.content
await generate_recovery_codes(ctx, user_id)
except asyncio.TimeoutError:
if user_id in user_sessions:
del user_sessions[user_id]
await ctx.send("⏱️ Setup timed out.")
async def generate_recovery_codes(ctx, user_id):
"""Generate and display recovery codes"""
import secrets
import string
codes = []
for _ in range(8):
code = ''.join(secrets.choice(string.ascii_uppercase + string.digits) for _ in range(8))
formatted_code = f"{code[:4]}-{code[4:]}"
codes.append(formatted_code)
user_sessions[user_id]['data']['recovery_codes'] = codes
embed = discord.Embed(
title="🔑 Step 3: Recovery Codes Generated",
description="**⚠️ SAVE THESE CODES IMMEDIATELY! ⚠️**\n\nThese codes can be used to recover your account if you lose access.",
color=discord.Color.gold()
)
codes_text = "\n".join([f"`{i+1}. {code}`" for i, code in enumerate(codes)])
embed.add_field(name="Your Recovery Codes:", value=codes_text, inline=False)
embed.add_field(
name="Important:",
value="• Save these in a secure location\n• Each code can only be used once\n• Never share these with anyone",
inline=False
)
embed.set_footer(text="Reply with 'confirm' once you've saved these codes")
try:
await ctx.author.send(embed=embed)
await ctx.send("✅ Recovery codes sent to your DMs! Check your private messages.")
except discord.Forbidden:
await ctx.send(embed=embed)
await ctx.send("⚠️ I couldn't DM you. Make sure your DMs are open for security!")
def check(m):
return m.author.id == user_id and (m.channel.id == ctx.channel.id or isinstance(m.channel, discord.DMChannel))
try:
msg = await bot.wait_for('message', check=check, timeout=300.0)
if msg.content.lower() == 'confirm':
await finalize_setup(ctx, user_id)
else:
await ctx.send("⚠️ Please type 'confirm' to continue.")
except asyncio.TimeoutError:
if user_id in user_sessions:
del user_sessions[user_id]
await ctx.send("⏱️ Setup timed out.")
async def finalize_setup(ctx, user_id):
"""Finalize the security setup"""
session_data = user_sessions[user_id]['data']
save_user_data(user_id, session_data)
embed = discord.Embed(
title="✅ Security Setup Complete!",
description="Your Microsoft account security has been configured.",
color=discord.Color.green()
)
embed.add_field(name="Microsoft Account:", value=session_data['ms_email'], inline=False)
if 'recovery_email' in session_data:
embed.add_field(name="Recovery Email:", value=session_data['recovery_email'], inline=False)
embed.add_field(
name="Next Steps:",
value="• Keep your recovery codes in a safe place\n• Enable two-factor authentication\n• Regularly update your security settings",
inline=False
)
await ctx.send(embed=embed)
del user_sessions[user_id]