Rifat Azad commited on
Commit
f0a53ee
·
1 Parent(s): a639187

cleanup code

Browse files
Files changed (1) hide show
  1. pydvpl_bot.py +165 -63
pydvpl_bot.py CHANGED
@@ -7,17 +7,26 @@ from discord.ext.commands import DefaultHelpCommand
7
  from pydvpl.dvpl import compress_dvpl, decompress_dvpl
8
 
9
 
10
- # Define the authorized user ID
 
 
11
  AUTHORIZED_USER_ID = os.getenv('AUTHORIZED_ID')
12
 
13
- # Define your Discord bot token here
14
  TOKEN = os.getenv('DISCORD_TOKEN')
15
 
16
- LOG_CHANNEL_ID = 1220420254962942084
 
 
17
 
18
  activity_start_time = 0
19
 
20
 
 
 
 
 
 
 
21
  class PyDVPLHelpCommand(DefaultHelpCommand):
22
  def get_command_signature(self, command):
23
  return f"{self.context.prefix}{command.qualified_name} {command.signature}"
@@ -35,12 +44,7 @@ class PyDVPLHelpCommand(DefaultHelpCommand):
35
  await self.get_destination().send(embed=embed)
36
 
37
 
38
- # Set up bot command prefix
39
- bot_intents = discord.Intents.all()
40
-
41
-
42
- # Initialize the bot with intents
43
- bot = commands.Bot(command_prefix=">>", intents=bot_intents, help_command=PyDVPLHelpCommand())
44
 
45
 
46
  # Ensure the existence of folders
@@ -51,6 +55,26 @@ def ensure_folders():
51
  os.makedirs("received_to_decompress")
52
 
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  @bot.event
55
  async def on_ready():
56
  global activity_start_time
@@ -67,6 +91,57 @@ async def on_ready():
67
 
68
  ensure_folders()
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
 
72
 
@@ -83,8 +158,7 @@ async def update_playing_time_activity():
83
  await bot.change_presence(activity=discord.Game(name=playing_time_activity))
84
 
85
 
86
- # Define the interval for updating the playing time (in seconds)
87
- UPDATE_INTERVAL = 60 # Update every minute
88
 
89
 
90
  @tasks.loop(seconds=UPDATE_INTERVAL)
@@ -92,6 +166,11 @@ async def update_playing_time():
92
  await update_playing_time_activity()
93
 
94
 
 
 
 
 
 
95
  async def compress_file(file_path):
96
  try:
97
  with open(file_path, "rb") as f:
@@ -106,6 +185,9 @@ async def compress_file(file_path):
106
  return None
107
 
108
 
 
 
 
109
  async def decompress_file(file_path):
110
  try:
111
  with open(file_path, "rb") as f:
@@ -120,6 +202,9 @@ async def decompress_file(file_path):
120
  return None
121
 
122
 
 
 
 
123
  @bot.command(help="Compresses attached files.")
124
  async def compress(ctx):
125
  attachments = ctx.message.attachments
@@ -152,6 +237,9 @@ async def compress(ctx):
152
  await ctx.message.delete()
153
 
154
 
 
 
 
155
  @bot.command(help="Decompresses attached files.")
156
  async def decompress(ctx):
157
  attachments = ctx.message.attachments
@@ -183,6 +271,12 @@ async def decompress(ctx):
183
  await ctx.message.delete()
184
 
185
 
 
 
 
 
 
 
186
  @bot.command(help="Pings to check bot's latency")
187
  async def ping(ctx):
188
  latency = round(bot.latency * 1000) # latency in milliseconds
@@ -191,6 +285,40 @@ async def ping(ctx):
191
  await ctx.message.delete()
192
 
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  @bot.command(help="Cleans all files in received_to_compress and received_to_decompress directories.")
195
  async def clean(ctx):
196
  # Check if the user invoking the command is authorized
@@ -227,6 +355,9 @@ async def clean(ctx):
227
  await ctx.message.delete()
228
 
229
 
 
 
 
230
  @bot.command(help="Set bot's presence to online")
231
  async def online(ctx):
232
  if str(ctx.author.id) != AUTHORIZED_USER_ID:
@@ -238,6 +369,9 @@ async def online(ctx):
238
  await ctx.message.delete()
239
 
240
 
 
 
 
241
  @bot.command(help="Set bot's presence to Do Not Disturb")
242
  async def dnd(ctx):
243
  if str(ctx.author.id) != AUTHORIZED_USER_ID:
@@ -249,6 +383,9 @@ async def dnd(ctx):
249
  await ctx.message.delete()
250
 
251
 
 
 
 
252
  @bot.command(help="Set bot's presence to offline")
253
  async def offline(ctx):
