Skip to content

Instantly share code, notes, and snippets.

@billieeyelashh
Created March 16, 2023 20:11
Show Gist options
  • Save billieeyelashh/f048f1e6a7de1c20db4424d95f0259c6 to your computer and use it in GitHub Desktop.
Save billieeyelashh/f048f1e6a7de1c20db4424d95f0259c6 to your computer and use it in GitHub Desktop.
import os
import discord
from discord.ext import commands
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import base64
import logging
# Set up logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
# Generate a key for encryption
def generate_key(password, salt):
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
)
key = base64.urlsafe_b64encode(kdf.derive(password.encode()))
return key
# Encrypt a message
def encrypt_message(message, key):
fernet = Fernet(key)
return fernet.encrypt(message.encode()).decode()
# Decrypt a message
def decrypt_message(message, key):
fernet = Fernet(key)
return fernet.decrypt(message.encode()).decode()
intents = discord.Intents.default()
intents.typing = False
intents.presences = False
bot = commands.Bot(command_prefix="!", intents=intents)
# Set your password and salt
PASSWORD = "your_password"
SALT = os.urandom(16)
# Store the encryption key
encryption_key = generate_key(PASSWORD, SALT)
encrypted_channel_id = None
@bot.event
async def on_ready():
logging.info(f"{bot.user.name} is ready")
@bot.event
async def on_message(message):
global encrypted_channel_id, encryption_key
if message.author.bot:
return
if message.channel.id == encrypted_channel_id:
encrypted_message = encrypt_message(message.content, encryption_key)
await message.delete()
await message.channel.send(f"{message.author.mention}: ||{encrypted_message}||")
logging.info(f"Encrypted message from {message.author}: {message.content} -> {encrypted_message}")
await bot.process_commands(message)
@bot.command(name="set_encrypted_channel")
async def set_encrypted_channel(ctx, channel: discord.TextChannel):
global encrypted_channel_id
encrypted_channel_id = channel.id
await ctx.send(f"Encrypted channel set to {channel.mention}")
logging.info(f"Encrypted channel set to {channel}")
@set_encrypted_channel.error
async def set_encrypted_channel_error(ctx, error):
if isinstance(error, commands.MissingRole):
await ctx.send(f"{ctx.author.mention}, you do not have the required role to use this command.")
else:
logging.error(f"An unexpected error occurred: {error}")
@bot.command(name="decrypt")
async def decrypt(ctx, password: str, *, encrypted_message: str):
if password == PASSWORD:
try:
decrypted_message = decrypt_message(encrypted_message, encryption_key)
await ctx.send(f"{ctx.author.mention}, the decrypted message is: {decrypted_message}")
except:
await ctx.send(f"{ctx.author.mention}, decryption failed. The message might be tampered with or the password is incorrect.")
else:
await ctx.send(f"{ctx.author.mention}, incorrect password.")
@bot.command(name="decrypt_message_id")
async def decrypt_message_id(ctx, password: str, message_id: int):
if password == PASSWORD:
try:
message_to_decrypt = await ctx.channel.fetch_message(message_id)
decrypted_message = decrypt_message(message_to_decrypt.content, encryption_key)
await ctx.send(f"{ctx.author.mention}, the decrypted message is: {decrypted_message}")
except discord.NotFound:
await ctx.send(f"{ctx.author.mention}, message not found.")
except:
await ctx.send(f"{ctx.author.mention}, decryption failed. The message might be tampered with or the password is incorrect.")
else:
await ctx.send(f"{ctx.author.mention}, incorrect password.")
bot.run("Your bot token here")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment