Skip to content

Commit

Permalink
Merge pull request #3 from surajstaabi/main
Browse files Browse the repository at this point in the history
File path + Exception handling
  • Loading branch information
mochiron-desu authored Aug 4, 2024
2 parents 58dd3fa + 88ba3af commit 5182762
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 67 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.env
.gitattributes
.gitignore
.venv
.venv/*

# config file
key.txt
Expand All @@ -13,4 +13,7 @@ data/mouse-ss.png
data/combined_image.png
data/webcam-capture.jpg
test.py
commands-to-be-added/*
commands-to-be-added/*

commands/__pycache__/*
/venv
40 changes: 37 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Discord Windows Controller Bot

The Discord Windows Controller Bot is a Python bot designed to control Windows operations through Discord commands. It can perform tasks like locking your workstation and monitoring mouse movement in a specified channel. Follow these steps to set up and run the bot.
The Discord Windows Controller Bot is a Python bot designed to control Windows operations through Discord commands. It can perform tasks like locking your workstation and monitoring mouse movement in a specified channel as well as saving files you send to the local workstation. Follow these steps to set up and run the bot.

## Step 1: Clone the Repository

Expand Down Expand Up @@ -45,6 +45,38 @@ Install the necessary libraries for your bot using the following command:
pip install -r requirements.txt
```

### Troubleshooting Installation Errors

If you encounter an error message similar to the following:

```
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
```

This error occurs because some packages require compilation, and your system is missing the necessary tools. To resolve this:

1. Install Microsoft Visual C++ Build Tools:
- Go to https://visualstudio.microsoft.com/visual-cpp-build-tools/
- Download and run the installer
- In the installer, select "C++ build tools" and install it

2. After installation, restart your computer.

3. Try running the pip install command again:

```bash
pip install -r requirements.txt
```

If you still face issues, try the following:

- Update pip and setuptools:
```bash
pip install --upgrade pip setuptools wheel
```

- If problems persist, please open an issue on the GitHub repository for further assistance.

## Step 5: Create a Discord Bot

1. Go to the Discord Developer Portal: [Discord Developer Portal](https://discord.com/developers/applications)
Expand All @@ -65,6 +97,7 @@ pip install -r requirements.txt
1. In the project folder, create a text file named "key.txt."
2. Paste the copied bot token into the "key.txt" file and save it.
3. Get your Channel ID and your own ID from Discord, and paste them into the "config.json" file.
4. Create a folder where you would want the files to be saved to locally, and copy the path to the `file_save_path` variable

Example:

Expand All @@ -73,7 +106,8 @@ Example:
"command_channel_id": 1137276176451079374,
"author_id": 637917562920429309,
"log_channel_id": 1137693326543122128,
"mouse_log_channel_id": 1137693326543123128
"mouse_log_channel_id": 1137693326543123128,
"file_save_path": "insert/file/path"
}
```

Expand All @@ -100,4 +134,4 @@ Example:
## Step 9: Test the Functionality

1. In the server where your bot is added, send a message with the content "lock" in the designated channel.
2. The bot should respond by locking the workstation and sending a confirmation message.
2. The bot should respond by locking the workstation and sending a confirmation message.
65 changes: 50 additions & 15 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import asyncio
import pyautogui
from commands.mouse_movement import monitor_mouse_movement
from aiohttp.client_exceptions import ClientConnectorError

KEY_PATH = "D:\Coding\Discord bots\python-windows-bot\key.txt"
CONFIG_PATH = "D:\Coding\Discord bots\python-windows-bot\config.json"
KEY_PATH = "key.txt"
CONFIG_PATH = "config.json"

def is_connected():
try:
# Try to resolve a common domain to check if the network is available
socket.create_connection(("www.google.com", 80))
return True
except OSError:
Expand All @@ -29,14 +29,14 @@ async def network_monitor():
if not is_connected():
print("Network connection lost. Waiting for network...")
while not is_connected():
await asyncio.sleep(5) # Wait for 5 seconds before checking again
await asyncio.sleep(5)
print("Network has been restored.")
await asyncio.sleep(60) # Check network status every 60 seconds
await asyncio.sleep(60)

async def main():
if os.path.exists(KEY_PATH):
with open(KEY_PATH, "r") as f:
TOKEN = f.read()
TOKEN = f.read().strip()
else:
TOKEN = ""

Expand All @@ -46,16 +46,13 @@ async def main():

client = discord.Client(intents=intents)

# Start the network monitoring task in the background
asyncio.create_task(network_monitor())

@client.event
async def on_ready():
print(f"We have logged in as {client.user}")
starting_mouse_position = pyautogui.position()
asyncio.create_task(
monitor_mouse_movement(client, config, starting_mouse_position)
) # Call the function
)

@client.event
async def on_message(message):
Expand All @@ -73,23 +70,61 @@ async def on_message(message):

if str(message.attachments) != "[]":
from commands.file_save import execute_file_save

await execute_file_save(client, message, config)

if msg == "lock":
from commands.lock_command import execute_lock_command

await execute_lock_command(client, message, config)

if msg == "ping":
from commands.ping_command import execute_ping_command

await execute_ping_command(client, message, config)

if msg == "ss" or msg == "screenshot":
from commands.screenshot_command import execute_screenshot_command
await execute_screenshot_command(message, args)

if msg == "help" or msg == "?":
from commands.help_command import execute_help_command
await execute_help_command(client, message, config)

async def start_bot():
while True:
try:
# Start the network monitoring task
network_task = asyncio.create_task(network_monitor())

# Start the client
await client.start(TOKEN)
except ClientConnectorError:
print("Failed to connect to Discord. Retrying in 30 seconds...")
await asyncio.sleep(30)
except KeyboardInterrupt:
print("Interrupt received. Shutting down gracefully...")
break
except Exception as e:
print(f"An unexpected error occurred: {e}")
print("Restarting the bot in 30 seconds...")
await asyncio.sleep(30)

await client.start(TOKEN)
try:
await start_bot()
finally:
# Cancel all running tasks
tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
for task in tasks:
task.cancel()

# Wait for all tasks to complete
await asyncio.gather(*tasks, return_exceptions=True)

# Close the client connection
await client.close()

print("Bot has been shut down.")

asyncio.run(main())
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print("Bot stopped.")
32 changes: 26 additions & 6 deletions commands/file_save.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
import json
import os

async def execute_file_save(client, message, config):
LOG_CHANNEL_ID = config["log_channel_id"]

# Load the configuration file
with open('config.json', 'r') as config_file:
full_config = json.load(config_file)

# Get the file save path from the config
file_save_path = full_config.get("file_save_path", "")

if not file_save_path:
await message.guild.get_channel(LOG_CHANNEL_ID).send("Error: file_save_path not configured in config.json")
print("[file_save_LOG] - Error: file_save_path not configured in config.json")
return

split_v1 = str(message.attachments).split("filename='")[1]
filename = str(split_v1).split("' ")[0]
await message.guild.get_channel(LOG_CHANNEL_ID).send(f"File Detected: {filename}")

try:
await message.attachments[0].save(
fp="D:/Coding\Discord bots/python-windows-bot/files/{}".format(filename)
) # saves the file
await message.guild.get_channel(LOG_CHANNEL_ID).send("File Sucessfully saved")
print(f"[file_save_LOG] - File Sucessfully saved.")
# Ensure the directory exists
os.makedirs(file_save_path, exist_ok=True)

# Construct the full file path
full_file_path = os.path.join(file_save_path, filename)

await message.attachments[0].save(fp=full_file_path) # saves the file
await message.guild.get_channel(LOG_CHANNEL_ID).send("File Successfully saved")
print(f"[file_save_LOG] - File Successfully saved to {full_file_path}")
except Exception as e:
await message.guild.get_channel(LOG_CHANNEL_ID).send(f"Error while saving: {e}")
print(f"[file_save_LOG] - Error while saving: {e}")
print(f"[file_save_LOG] - Error while saving: {e}")
33 changes: 33 additions & 0 deletions commands/help_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os
import discord
from discord.ext import commands

async def execute_help_command(client, message, config):
"""
Displays a list of all available commands and their descriptions.
Usage: !help or !?
"""
LOG_CHANNEL_ID = config["log_channel_id"]

commands_dir = r"D:\Coding\python-discord-windows-controller\commands"
command_files = [f for f in os.listdir(commands_dir) if f.endswith('.py') and f != '__init__.py']

embed = discord.Embed(title="Bot Commands", description="Here are all available commands:", color=discord.Color.blue())

for file in command_files:
command_name = file[:-3] # Remove .py extension
module = __import__(f"commands.{command_name}", fromlist=[''])

# Try to get the docstring of the execute function
try:
func = getattr(module, f"execute_{command_name}_command")
description = func.__doc__ or "No description available."
except AttributeError:
description = "No description available."

embed.add_field(name=f"!{command_name}", value=description, inline=False)

await message.channel.send(embed=embed)
print(f"[help_command_LOG] - Help command executed.")
await message.delete()
5 changes: 5 additions & 0 deletions commands/lock_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@


async def execute_lock_command(client, message, config):
"""
Locks the computer screen.
Usage: !lock
"""
LOG_CHANNEL_ID = config["log_channel_id"]

command = "rundll32.exe user32.dll,LockWorkStation"
Expand Down
Loading

0 comments on commit 5182762

Please sign in to comment.