254
  if str(ctx.author.id) != AUTHORIZED_USER_ID:
@@ -259,6 +396,10 @@ async def offline(ctx):
259
  await ctx.send("Bot's presence set to offline.")
260
  await ctx.message.delete()
261
 
 
 
 
 
262
  @bot.command(help="Set bot's activity")
263
  async def activity(ctx, activity_type, *, activity_text=None):
264
  if str(ctx.author.id) != AUTHORIZED_USER_ID:
@@ -287,6 +428,9 @@ async def activity(ctx, activity_type, *, activity_text=None):
287
  await ctx.message.delete()
288
 
289
 
 
 
 
290
  @bot.command(help="Send messages to specific channels or users")
291
  async def say(ctx, destination_type, *args):
292
  if str(ctx.author.id) != AUTHORIZED_USER_ID:
@@ -374,6 +518,9 @@ async def say(ctx, destination_type, *args):
374
  await ctx.message.delete()
375
 
376
 
 
 
 
377
  @bot.command(help="Show how long the bot has been playing the current game.")
378
  async def uptime(ctx):
379
  global activity_start_time
@@ -395,6 +542,9 @@ async def uptime(ctx):
395
  await ctx.message.delete()
396
 
397
 
 
 
 
398
  @bot.command(help="Show statistics about bot's health and connection.")
399
  async def stats(ctx):
400
 
@@ -433,62 +583,14 @@ async def stats(ctx):
433
  await ctx.message.delete()
434
 
435
 
436
- @bot.command(help="Generates an invite link for the bot and sends it to your DM.")
437
- async def invite(ctx):
438
- permissions = discord.Permissions(
439
- read_messages=True,
440
- send_messages=True,
441
- embed_links=True, # Permission for embedded activities
442
- attach_files=True,
443
- read_message_history=True,
444
- mention_everyone=True,
445
- add_reactions=True,
446
- manage_guild=True,
447
- )
448
-
449
- client_id = bot.user.id
450
- invite_link = discord.utils.oauth_url(client_id, permissions=permissions, scopes=['bot'])
451
-
452
- # Send the invite link to the user's DM
453
- try:
454
- await ctx.author.send(f"Invite link for PyDVPL: {invite_link}")
455
- await ctx.send(f"Sent the invite link to your DM. {ctx.author.mention}")
456
- except discord.Forbidden:
457
- await ctx.send(f"Failed to send the invite link to your DM. {ctx.author.mention} Please make sure your DMs are open.")
458
- await ctx.message.delete()
459
-
460
-
461
- @bot.event
462
- async def on_disconnect():
463
- print('Bot disconnected from Discord. Attempting to reconnect...')
464
- await bot.change_presence(status=discord.Status.invisible)
465
-
466
 
467
- @bot.event
468
- async def on_command(ctx):
469
- # Log command usage
470
- log_channel = bot.get_channel(LOG_CHANNEL_ID)
471
- if log_channel:
472
- if ctx.guild:
473
- log_message = f"Command `{ctx.command}` used by `{ctx.author} ({ctx.author.id})` in `{ctx.guild} ({ctx.guild.id})`"
474
- else:
475
- log_message = f"Command `{ctx.command}` used by `{ctx.author} ({ctx.author.id})` in {ctx.me.mention}'s `DM`"
476
- await log_channel.send(log_message)
477
 
478
 
479
- @bot.event
480
- async def on_command_error(ctx, error):
481
- if isinstance(error, commands.CommandNotFound):
482
- await ctx.send("Sorry, I couldn't find that command. Use `>>help` to see the list of available commands.")
483
- await ctx.message.delete()
484
- else:
485
- print(f'An error occurred: {error}')
486
 
487
 
488
- @bot.event
489
- async def on_error(event, *args):
490
- print(f'An error occurred during event {event}: {args[0]}')
491
- await bot.change_presence(status=discord.Status.dnd)
492
 
493
 
494
- bot.run(TOKEN)
 
7
  from pydvpl.dvpl import compress_dvpl, decompress_dvpl
8
 
9
 
10
+ # DEFINES ------------------------------------------------------ START
11
+
12
+
13
  AUTHORIZED_USER_ID = os.getenv('AUTHORIZED_ID')
14
 
 
15
  TOKEN = os.getenv('DISCORD_TOKEN')
16
 
17
+ LOG_CHANNEL_ID = os.getenv('CHANNEL_ID')
18
+
19
+ UPDATE_INTERVAL = 60
20
 
21
  activity_start_time = 0
22
 
23
 
24
+ # DEFINES ------------------------------------------------------ END
25
+
26
+
27
+ # HELPER ------------------------------------------------------ START
28
+
29
+
30
  class PyDVPLHelpCommand(DefaultHelpCommand):
