Files
.gitignore
bot.py
requirements.txt
Code
3 166
bot.py
import discord
from discord.ext import commands
import os
from dotenv import load_dotenv
import json
import hashlib
load_dotenv()
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
PASSWORD_FILE = 'passwords.json'
def load_passwords():
if os.path.exists(PASSWORD_FILE):
with open(PASSWORD_FILE, 'r') as f:
return json.load(f)
return {}
def save_passwords(passwords):
with open(PASSWORD_FILE, 'w') as f:
json.dump(passwords, f, indent=4)
def hash_password(password):
return hashlib.sha256(password.encode()).hexdigest()
@bot.event
async def on_ready():
print(f'Bot is ready! Logged in as {bot.user}')
print(f'Bot ID: {bot.user.id}')
@bot.command()
async def setpassword(ctx, password: str):
passwords = load_passwords()
user_id = str(ctx.author.id)
if user_id in passwords:
await ctx.send('You already have a password set! Use `!changepassword <old_password> <new_password>` to change it.')
return
if len(password) < 4:
await ctx.send('Password must be at least 4 characters long!')
return
passwords[user_id] = hash_password(password)
save_passwords(passwords)
await ctx.send('Password set successfully! ✅')
try:
await ctx.message.delete()
except:
pass
@bot.command()
async def changepassword(ctx, old_password: str, new_password: str):
passwords = load_passwords()
user_id = str(ctx.author.id)
if user_id not in passwords:
await ctx.send('You don\'t have a password set! Use `!setpassword <password>` first.')
return
if passwords[user_id] != hash_password(old_password):
await ctx.send('❌ Old password is incorrect!')
try:
await ctx.message.delete()
except:
pass
return
if len(new_password) < 4:
await ctx.send('New password must be at least 4 characters long!')
try:
await ctx.message.delete()
except:
pass
return
passwords[user_id] = hash_password(new_password)
save_passwords(passwords)
await ctx.send('✅ Password changed successfully!')
try:
await ctx.message.delete()
except:
pass
@bot.command()
async def checkpassword(ctx, password: str):
passwords = load_passwords()
user_id = str(ctx.author.id)
if user_id not in passwords:
await ctx.send('You don\'t have a password set!')
return
if passwords[user_id] == hash_password(password):
await ctx.send('✅ Password is correct!')
else:
await ctx.send('❌ Password is incorrect!')
try:
await ctx.message.delete()
except:
pass
@bot.command()
@commands.has_permissions(administrator=True)
async def resetpassword(ctx, member: discord.Member):
passwords = load_passwords()
user_id = str(member.id)
if user_id in passwords:
del passwords[user_id]
save_passwords(passwords)
await ctx.send(f'Password for {member.display_name} has been reset.')
else:
await ctx.send(f'{member.display_name} doesn\'t have a password set.')
@bot.command()
async def removepassword(ctx, password: str):
passwords = load_passwords()
user_id = str(ctx.author.id)
if user_id not in passwords:
await ctx.send('You don\'t have a password set!')
return
if passwords[user_id] != hash_password(password):
await ctx.send('❌ Password is incorrect!')
try:
await ctx.message.delete()
except:
pass
return
del passwords[user_id]
save_passwords(passwords)
await ctx.send('Password removed successfully!')
try:
await ctx.message.delete()
except:
pass
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send(f'Missing required argument: {error.param.name}')
elif isinstance(error, commands.MissingPermissions):
await ctx.send('You don\'t have permission to use this command!')
elif isinstance(error, commands.MemberNotFound):
await ctx.send('Member not found!')
else:
await ctx.send(f'An error occurred: {str(error)}')
bot.run(os.getenv('DISCORD_TOKEN'))