-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from surajstaabi/main
File path + Exception handling
- Loading branch information
Showing
10 changed files
with
211 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.