-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
841 lines (747 loc) · 37.7 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
import discord
from discord.ext import commands
import asyncio
import json5
from art import text2art
import os
from colorama import init, Fore
from collections import deque
import time
from PIL import Image
init()
with open("config.json5", "r") as config_file:
config = json5.load(config_file)
PREFIX = config.get("prefix")
PING = config.get("ping")
STATUS_MODE = config.get("status_mode")
STATUS_CONTENT = config.get("status_content")
LIBRUARY_PATH = config.get("library_path")
LIBRUARY_STATUS = config.get("library_status")
intents = discord.Intents.all()
if len(PREFIX) > 4:
PREFIX = "dfb."
bot = commands.Bot(command_prefix=PREFIX, intents=intents)
class InfoButtom(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
@discord.ui.button(label="Discord For Bots", style=discord.ButtonStyle.gray)
async def info(self, interaction: discord.Interaction, button: discord.ui.Button):
embed = discord.Embed(description=f"Discord For Bots is an unofficial open source program that lets you run discord as your own app! Want to download it? You can find the exe file and the code on [github]( https://github.com/stainowy/DiscordForBots)!", color=0x0e3972)
await interaction.response.send_message(embed=embed, ephemeral=True)
def clear_console():
command = 'cls' if os.name == 'nt' else 'clear'
os.system(command)
def start_widget():
title = text2art("Discord For Bots")
print(f"{Fore.LIGHTBLUE_EX}{title}")
print(f"{Fore.CYAN}GITHUB {Fore.WHITE} https://github.com/stainowy/DiscordForBots")
print(f"{Fore.CYAN}AUTHOR {Fore.WHITE} https://stainowy.gihub.io")
print("")
async def get_debug(content: str):
STATUS = config.get("debug")
if STATUS == "ON":
print(f"{Fore.GREEN}DEBUG {Fore.WHITE} {content}")
else:
pass
async def get_error(content: str):
print(f"{Fore.RED}ERROR {Fore.WHITE} {content}")
async def get_keypress():
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, input, "")
@bot.command(name="ping")
async def ping(ctx):
start_time = time.time()
COMMAND = config.get("commands")
if COMMAND == "ON":
try:
await get_debug(f"User '{ctx.author.name}' used a command '{PREFIX}ping'")
bot_ping = round(bot.latency * 1000)
end_time = time.time()
client_ping = round((end_time - start_time) * 1000)
embed = discord.Embed(description=f"> Bot ping: ``{bot_ping}ms``\n> Client ping: ``{client_ping}ms``")
await ctx.send(embed=embed, view=InfoButtom())
except Exception as e:
await get_error(e)
else:
await get_debug(f"User '{ctx.author.name}' tried to use the '{PREFIX}ping' command but it is disabled in the config")
@bot.event
async def on_message(message):
if message.content.strip() == f'<@{bot.user.id}>' or message.content.strip() == f'<@!{bot.user.id}>':
if PING == "ON":
embed = discord.Embed(description=f"Hi, I'm {bot.user.name}. And I'm an application launched with DiscordForBots :)", color=0x0e3972)
await message.channel.send(f'{message.author.mention}', embed=embed, view=InfoButtom())
await get_debug(f"User '{message.author.name}' used a command '@ping'")
else:
await get_debug(f"User '{message.author.name}' tried to use the '@ping' command but it is disabled in the config")
pass
async def get_user_id_or_nick(input_value, bot):
if input_value.startswith("@"):
nick = input_value[1:]
for guild in bot.guilds:
member = discord.utils.get(guild.members, name=nick)
if member:
return member.id
await get_error(f"User with nickname '{nick}' not found!")
raise ValueError("User not found.")
else:
try:
return int(input_value)
except ValueError:
await get_error("Invalid user ID format!")
raise ValueError("Invalid ID.")
async def select_options():
print(f" {Fore.CYAN}[1] {Fore.WHITE}Browse servers")
print(f" {Fore.CYAN}[2] {Fore.WHITE}Send DM")
print(f" {Fore.CYAN}[3] {Fore.WHITE}Edit Profile")
while True:
key = await get_keypress()
if key == "1":
clear_console()
start_widget()
await show_server_selection()
elif key == "2":
clear_console()
start_widget()
await select_user()
elif key == "3":
clear_console()
start_widget()
await select_action()
else:
await get_error("Invalid option!")
# - - - - - - - - - - - - - - - - - - - - - - Profile - - - - - - - - - - - - - - - - - - - - - - #
async def select_action():
print(f" {Fore.CYAN}[1] {Fore.WHITE}Change Status")
print(f" {Fore.CYAN}[2] {Fore.WHITE}Change Name")
print(f" {Fore.CYAN}[3] {Fore.WHITE}Change Profile Picture")
while True:
key = await get_keypress()
if key == "1":
clear_console()
start_widget()
await change_status()
elif key == "2":
clear_console()
start_widget()
await change_name()
elif key == "3":
clear_console()
start_widget()
await change_profile_picture()
elif key == "!back" or key == "!home":
clear_console()
start_widget()
await select_options()
else:
await get_error("Invalid option!")
async def change_profile_picture():
print(f"{Fore.CYAN}INFO {Fore.WHITE}You are in the edit view of your bot's profile picture")
print(f"{Fore.CYAN}INFO {Fore.WHITE}Enter the path to the new profile picture")
path = input(f"")
if path == "!back" or path == "!home":
clear_console()
start_widget()
await select_options()
if not os.path.exists(path):
await get_error("File does not exist! Please check the path and try again.")
return
if not path.lower().endswith((".png", ".jpg", ".jpeg")):
await get_error("Invalid file format! Please use PNG or JPG.")
return
try:
with Image.open(path) as img:
if img.width > 1024 or img.height > 1024:
await get_error("Image dimensions exceed 1024x1024! Resize and try again.")
return
with open(path, "rb") as image_file:
await bot.user.edit(avatar=image_file.read())
clear_console()
start_widget()
await get_debug("Profile picture updated successfully!")
await select_options()
except Exception as e:
await get_error(f"An error occurred: {e}")
async def change_status():
print(f"{Fore.CYAN}INFO {Fore.WHITE}Select your activity mode:")
print(f"")
print(f" {Fore.CYAN}[1] {Fore.WHITE}Playing")
print(f" {Fore.CYAN}[2] {Fore.WHITE}Watching")
print(f" {Fore.CYAN}[3] {Fore.WHITE}Competing")
print(f" {Fore.CYAN}[4] {Fore.WHITE}Listening")
print(f" {Fore.CYAN}[5] {Fore.WHITE}Custom")
print(f" {Fore.CYAN}[6] {Fore.WHITE}Clear")
tryb = input(f"")
if tryb == "!back" or tryb == "!home":
clear_console()
start_widget()
await select_options()
clear_console()
start_widget()
print(f"{Fore.CYAN}INFO {Fore.WHITE} Enter a activity content")
tresc = input(f"")
if tresc == "!back" or tresc == "!home":
clear_console()
start_widget()
await select_options()
if tryb == "1":
activity = discord.Game(name=tresc)
await bot.change_presence(activity=activity)
elif tryb == "2":
activity = discord.Activity(type=discord.ActivityType.watching, name=tresc)
await bot.change_presence(activity=activity)
elif tryb == "3":
activity = discord.Activity(type=discord.ActivityType.competing, name=tresc)
await bot.change_presence(activity=activity)
elif tryb == "4":
activity = discord.Activity(type=discord.ActivityType.listening, name=tresc)
await bot.change_presence(activity=activity)
elif tryb == "5":
activity = discord.CustomActivity(name=tresc)
await bot.change_presence(activity=activity)
elif tryb == "6":
await bot.change_presence(status=discord.Status.online)
clear_console()
start_widget()
await select_options()
await get_debug("Status set successfully")
async def change_name():
print(f"{Fore.CYAN}INFO {Fore.WHITE}You are in the edit view of your bot name")
print(f"{Fore.CYAN}INFO {Fore.WHITE}Enter a new name for your bot")
name = input(f"")
clear_console()
start_widget()
print(f"{Fore.CYAN}INFO {Fore.WHITE}You are trying to change your bot's name")
print(f"{Fore.RED}Old Name: {bot.user.name}")
print(f"{Fore.GREEN}New Name: {name}")
print(f"{Fore.CYAN}INFO{Fore.WHITE} Do you want to do this? To confirm, type {Fore.GREEN}O{Fore.WHITE}, to reject {Fore.RED}X{Fore.WHITE}")
decision = input(f"")
if decision == "O":
await bot.user.edit(username=name)
clear_console()
start_widget()
await get_debug(f"The bot name has been changed!")
await select_options()
else:
clear_console()
start_widget()
await select_action()
# - - - - - - - - - - - - - - - - - - - - - - DM - - - - - - - - - - - - - - - - - - - - - - #
async def send_dm(user_id):
recent_messages = deque(maxlen=10)
user = await bot.fetch_user(int(user_id))
async def display_recent_messages():
clear_console()
start_widget()
print(f"{Fore.CYAN}---------------- ChatBox ----------------")
print(f" {Fore.BLUE}INFO {Fore.WHITE}You chatting with: {user.name}")
print(f"{Fore.CYAN}---------------- ChatBox ----------------")
for msg in recent_messages:
if msg.author.id == bot.user.id:
print(f" {Fore.BLUE}You: {Fore.WHITE}{msg.content}")
else:
print(f" {Fore.BLUE}{msg.author.name}: {Fore.WHITE}{msg.content}")
print(f"{Fore.CYAN}---------------- ChatBox ----------------")
print("")
print(f"{Fore.CYAN}INFO {Fore.WHITE}You are in the DM chatbox, here you can see the chat with the user and send messages.")
print(f"{Fore.CYAN}INFO {Fore.WHITE}To undo your selection, use !back")
if not user:
print(f"{Fore.RED}ERROR {Fore.WHITE}User with ID {user_id} not found.")
return
async for message in user.history(limit=10, oldest_first=False):
recent_messages.appendleft(message)
await display_recent_messages()
async def listen_for_new_messages():
while True:
await asyncio.sleep(0.5)
async for message in user.history(limit=1, oldest_first=False):
if message not in recent_messages:
recent_messages.append(message)
await display_recent_messages()
break
listener_task = asyncio.create_task(listen_for_new_messages())
try:
while True:
message_content = await get_keypress()
if message_content.lower() == "!back":
listener_task.cancel()
clear_console()
start_widget()
await select_user()
elif message_content.lower() == "!home":
listener_task.cancel()
clear_console()
start_widget()
await select_options()
elif message_content.lower() == "!refresh":
await display_recent_messages()
elif message_content.lower() == "!help":
await display_recent_messages()
print(" ")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!help - View the help view (this view)")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!back - Go back to previous view")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!home - Go to the home view")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!delete - Delete the last message you sent")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!info - View information about last message")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!refresh - Refresh the chatbox")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!pin - Pin the last message")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!unpin - Unpin the last pinned message")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!reaction [emoji] - React to the last message with the specified emoji")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!reply [messge_id] [message_content] - Reply to a message with the specified content")
elif message_content.lower() == "!delete":
print(" ")
if recent_messages and recent_messages[-1].author.id == bot.user.id:
try:
await recent_messages[-1].delete()
recent_messages.pop()
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Last message deleted successfully.")
except Exception as e:
print(f"{Fore.RED}ERROR {Fore.WHITE}Failed to delete the last message: {e}")
else:
print(f"{Fore.RED}ERROR {Fore.WHITE}No message to delete or last message was not sent by you.")
elif message_content.lower() == "!info":
print(" ")
if recent_messages:
last_message = recent_messages[-1]
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Last message details:")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Author: {last_message.author.name}")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Content: {last_message.content}")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Sent at: {last_message.created_at}")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}ID: {last_message.id}")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}URL: {last_message.jump_url}")
else:
print(f"{Fore.RED}ERROR {Fore.WHITE}No messages available.")
elif message_content.lower() == "!pin":
print(" ")
if recent_messages:
try:
await recent_messages[-1].pin()
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Message pinned successfully.")
except Exception as e:
print(f"{Fore.RED}ERROR {Fore.WHITE}Failed to pin the message: {e}")
else:
print(f"{Fore.RED}ERROR {Fore.WHITE}No messages available to pin.")
elif message_content.lower() == "!unpin":
print(" ")
try:
pinned_messages = await user.pins()
if pinned_messages:
await pinned_messages[-1].unpin()
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Last pinned message unpinned successfully.")
else:
print(f"{Fore.RED}ERROR {Fore.WHITE}No pinned messages to unpin.")
except Exception as e:
print(f"{Fore.RED}ERROR {Fore.WHITE}Failed to unpin the message: {e}")
elif message_content.lower().startswith("!reaction"):
print(" ")
emoji = message_content.split(maxsplit=1)[1] if len(message_content.split()) > 1 else None
if emoji and recent_messages:
try:
await recent_messages[-1].add_reaction(emoji)
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Reaction {emoji} added successfully to the last message.")
except Exception as e:
print(f"{Fore.RED}ERROR {Fore.WHITE}Failed to add reaction: {e}")
else:
print(f"{Fore.RED}ERROR {Fore.WHITE}Invalid command or no messages available to react to. Use !reaction emoji.")
elif message_content.lower().startswith("!reply"):
parts = message_content.split(maxsplit=2)
if len(parts) == 3:
message_id, reply_content = parts[1], parts[2]
try:
message_to_reply = await user.fetch_message(int(message_id))
await message_to_reply.reply(reply_content)
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Replied successfully to message {message_id}.")
except Exception as e:
print(f"{Fore.RED}ERROR {Fore.WHITE}Failed to reply: {e}")
else:
print(f"{Fore.RED}ERROR {Fore.WHITE}Invalid command format. Use !reply id message.")
else:
try:
sent_message = await user.send(message_content, view=InfoButtom())
recent_messages.append(sent_message)
await display_recent_messages()
except Exception as e:
print(f"{Fore.RED}ERROR {Fore.WHITE}Failed to send the message: {e}")
except Exception as e:
await get_error(e)
listener_task.cancel()
async def select_user():
print(f"{Fore.CYAN}INFO {Fore.WHITE}Enter the recipient ID or @nickname of the DM message.")
print(f"{Fore.CYAN}INFO {Fore.WHITE}The user must share at least one server with the bot, or have started a conversation.")
while True:
input_value = await get_keypress()
if input_value == "!back" or input_value == "!home":
clear_console()
start_widget()
await select_options()
try:
user_id = await get_user_id_or_nick(input_value, bot)
await send_dm(user_id)
except ValueError:
await get_error("Invalid input!")
# - - - - - - - - - - - - - - - - - - - - - - Servers - - - - - - - - - - - - - - - - - - - - - - #
async def show_server_selection():
servers = list(bot.guilds)
print(f"{Fore.CYAN}INFO {Fore.WHITE}Select the server you want to view")
print(f"{Fore.CYAN}INFO {Fore.WHITE}To undo your selection, use !back")
print("")
for idx, guild in enumerate(servers):
print(f" {Fore.CYAN}[{idx + 1}] {Fore.WHITE}{guild.name}")
try:
while True:
key = await get_keypress()
if key.isdigit() and 1 <= int(key) <= len(servers):
selected_guild = servers[int(key) - 1]
clear_console()
start_widget()
await show_channel_selection(selected_guild)
else:
if key == "!back" or key == "!home":
clear_console()
start_widget()
await select_options()
except Exception as e:
await get_error(e)
async def show_channel_selection(guild):
channels = sorted(
[channel for channel in guild.channels if isinstance(channel, discord.TextChannel)],
key=lambda c: c.position
)
for idx, channel in enumerate(channels):
print(f" {Fore.CYAN}[{idx + 1}] {Fore.WHITE}{channel.name}")
print("")
print(f"{Fore.CYAN}INFO {Fore.WHITE}Select the channel you want to view")
print(f"{Fore.CYAN}INFO {Fore.WHITE}To undo your selection, use !back")
print("")
try:
while True:
key = await get_keypress()
if key.startswith("!create-channel"):
_, channel_name = key.split(maxsplit=1)
new_channel = await guild.create_text_channel(channel_name)
clear_console()
start_widget()
await show_channel_selection(guild=guild)
await get_debug(f"Channel '{new_channel.name}' created at position {new_channel.position}.")
elif key.startswith("!delete-channel"):
_, channel_number = key.split(maxsplit=1)
channel_number = int(channel_number) - 1
if 0 <= channel_number < len(channels):
channel_to_delete = channels[channel_number]
await channel_to_delete.delete()
clear_console()
start_widget()
await show_channel_selection(guild=guild)
await get_debug(f"Channel '{channel_to_delete.name}' deleted.")
else:
await get_error("Invalid channel number.")
elif key.startswith("!edit-channel"):
_, channel_number = key.split(maxsplit=1)
channel_number = int(channel_number) - 1
if 0 <= channel_number < len(channels):
channel = channels[channel_number]
print(f"{Fore.YELLOW}MSG {Fore.WHITE}{Fore.WHITE}Editing channel: {channel.name}")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}What would you like to edit?")
print(f"{Fore.YELLOW}MSG {Fore.WHITE} 1. Name")
print(f"{Fore.YELLOW}MSG {Fore.WHITE} 2. Topic")
print(f"{Fore.YELLOW}MSG {Fore.WHITE} 3. Slowmode")
edit_choice = input(f"")
if edit_choice == "1":
clear_console()
start_widget()
await show_channel_selection(guild=guild)
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Enter new name:")
new_name = input("")
await channel.edit(name=new_name)
clear_console()
start_widget()
await show_channel_selection(guild=guild)
await get_debug(f"Channel renamed to '{new_name}'.")
elif edit_choice == "2":
clear_console()
start_widget()
await show_channel_selection(guild=guild)
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Enter new topic:")
new_topic = input("")
await channel.edit(topic=new_topic)
clear_console()
start_widget()
await show_channel_selection(guild=guild)
await get_debug("Channel topic updated.")
elif edit_choice == "3":
clear_console()
start_widget()
await show_channel_selection(guild=guild)
print(f"{Fore.YELLOW}MSG {Fore.WHITE} Enter new slowmode delay (seconds): ")
new_slowmode = int(input(""))
await channel.edit(slowmode_delay=new_slowmode)
clear_console()
start_widget()
await show_channel_selection(guild=guild)
await get_debug(f"Channel slowmode updated to {new_slowmode} seconds.")
else:
await get_error("Invalid choice.")
else:
await get_error("Invalid channel number.")
elif key.startswith("!info-channel"):
_, channel_number = key.split(maxsplit=1)
channel_number = int(channel_number) - 1
if 0 <= channel_number < len(channels):
channel = channels[channel_number]
print(f"{Fore.YELLOW}MSG {Fore.WHITE}{Fore.WHITE}Channel Info:")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}ID: {channel.id}")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Name: {channel.name}")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Position: {channel.position}")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Topic: {channel.topic if channel.topic else 'No topic set'}")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Slowmode: {channel.slowmode_delay} seconds")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Permissions:")
for perm, value in channel.overwrites.items():
print(f"{Fore.YELLOW}MSG {perm}: {value}")
else:
await get_debug(f"Invalid channel number.")
elif key.isdigit() and 1 <= int(key) <= len(channels):
selected_channel = channels[int(key) - 1]
await send_message(selected_channel)
break
elif key == "!back":
clear_console()
start_widget()
await show_server_selection()
elif key == "!refresh":
clear_console()
start_widget()
await show_channel_selection(guild=guild)
elif key == "!home":
clear_console()
start_widget()
await select_options()
elif key == "!leave":
await guild.leave()
clear_console()
start_widget()
await show_server_selection()
elif key == "!help":
print(" ")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!help - View the help view (this view)")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!back - Go back to previous view")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!home - Go to the home view")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!refresh - Refresh this view")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!leave - Leave this server")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!create-channel [name] - Create a new text channel")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!delete-channel [number] - Delete a channel by its number")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!edit-channel [number] - Edit a channel's settings by its number")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!info-channel [number] - Show info about a channel")
else:
print(f"{Fore.RED}ERROR {Fore.WHITE}Unknown command or invalid input.")
except Exception as e:
await get_error(e)
async def send_message(channel):
recent_messages = deque(maxlen=10)
async def display_recent_messages():
clear_console()
start_widget()
print(f"{Fore.CYAN}---------------- ChatBox ----------------")
for msg in recent_messages:
if msg.author.name == bot.user.name:
print(f" {Fore.BLUE}You: {Fore.WHITE}{msg.content}")
else:
print(f" {Fore.BLUE}{msg.author.name}: {Fore.WHITE}{msg.content}")
print(f"{Fore.CYAN}---------------- ChatBox ----------------")
print("")
print(f"{Fore.CYAN}INFO {Fore.WHITE}You are in the chatbox, here you can see the chat from the selected channel and you can send messages.")
print(f"{Fore.CYAN}INFO {Fore.WHITE}To undo your selection, use !back")
async for message in channel.history(limit=10, oldest_first=False):
recent_messages.appendleft(message)
await display_recent_messages()
async def listen_for_new_messages():
while True:
await asyncio.sleep(0.5)
async for message in channel.history(limit=1, oldest_first=False):
if message not in recent_messages:
recent_messages.append(message)
await display_recent_messages()
break
listener_task = asyncio.create_task(listen_for_new_messages())
try:
while True:
message = await get_keypress()
if message.lower() == "!back":
listener_task.cancel()
clear_console()
start_widget()
await show_channel_selection(channel.guild)
break
elif message.lower() == "!home":
listener_task.cancel()
clear_console()
start_widget()
await select_options()
break
elif message.lower() == "!refresh":
await display_recent_messages()
elif message.lower() == "!help":
await display_recent_messages()
print(" ")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!help - View the help view (this view)")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!back - Go back to previous view")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!home - Go to the home view")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!delete - Delete the last message you sent")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!info - View information about last message")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!refresh - Refresh the chatbox")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!pin - Pin the last message")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!unpin - Unpin the last pinned message")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!reaction [emoji] - React to the last message with the specified emoji")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}!reply [message_id] [message_content] - Reply to a message with the specified content")
elif message.lower() == "!delete":
print(" ")
if recent_messages and recent_messages[-1].author.id == bot.user.id:
try:
await recent_messages[-1].delete()
recent_messages.pop()
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Last message deleted successfully.")
except Exception as e:
print(f"{Fore.RED}ERROR {Fore.WHITE}Failed to delete the last message: {e}")
else:
print(f"{Fore.RED}ERROR {Fore.WHITE}No message to delete or last message was not sent by you.")
elif message.lower() == "!info":
print(" ")
if recent_messages:
last_message = recent_messages[-1]
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Last message details:")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Author: {last_message.author.name}")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Content: {last_message.content}")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Sent at: {last_message.created_at}")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}ID: {last_message.id}")
print(f"{Fore.YELLOW}MSG {Fore.WHITE}URL: {last_message.jump_url}")
else:
print(f"{Fore.RED}ERROR {Fore.WHITE}No messages available.")
elif message.lower() == "!pin":
print(" ")
if recent_messages:
try:
await recent_messages[-1].pin()
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Message pinned successfully.")
except Exception as e:
print(f"{Fore.RED}ERROR {Fore.WHITE}Failed to pin the message: {e}")
else:
print(f"{Fore.RED}ERROR {Fore.WHITE}No messages available to pin.")
elif message.lower() == "!unpin":
print(" ")
try:
pinned_messages = await channel.pins()
if pinned_messages:
await pinned_messages[-1].unpin()
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Last pinned message unpinned successfully.")
else:
print(f"{Fore.RED}ERROR {Fore.WHITE}No pinned messages to unpin.")
except Exception as e:
print(f"{Fore.RED}ERROR {Fore.WHITE}Failed to unpin the message: {e}")
elif message.lower().startswith("!reaction"):
print(" ")
emoji = message.split(maxsplit=1)[1] if len(message.split()) > 1 else None
if emoji and recent_messages:
try:
await recent_messages[-1].add_reaction(emoji)
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Reaction {emoji} added successfully to the last message.")
except Exception as e:
print(f"{Fore.RED}ERROR {Fore.WHITE}Failed to add reaction: {e}")
else:
print(f"{Fore.RED}ERROR {Fore.WHITE}Invalid command or no messages available to react to. Use !reaction emoji.")
elif message.lower().startswith("!reply"):
print(" ")
parts = message.split(maxsplit=2)
if len(parts) == 3:
message_id, reply_content = parts[1], parts[2]
try:
message_to_reply = await channel.fetch_message(int(message_id))
await message_to_reply.reply(reply_content)
print(f"{Fore.YELLOW}MSG {Fore.WHITE}Replied successfully to message {message_id}.")
except Exception as e:
print(f"{Fore.RED}ERROR {Fore.WHITE}Failed to reply: {e}")
else:
print(f"{Fore.RED}ERROR {Fore.WHITE}Invalid command format. Use !reply id message.")
else:
try:
sent_message = await channel.send(message, view=InfoButtom())
recent_messages.append(sent_message)
await display_recent_messages()
except Exception as e:
print(f"{Fore.RED}ERROR {Fore.WHITE}Failed to send the message: {e}")
except Exception as e:
await get_error(e)
listener_task.cancel()
@bot.event
async def on_guild_join(guild):
bot_member = guild.get_member(bot.user.id)
if bot_member and bot_member.guild_permissions.administrator:
await get_debug(f"Joined new server: {guild.name} - Bot has admin rights.")
else:
clear_console()
start_widget()
await get_error(f"Joined new server: {guild.name} - Bot does NOT have admin rights.")
@bot.event
async def on_ready():
try:
clear_console()
start_widget()
await get_debug(f"Logged in as {bot.user}")
required_intents = ["messages", "guilds", "members"]
missing_intents = [intent for intent in required_intents if not getattr(intents, intent, False)]
if missing_intents:
await get_error(f"Missing intents: {', '.join(missing_intents)}")
return
for guild in bot.guilds:
bot_member = guild.get_member(bot.user.id)
if bot_member.guild_permissions.administrator:
continue
else:
await get_error(f"Bot does NOT have admin rights on server: {guild.name}")
return
try:
if STATUS_MODE == "Play":
activity = discord.Game(name=STATUS_CONTENT)
await bot.change_presence(activity=activity)
elif STATUS_MODE == "Watch":
activity = discord.Activity(type=discord.ActivityType.watching, name=STATUS_CONTENT)
await bot.change_presence(activity=activity)
elif STATUS_MODE == "Compet":
activity = discord.Activity(type=discord.ActivityType.competing, name=STATUS_CONTENT)
await bot.change_presence(activity=activity)
elif STATUS_MODE == "Listen":
activity = discord.Activity(type=discord.ActivityType.listening, name=STATUS_CONTENT)
await bot.change_presence(activity=activity)
elif STATUS_MODE == "Custom":
activity = discord.CustomActivity(name=STATUS_CONTENT)
await bot.change_presence(activity=activity)
elif STATUS_MODE == "None":
await bot.change_presence(status=discord.Status.online)
else:
return
await get_debug(f"Status has been setted")
except Exception as e:
await get_error("Problem with set a activity!")
await select_options()
except Exception as e:
await get_error(f"{str(e)}")
if LIBRUARY_STATUS == "ON":
with open(LIBRUARY_PATH, "r") as config_file:
preset = json5.load(config_file)
try:
LIBRUARY_PRESET = config.get("libruary_preset")
TOKEN = preset.get(LIBRUARY_PRESET)
except Exception as e:
clear_console()
start_widget()
print(f"{Fore.RED}ERROR{Fore.WHITE} Problem with import a token from TokenLibruary")
else:
TOKEN = config.get("token")
try:
if TOKEN == "Your Token":
start_widget()
print(f"{Fore.YELLOW} WARN{Fore.WHITE} You forgot to configure the program in the config.json5 file! If you don't know how to do this, read the documentation available on our github!")
else:
bot.run(TOKEN)
except Exception as e:
start_widget()
print(f"{Fore.RED}ERROR {Fore.WHITE} There was a problem starting the bot. Check the correctness of the token in the config!")
print(f"{Fore.YELLOW}TIP{Fore.WHITE} Check if the template was created correctly in the library and imported correctly in the config")
print(f"{Fore.YELLOW}TIP{Fore.WHITE} Check if your application has all three intents")