Rifat Azad commited on
Commit
53e1e26
·
1 Parent(s): 8958eee

init commit

Browse files
Dockerfile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.12-slim
2
+
3
+ WORKDIR /code
4
+
5
+ ENV PYTHONUNBUFFERED 1
6
+
7
+ RUN pip install --no-cache-dir -U discord pydvpl
8
+
9
+ COPY ./pydvpl_bot.py /code
10
+
11
+ CMD ["python", "pydvpl_bot.py"]
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: PyDVPL Discord Bot
3
- emoji: 🏃
4
- colorFrom: indigo
5
- colorTo: yellow
6
  sdk: docker
7
  pinned: false
8
  ---
 
1
  ---
2
  title: PyDVPL Discord Bot
3
+ emoji: 🌍
4
+ colorFrom: pink
5
+ colorTo: indigo
6
  sdk: docker
7
  pinned: false
8
  ---
pydvpl_bot.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import discord
3
+ from discord.ext import commands
4
+ from pydvpl.dvpl import compress_dvpl, decompress_dvpl
5
+
6
+ # Define your Discord bot token here
7
+ TOKEN = os.getenv('DISCORD_TOKEN')
8
+
9
+ # Set up bot command prefix
10
+ bot_intents = discord.Intents.all()
11
+
12
+ # Initialize the bot with intents
13
+ bot = commands.Bot(command_prefix=">>", intents=bot_intents)
14
+
15
+ # Ensure the existence of folders
16
+ def ensure_folders():
17
+ if not os.path.exists("received_to_compress"):
18
+ os.makedirs("received_to_compress")
19
+ if not os.path.exists("received_to_decompress"):
20
+ os.makedirs("received_to_decompress")
21
+
22
+ @bot.event
23
+ async def on_ready():
24
+ print(f'{bot.user} has connected to Discord!')
25
+ ensure_folders()
26
+
27
+ async def compress_file(file_path):
28
+ try:
29
+ with open(file_path, "rb") as f:
30
+ file_data = f.read()
31
+ compressed_data = compress_dvpl(file_data)
32
+ compressed_file_path = file_path + ".dvpl" # Example: Add .dvpl extension
33
+ with open(compressed_file_path, "wb") as f:
34
+ f.write(compressed_data)
35
+ return compressed_file_path
36
+ except Exception as e:
37
+ print(f"Compression failed: {e}")
38
+ return None
39
+
40
+ async def decompress_file(file_path):
41
+ try:
42
+ with open(file_path, "rb") as f:
43
+ file_data = f.read()
44
+ decompressed_data = decompress_dvpl(file_data)
45
+ decompressed_file_path = os.path.splitext(file_path)[0] # Example: Remove .dvpl extension
46
+ with open(decompressed_file_path, "wb") as f:
47
+ f.write(decompressed_data)
48
+ return decompressed_file_path
49
+ except Exception as e:
50
+ print(f"Decompression failed: {e}")
51
+ return None
52
+
53
+ @bot.command()
54
+ async def compress(ctx):
55
+ attachments = ctx.message.attachments
56
+
57
+ if not attachments:
58
+ await ctx.send("No file attached.")
59
+ return
60
+
61
+ for attachment in attachments:
62
+ file_path = os.path.join("received_to_compress", attachment.filename)
63
+ await attachment.save(file_path)
64
+ compressed_file_path = await compress_file(file_path)
65
+ if compressed_file_path:
66
+ await ctx.send(file=discord.File(compressed_file_path))
67
+ os.remove(file_path) # Delete the original file
68
+ os.remove(compressed_file_path) # Delete the compressed file
69
+ else:
70
+ await ctx.send("Failed to compress the file.")
71
+
72
+ @bot.command()
73
+ async def decompress(ctx):
74
+ attachments = ctx.message.attachments
75
+
76
+ if not attachments:
77
+ await ctx.send("No file attached.")
78
+ return
79
+
80
+ for attachment in attachments:
81
+ file_path = os.path.join("received_to_decompress", attachment.filename)
82
+ await attachment.save(file_path)
83
+ decompressed_file_path = await decompress_file(file_path)
84
+ if decompressed_file_path:
85
+ await ctx.send(file=discord.File(decompressed_file_path))
86
+ os.remove(file_path) # Delete the original file
87
+ os.remove(decompressed_file_path) # Delete the decompressed file
88
+ else:
89
+ await ctx.send("Failed to decompress the file.")
90
+
91
+ bot.run(TOKEN)
received_to_compress/example.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ example.txt
received_to_decompress/example.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ example.txt