Skip to content

Commit a8948ba

Browse files
committed
Added Screenshot command and fixed network monitor
1 parent 3dce957 commit a8948ba

File tree

7 files changed

+45
-13
lines changed

7 files changed

+45
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ config.json
99
files/*
1010
data/data.json
1111
data/screenshot.png
12+
data/mouse-ss.png
1213
test.py

bot.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@
99
KEY_PATH = "D:\Coding\Discord bots\python-windows-bot\key.txt"
1010
CONFIG_PATH = "D:\Coding\Discord bots\python-windows-bot\config.json"
1111

12-
1312
def is_connected():
1413
try:
15-
# Try to resolve a common domain to check if network is available
14+
# Try to resolve a common domain to check if the network is available
1615
socket.create_connection(("www.google.com", 80))
1716
return True
1817
except OSError:
1918
pass
2019
return False
2120

22-
2321
def load_config():
2422
print("Loading configuration file...")
2523
with open(CONFIG_PATH, "r") as config_file:
2624
config = json.load(config_file)
2725
return config
2826

29-
30-
async def main():
27+
async def network_monitor():
3128
while True:
32-
if is_connected():
33-
break
34-
print("Waiting for network...")
35-
await asyncio.sleep(5) # Wait for 5 seconds before checking again
29+
if not is_connected():
30+
print("Network connection lost. Waiting for network...")
31+
while not is_connected():
32+
await asyncio.sleep(5) # Wait for 5 seconds before checking again
33+
print("Network has been restored.")
34+
await asyncio.sleep(60) # Check network status every 60 seconds
3635

36+
async def main():
3737
if os.path.exists(KEY_PATH):
3838
with open(KEY_PATH, "r") as f:
3939
TOKEN = f.read()
@@ -46,6 +46,9 @@ async def main():
4646

4747
client = discord.Client(intents=intents)
4848

49+
# Start the network monitoring task in the background
50+
asyncio.create_task(network_monitor())
51+
4952
@client.event
5053
async def on_ready():
5154
print(f"We have logged in as {client.user}")
@@ -56,7 +59,8 @@ async def on_ready():
5659

5760
@client.event
5861
async def on_message(message):
59-
msg = str(message.content).lower()
62+
msg = str(message.content).lower().split(' ')[0]
63+
args = message.content.split(" ")[1:]
6064

6165
if (
6266
message.author == client.user
@@ -66,7 +70,7 @@ async def on_message(message):
6670

6771
if message.author.bot or message.author.id != config["author_id"]:
6872
return
69-
73+
7074
if str(message.attachments) != "[]":
7175
from commands.file_save import execute_file_save
7276

@@ -81,8 +85,11 @@ async def on_message(message):
8185
from commands.ping_command import execute_ping_command
8286

8387
await execute_ping_command(client, message, config)
88+
89+
if msg == "ss" or msg == "screenshot":
90+
from commands.screenshot_command import execute_screenshot_command
91+
await execute_screenshot_command(message, args)
8492

8593
await client.start(TOKEN)
8694

87-
8895
asyncio.run(main())
42 Bytes
Binary file not shown.
-2 Bytes
Binary file not shown.
1.8 KB
Binary file not shown.

commands/mouse_movement.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from PIL import ImageGrab
99

1010
DATA_JSON_PATH = "D:\Coding\Discord bots\python-windows-bot\data\data.json"
11-
SCREENSHOT_PATH = "D:\Coding\Discord bots\python-windows-bot\data\screenshot.png"
11+
SCREENSHOT_PATH = "D:\Coding\Discord bots\python-windows-bot\data\mouse-ss.png"
1212

1313

1414
async def monitor_mouse_movement(client, config, starting_mouse_position):

commands/screenshot_command.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import discord
2+
import datetime
3+
from PIL import ImageGrab
4+
5+
SCREENSHOT_PATH = "D:\Coding\Discord bots\python-windows-bot\data\screenshot.png"
6+
7+
def get_ss():
8+
screenshot = ImageGrab.grab()
9+
screenshot.save(SCREENSHOT_PATH)
10+
return SCREENSHOT_PATH
11+
12+
async def execute_screenshot_command(message,args):
13+
time = datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S")
14+
MESSAGE = f"Screenshot at {time}"
15+
PATH = get_ss()
16+
ss_message = await message.channel.send(MESSAGE, file=discord.File(PATH))
17+
await message.delete()
18+
print(f"[screenshot_command_LOG] - Screenshot taken at {time}.")
19+
if("-p" in args):
20+
print("Persistent screenshot requested")
21+
else:
22+
time.sleep(5)
23+
await ss_message.delete()
24+
print("Screenshot Deleted")

0 commit comments

Comments
 (0)