|
5 | 5 | import asyncio |
6 | 6 | import pyautogui |
7 | 7 | from commands.mouse_movement import monitor_mouse_movement |
| 8 | +from aiohttp.client_exceptions import ClientConnectorError |
8 | 9 |
|
9 | 10 | KEY_PATH = "key.txt" |
10 | 11 | CONFIG_PATH = "config.json" |
@@ -35,7 +36,7 @@ async def network_monitor(): |
35 | 36 | async def main(): |
36 | 37 | if os.path.exists(KEY_PATH): |
37 | 38 | with open(KEY_PATH, "r") as f: |
38 | | - TOKEN = f.read() |
| 39 | + TOKEN = f.read().strip() |
39 | 40 | else: |
40 | 41 | TOKEN = "" |
41 | 42 |
|
@@ -87,14 +88,27 @@ async def on_message(message): |
87 | 88 | from commands.help_command import execute_help_command |
88 | 89 | await execute_help_command(client, message, config) |
89 | 90 |
|
| 91 | + async def start_bot(): |
| 92 | + while True: |
| 93 | + try: |
| 94 | + # Start the network monitoring task |
| 95 | + network_task = asyncio.create_task(network_monitor()) |
| 96 | + |
| 97 | + # Start the client |
| 98 | + await client.start(TOKEN) |
| 99 | + except ClientConnectorError: |
| 100 | + print("Failed to connect to Discord. Retrying in 30 seconds...") |
| 101 | + await asyncio.sleep(30) |
| 102 | + except KeyboardInterrupt: |
| 103 | + print("Interrupt received. Shutting down gracefully...") |
| 104 | + break |
| 105 | + except Exception as e: |
| 106 | + print(f"An unexpected error occurred: {e}") |
| 107 | + print("Restarting the bot in 30 seconds...") |
| 108 | + await asyncio.sleep(30) |
| 109 | + |
90 | 110 | try: |
91 | | - # Start the network monitoring task |
92 | | - network_task = asyncio.create_task(network_monitor()) |
93 | | - |
94 | | - # Start the client |
95 | | - await client.start(TOKEN) |
96 | | - except KeyboardInterrupt: |
97 | | - print("Interrupt received. Shutting down gracefully...") |
| 111 | + await start_bot() |
98 | 112 | finally: |
99 | 113 | # Cancel all running tasks |
100 | 114 | tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()] |
|
0 commit comments