31
  def get_command_signature(self, command):
32
  return f"{self.context.prefix}{command.qualified_name} {command.signature}"
 
44
  await self.get_destination().send(embed=embed)
45
 
46
 
47
+ # FUNCTION ------------------------------------------------------ SPLIT
 
 
 
 
 
48
 
49
 
50
  # Ensure the existence of folders
 
55
  os.makedirs("received_to_decompress")
56
 
57
 
58
+ # HELPER ------------------------------------------------------ END
59
+
60
+
61
+ # CORE_PREFIX ------------------------------------------------------ START
62
+
63
+
64
+ # Set up bot command prefix
65
+ bot_intents = discord.Intents.all()
66
+
67
+
68
+ # Initialize the bot with intents
69
+ bot = commands.Bot(command_prefix=">>", intents=bot_intents, help_command=PyDVPLHelpCommand())
70
+
71
+
72
+ # CORE_PREFIX ------------------------------------------------------ END
73
+
74
+
75
+ # CORE ------------------------------------------------------ START
76
+
77
+
78
  @bot.event
79
  async def on_ready():
80
  global activity_start_time
 
91
 
92
  ensure_folders()
93
 
94
+
95
+ # FUNCTION ------------------------------------------------------ SPLIT
96
+
97
+
98
+ @bot.event
99
+ async def on_disconnect():
100
+ print('Bot disconnected from Discord. Attempting to reconnect...')
101
+ await bot.change_presence(status=discord.Status.invisible)
102
+
103
+
104
+ # FUNCTION ------------------------------------------------------ SPLIT
105
+
106
+
107
+ @bot.event
108
+ async def on_command(ctx):
109
+ # Log command usage
110
+ log_channel = bot.get_channel(LOG_CHANNEL_ID)
111
+ if log_channel:
112
+ if ctx.guild:
113
+ log_message = f"Command `{ctx.command}` used by `{ctx.author} ({ctx.author.id})` in `{ctx.guild} ({ctx.guild.id})`"
114
+ else:
115
+ log_message = f"Command `{ctx.command}` used by `{ctx.author} ({ctx.author.id})` in {ctx.me.mention}'s `DM`"
116
+ await log_channel.send(log_message)
117
+
118
+
119
+ # FUNCTION ------------------------------------------------------ SPLIT
120
+
121
+
122
+ @bot.event
123
+ async def on_command_error(ctx, error):
124
+ if isinstance(error, commands.CommandNotFound):
125
+ await ctx.send("Sorry, I couldn't find that command. Use `>>help` to see the list of available commands.")
126
+ await ctx.message.delete()
127
+ else:
128
+ print(f'An error occurred: {error}')
129
+
130
+
131
+ # FUNCTION ------------------------------------------------------ SPLIT
132
+
133
+
134
+ @bot.event
135
+ async def on_error(event, *args):
136
+ print(f'An error occurred during event {event}: {args[0]}')
137
+ await bot.change_presence(status=discord.Status.dnd)
138
+
139
+
140
+ # CORE ------------------------------------------------------ END
141
+
142
+
143
+
144
+ # ACTIVITY ------------------------------------------------------ START
145
 
146
 
147
 
 
158
  await bot.change_presence(activity=discord.Game(name=playing_time_activity))
159
 
160
 
161
+ # FUNCTION ------------------------------------------------------ SPLIT
 
162
 
163
 
164
  @tasks.loop(seconds=UPDATE_INTERVAL)
 
166
  await update_playing_time_activity()
167
 
168
 
169
+ # ACTIVITY ------------------------------------------------------ END
170
+
171
+
172
+ # PYDVPL ------------------------------------------------------ START
173
+
174
  async def compress_file(file_path):
175
  try:
176
  with open(file_path, "rb") as f:
 
185
  return None
186
 
187
 
188
+ # FUNCTION ------------------------------------------------------ SPLIT
189
+
190
+
191
  async def decompress_file(file_path):
192
  try:
193
  with open(file_path, "rb") as f:
 
202
  return None
203
 
204
 
205
+ # FUNCTION ------------------------------------------------------ SPLIT
206
+
207
+
208
  @bot.command(help="Compresses attached files.")
209
  async def compress(ctx):
210
  attachments = ctx.message.attachments
 
237
  await ctx.message.delete()
238
 
239
 
240
+ # FUNCTION ------------------------------------------------------ SPLIT
241
+
242
+
243
  @bot.command(help="Decompresses attached files.")
244
  async def decompress(ctx):
245
  attachments = ctx.message.attachments
 
271
  await ctx.message.delete()
272
 
273
 
274
+ # PYDVPL ------------------------------------------------------ END
275
+
276
+
277
+ # UTILITY USER_CMD ------------------------------------------------------ START
278
+
279
+
280
  @bot.command(help="Pings to check bot's latency")
281
  async def ping(ctx):
282
  latency = round(bot.latency * 1000) # latency in milliseconds
 
285
  await ctx.message.delete()
286
 
287
 
288
+ # FUNCTION ------------------------------------------------------ SPLIT
289
+
290
+
291
+ @bot.command(help="Generates an invite link for the bot and sends it to your DM.")
292
+ async def invite(ctx):
293
+ permissions = discord.Permissions(
294
+ read_messages=True,
295
+ send_messages=True,
296
+ embed_links=True, # Permission for embedded activities
297
+ attach_files=True,
298
+ read_message_history=True,
299
+ mention_everyone=True,
300
+ add_reactions=True,
301
+ manage_guild=True,
302
+ )
303
+
304
+ client_id = bot.user.id
305
+ invite_link = discord.utils.oauth_url(client_id, permissions=permissions, scopes=['bot'])
306
+
307
+ # Send the invite link to the user's DM
308
+ try:
309
+ await ctx.author.send(f"Invite link for PyDVPL: {invite_link}")
310
+ await ctx.send(f"Sent the invite link to your DM. {ctx.author.mention}")
311
+ except discord.Forbidden:
312
+ await ctx.send(f"Failed to send the invite link to your DM. {ctx.author.mention} Please make sure your DMs are open.")
313
+ await ctx.message.delete()
314
+
315
+
316
+ # UTILITY USER_CMD ------------------------------------------------------ END
317
+
318
+
319
+ # UTILITY ADMIN_CMD ------------------------------------------------------ START
320
+
321
+
322
  @bot.command(help="Cleans all files in received_to_compress and received_to_decompress directories.")
323
  async def clean(ctx):
324
  # Check if the user invoking the command is authorized
 
355
  await ctx.message.delete()
356
 
357
 
358
+ # FUNCTION ------------------------------------------------------ SPLIT
359
+
360
+
361
  @bot.command(help="Set bot's presence to online")
362
  async def online(ctx):
363
  if str(ctx.author.id) != AUTHORIZED_USER_ID:
 
369
  await ctx.message.delete()
370
 
371
 
372
+ # FUNCTION ------------------------------------------------------ SPLIT
373
+
374
+
375
  @bot.command(help="Set bot's presence to Do Not Disturb")
376
  async def dnd(ctx):
377
  if str(ctx.author.id) != AUTHORIZED_USER_ID:
 
383
  await ctx.message.delete()
384
 
385
 
386
+ # FUNCTION ------------------------------------------------------ SPLIT
387
+
388
+
389
  @bot.command(help="Set bot's presence to offline")
390
  async def offline(ctx):
391
  if str(ctx.author.id) != AUTHORIZED_USER_ID:
 
396
  await ctx.send("Bot's presence set to offline.")
397
  await ctx.message.delete()
398
 
399
+
400
+ # FUNCTION ------------------------------------------------------ SPLIT
401
+
402
+
403
  @bot.command(help="Set bot's activity")
404
  async def activity(ctx, activity_type, *, activity_text=None):
405
  if str(ctx.author.id) != AUTHORIZED_USER_ID:
 
428
  await ctx.message.delete()
429
 
430
 
431
+ # FUNCTION ------------------------------------------------------ SPLIT
432
+
433
+
434
  @bot.command(help="Send messages to specific channels or users")
435
  async def say(ctx, destination_type, *args):
436
  if str(ctx.author.id) != AUTHORIZED_USER_ID:
 
518
  await ctx.message.delete()
519
 
520
 
521
+ # FUNCTION ------------------------------------------------------ SPLIT
522
+
523
+
524
  @bot.command(help="Show how long the bot has been playing the current game.")
525
  async def uptime(ctx):
526
  global activity_start_time
 
542
  await ctx.message.delete()
543
 
544
 
545
+ # FUNCTION ------------------------------------------------------ SPLIT
546
+
547
+
548
  @bot.command(help="Show statistics about bot's health and connection.")
549
  async def stats(ctx):
550
 
 
583
  await ctx.message.delete()
584
 
585
 
586
+ # UTILITY ADMIN_CMD ------------------------------------------------------ END
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
587
 
 
 
 
 
 
 
 
 
 
 
588
 
589
 
590
+ # RUN PYDVPL ------------------------------------------------------ START
 
 
 
 
 
 
591
 
592
 
593
+ bot.run(TOKEN)
 
 
 
594
 
595
 
596
+ # RUN PYDVPL ------------------------------------------------------ END