From 1e220c9bd079c27e21444331ab1b21d0af413f18 Mon Sep 17 00:00:00 2001 From: zeroquinc Date: Thu, 27 Jun 2024 10:35:01 +0200 Subject: [PATCH 01/12] feature: add disk space voice channel --- src/discord/cogs/tasks.py | 27 ++++++++++++++++++++++++++- src/linux/disk_space.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/linux/disk_space.py diff --git a/src/discord/cogs/tasks.py b/src/discord/cogs/tasks.py index 7521fd6..a09b763 100644 --- a/src/discord/cogs/tasks.py +++ b/src/discord/cogs/tasks.py @@ -5,7 +5,7 @@ from utils.custom_logger import logger from utils.datetime import TimeCalculator from src.trakt.functions import process_ratings, process_favorites -from config.globals import TRAKT_CHANNEL, TRAKT_USERNAME, ENABLE_DELAY +from config.globals import TRAKT_CHANNEL, TRAKT_USERNAME, ENABLE_DELAY, DISCORD_SERVER_ID class TasksCog(commands.Cog): def __init__(self, bot: commands.Bot) -> None: @@ -30,6 +30,26 @@ async def trakt_favorites(self): await process_favorites(favorites_channel, TRAKT_USERNAME) except Exception as e: logger.error(f'Error processing recent Trakt ratings: {e}') + + @tasks.loop(hours=12) + async def update_disk_space_channel(self): + await self.bot.wait_until_ready() + space = self.get_disk_space() + guild_id = DISCORD_SERVER_ID + guild = self.bot.get_guild(guild_id) + + if guild is not None: + # Check for any channel with "Disk Space" in its name + disk_space_channel = next((channel for channel in guild.channels if "Disk Space" in channel.name), None) + + if disk_space_channel is not None: + # If a "Disk Space" channel exists, update its name + await disk_space_channel.edit(name=space) + else: + # If no "Disk Space" channel exists, create a new voice channel + await guild.create_voice_channel(name=space) + else: + print(f"Guild with ID {guild_id} not found.") @trakt_ratings.before_loop async def before_trakt_ratings(self): @@ -44,6 +64,11 @@ async def before_trakt_favorites(self): seconds = TimeCalculator.seconds_until_next_day() logger.info(f'Trakt favorites task will start in {str(timedelta(seconds=seconds))}') await asyncio.sleep(seconds) + + @update_disk_space_channel.before_loop + async def before_update_disk_space_channel(self): + logger.info('Waiting for bot to be ready...') + await self.bot.wait_until_ready() async def setup(bot): logger.info('Cogs have been loaded') diff --git a/src/linux/disk_space.py b/src/linux/disk_space.py new file mode 100644 index 0000000..c8cab91 --- /dev/null +++ b/src/linux/disk_space.py @@ -0,0 +1,33 @@ +import subprocess + +def get_disk_space() -> str: + target_partition = '/dev/sdb' + + try: + result = subprocess.run(['df', '-BG'], capture_output=True, text=True, check=True) + except subprocess.CalledProcessError as e: + return f"Error running df command: {e}" + + lines = result.stdout.splitlines() + if len(lines) < 2: + return "Unexpected output format from df command" + + for line in lines[1:]: + parts = line.split() + if parts[0] == target_partition: + headers = lines[0].split() + values = line.split() + if "Available" in headers and "1G-blocks" in headers: + available_space_index = headers.index("Available") + total_space_index = headers.index("1G-blocks") + elif "Avail" in headers and "Size" in headers: # Some versions of 'df' abbreviate 'Available' as 'Avail' + available_space_index = headers.index("Avail") + total_space_index = headers.index("Size") + else: + return "Could not find space information in df output" + + available_space_gb = values[available_space_index].rstrip('G') + total_space_gb = values[total_space_index].rstrip('G') + return f"{available_space_gb} GB / {total_space_gb} GB" + + return f"No data found for {target_partition}" \ No newline at end of file From 71f694c4f3dec1f2c5394c0435e789374d60daa5 Mon Sep 17 00:00:00 2001 From: zeroquinc Date: Thu, 27 Jun 2024 10:40:35 +0200 Subject: [PATCH 02/12] remove loop --- src/discord/cogs/tasks.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/discord/cogs/tasks.py b/src/discord/cogs/tasks.py index a09b763..06a602d 100644 --- a/src/discord/cogs/tasks.py +++ b/src/discord/cogs/tasks.py @@ -64,11 +64,6 @@ async def before_trakt_favorites(self): seconds = TimeCalculator.seconds_until_next_day() logger.info(f'Trakt favorites task will start in {str(timedelta(seconds=seconds))}') await asyncio.sleep(seconds) - - @update_disk_space_channel.before_loop - async def before_update_disk_space_channel(self): - logger.info('Waiting for bot to be ready...') - await self.bot.wait_until_ready() async def setup(bot): logger.info('Cogs have been loaded') From e0f8db6cc901fb4ee568f8d0ed1d35b27ce56d1c Mon Sep 17 00:00:00 2001 From: zeroquinc Date: Thu, 27 Jun 2024 10:42:05 +0200 Subject: [PATCH 03/12] remove bot ready --- src/discord/cogs/tasks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/discord/cogs/tasks.py b/src/discord/cogs/tasks.py index 06a602d..a87dccc 100644 --- a/src/discord/cogs/tasks.py +++ b/src/discord/cogs/tasks.py @@ -30,10 +30,10 @@ async def trakt_favorites(self): await process_favorites(favorites_channel, TRAKT_USERNAME) except Exception as e: logger.error(f'Error processing recent Trakt ratings: {e}') - + + # Task to update the disk space channel @tasks.loop(hours=12) async def update_disk_space_channel(self): - await self.bot.wait_until_ready() space = self.get_disk_space() guild_id = DISCORD_SERVER_ID guild = self.bot.get_guild(guild_id) From 7abcf7c7d74d6800d209d5f9fdd4f9f021a8d181 Mon Sep 17 00:00:00 2001 From: zeroquinc Date: Thu, 27 Jun 2024 10:45:00 +0200 Subject: [PATCH 04/12] import get disk space functions in cog --- src/discord/cogs/tasks.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/discord/cogs/tasks.py b/src/discord/cogs/tasks.py index a87dccc..6c831ec 100644 --- a/src/discord/cogs/tasks.py +++ b/src/discord/cogs/tasks.py @@ -5,6 +5,7 @@ from utils.custom_logger import logger from utils.datetime import TimeCalculator from src.trakt.functions import process_ratings, process_favorites +from src.linux.disk_space import get_disk_space from config.globals import TRAKT_CHANNEL, TRAKT_USERNAME, ENABLE_DELAY, DISCORD_SERVER_ID class TasksCog(commands.Cog): @@ -34,9 +35,10 @@ async def trakt_favorites(self): # Task to update the disk space channel @tasks.loop(hours=12) async def update_disk_space_channel(self): - space = self.get_disk_space() + space = get_disk_space() guild_id = DISCORD_SERVER_ID guild = self.bot.get_guild(guild_id) + logger.info(f"Guild ID: {guild_id}, Space: {space}") if guild is not None: # Check for any channel with "Disk Space" in its name @@ -49,7 +51,7 @@ async def update_disk_space_channel(self): # If no "Disk Space" channel exists, create a new voice channel await guild.create_voice_channel(name=space) else: - print(f"Guild with ID {guild_id} not found.") + logger.info(f"Guild with ID {guild_id} not found.") @trakt_ratings.before_loop async def before_trakt_ratings(self): From 20371d0eb6ca4ed595f343c9b94917d27a7d3828 Mon Sep 17 00:00:00 2001 From: zeroquinc Date: Thu, 27 Jun 2024 10:47:47 +0200 Subject: [PATCH 05/12] add more logging and see if the channel name is to long --- src/discord/cogs/tasks.py | 33 +++++++++++++++++---------------- src/linux/disk_space.py | 3 +++ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/discord/cogs/tasks.py b/src/discord/cogs/tasks.py index 6c831ec..15ba85f 100644 --- a/src/discord/cogs/tasks.py +++ b/src/discord/cogs/tasks.py @@ -35,23 +35,24 @@ async def trakt_favorites(self): # Task to update the disk space channel @tasks.loop(hours=12) async def update_disk_space_channel(self): - space = get_disk_space() - guild_id = DISCORD_SERVER_ID - guild = self.bot.get_guild(guild_id) - logger.info(f"Guild ID: {guild_id}, Space: {space}") - - if guild is not None: - # Check for any channel with "Disk Space" in its name - disk_space_channel = next((channel for channel in guild.channels if "Disk Space" in channel.name), None) - - if disk_space_channel is not None: - # If a "Disk Space" channel exists, update its name - await disk_space_channel.edit(name=space) + try: + space = get_disk_space() + guild_id = DISCORD_SERVER_ID + guild = self.bot.get_guild(guild_id) + logger.info(f"Guild ID: {guild_id}, Space: {space}") + + if guild is not None: + disk_space_channel = next((channel for channel in guild.channels if "Disk Space" in channel.name), None) + + if disk_space_channel is not None: + # Truncate or format the space string to fit Discord channel name limitations here if necessary + await disk_space_channel.edit(name=f"Disk Space - {space}") + else: + await guild.create_voice_channel(name=f"Disk Space - {space}") else: - # If no "Disk Space" channel exists, create a new voice channel - await guild.create_voice_channel(name=space) - else: - logger.info(f"Guild with ID {guild_id} not found.") + logger.info(f"Guild with ID {guild_id} not found.") + except Exception as e: + logger.error(f"Failed to update disk space channel: {e}") @trakt_ratings.before_loop async def before_trakt_ratings(self): diff --git a/src/linux/disk_space.py b/src/linux/disk_space.py index c8cab91..0391085 100644 --- a/src/linux/disk_space.py +++ b/src/linux/disk_space.py @@ -1,5 +1,7 @@ import subprocess +from utils.custom_logger import logger + def get_disk_space() -> str: target_partition = '/dev/sdb' @@ -28,6 +30,7 @@ def get_disk_space() -> str: available_space_gb = values[available_space_index].rstrip('G') total_space_gb = values[total_space_index].rstrip('G') + logger.info(f"Available space: {available_space_gb} GB, Total space: {total_space_gb} GB") return f"{available_space_gb} GB / {total_space_gb} GB" return f"No data found for {target_partition}" \ No newline at end of file From 884818c4c1fe2979e68231b330af79dcd90494ca Mon Sep 17 00:00:00 2001 From: zeroquinc Date: Thu, 27 Jun 2024 10:55:16 +0200 Subject: [PATCH 06/12] starting the task at top of cog --- src/discord/cogs/tasks.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/discord/cogs/tasks.py b/src/discord/cogs/tasks.py index 15ba85f..d562649 100644 --- a/src/discord/cogs/tasks.py +++ b/src/discord/cogs/tasks.py @@ -13,6 +13,7 @@ def __init__(self, bot: commands.Bot) -> None: self.bot = bot self.trakt_ratings.start() self.trakt_favorites.start() + self.update_disk_space_channel.start() # Task to process recent Trakt ratings @tasks.loop(minutes=60) From 50c1a9b8318d926f446383cd1686160a7746ff93 Mon Sep 17 00:00:00 2001 From: zeroquinc Date: Thu, 27 Jun 2024 11:00:15 +0200 Subject: [PATCH 07/12] Use fetch_guild instead of get_guild --- src/discord/cogs/tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/discord/cogs/tasks.py b/src/discord/cogs/tasks.py index d562649..cb9f8f6 100644 --- a/src/discord/cogs/tasks.py +++ b/src/discord/cogs/tasks.py @@ -39,7 +39,7 @@ async def update_disk_space_channel(self): try: space = get_disk_space() guild_id = DISCORD_SERVER_ID - guild = self.bot.get_guild(guild_id) + guild = await self.bot.fetch_guild(guild_id) logger.info(f"Guild ID: {guild_id}, Space: {space}") if guild is not None: From db0ddca2552c579cbc3290ea4fe8c6713945587b Mon Sep 17 00:00:00 2001 From: zeroquinc Date: Thu, 27 Jun 2024 11:02:46 +0200 Subject: [PATCH 08/12] format voice channel name --- src/discord/cogs/tasks.py | 5 ++--- src/linux/disk_space.py | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/discord/cogs/tasks.py b/src/discord/cogs/tasks.py index cb9f8f6..6dc32be 100644 --- a/src/discord/cogs/tasks.py +++ b/src/discord/cogs/tasks.py @@ -40,16 +40,15 @@ async def update_disk_space_channel(self): space = get_disk_space() guild_id = DISCORD_SERVER_ID guild = await self.bot.fetch_guild(guild_id) - logger.info(f"Guild ID: {guild_id}, Space: {space}") if guild is not None: disk_space_channel = next((channel for channel in guild.channels if "Disk Space" in channel.name), None) if disk_space_channel is not None: # Truncate or format the space string to fit Discord channel name limitations here if necessary - await disk_space_channel.edit(name=f"Disk Space - {space}") + await disk_space_channel.edit(name=f"Free: {space}") else: - await guild.create_voice_channel(name=f"Disk Space - {space}") + await guild.create_voice_channel(name=f"Free: {space}") else: logger.info(f"Guild with ID {guild_id} not found.") except Exception as e: diff --git a/src/linux/disk_space.py b/src/linux/disk_space.py index 0391085..a4aaff5 100644 --- a/src/linux/disk_space.py +++ b/src/linux/disk_space.py @@ -31,6 +31,6 @@ def get_disk_space() -> str: available_space_gb = values[available_space_index].rstrip('G') total_space_gb = values[total_space_index].rstrip('G') logger.info(f"Available space: {available_space_gb} GB, Total space: {total_space_gb} GB") - return f"{available_space_gb} GB / {total_space_gb} GB" + return f"{available_space_gb}GB / {total_space_gb}GB" return f"No data found for {target_partition}" \ No newline at end of file From ed2a11203e424a09cc9aa5343666a8e8ac053a93 Mon Sep 17 00:00:00 2001 From: zeroquinc Date: Thu, 27 Jun 2024 11:06:45 +0200 Subject: [PATCH 09/12] disallow connecting to the voice channel --- src/discord/cogs/tasks.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/discord/cogs/tasks.py b/src/discord/cogs/tasks.py index 6dc32be..7af2d85 100644 --- a/src/discord/cogs/tasks.py +++ b/src/discord/cogs/tasks.py @@ -1,3 +1,4 @@ +import discord from discord.ext import tasks, commands import asyncio from datetime import timedelta @@ -42,13 +43,19 @@ async def update_disk_space_channel(self): guild = await self.bot.fetch_guild(guild_id) if guild is not None: - disk_space_channel = next((channel for channel in guild.channels if "Disk Space" in channel.name), None) + disk_space_channel = next((channel for channel in guild.channels if "HDD:" in channel.name), None) + + # Define the permission overwrite for @everyone role to disallow connect + overwrites = { + guild.default_role: discord.PermissionOverwrite(connect=False) + } if disk_space_channel is not None: - # Truncate or format the space string to fit Discord channel name limitations here if necessary - await disk_space_channel.edit(name=f"Free: {space}") + # Edit the existing channel with the updated permissions + await disk_space_channel.edit(name=f"HDD: {space}", overwrites=overwrites) else: - await guild.create_voice_channel(name=f"Free: {space}") + # Create a new voice channel with the specified permissions + await guild.create_voice_channel(name=f"HDD: {space}", overwrites=overwrites) else: logger.info(f"Guild with ID {guild_id} not found.") except Exception as e: From dd798635837d0b47e0c4af533717681e88dad0a6 Mon Sep 17 00:00:00 2001 From: zeroquinc Date: Thu, 27 Jun 2024 11:09:23 +0200 Subject: [PATCH 10/12] edit startswith HDD instead of the full voice channel --- src/discord/cogs/tasks.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/discord/cogs/tasks.py b/src/discord/cogs/tasks.py index 7af2d85..e10f3bb 100644 --- a/src/discord/cogs/tasks.py +++ b/src/discord/cogs/tasks.py @@ -43,7 +43,8 @@ async def update_disk_space_channel(self): guild = await self.bot.fetch_guild(guild_id) if guild is not None: - disk_space_channel = next((channel for channel in guild.channels if "HDD:" in channel.name), None) + # Look for a channel that starts with "HDD:" + disk_space_channel = next((channel for channel in guild.channels if channel.name.startswith("HDD:")), None) # Define the permission overwrite for @everyone role to disallow connect overwrites = { From 4f2e416006046348f9c3b192ad68177697a8d56b Mon Sep 17 00:00:00 2001 From: zeroquinc Date: Thu, 27 Jun 2024 11:11:33 +0200 Subject: [PATCH 11/12] add extra logging --- src/discord/cogs/tasks.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/discord/cogs/tasks.py b/src/discord/cogs/tasks.py index e10f3bb..c3385e7 100644 --- a/src/discord/cogs/tasks.py +++ b/src/discord/cogs/tasks.py @@ -41,20 +41,23 @@ async def update_disk_space_channel(self): space = get_disk_space() guild_id = DISCORD_SERVER_ID guild = await self.bot.fetch_guild(guild_id) - + if guild is not None: # Look for a channel that starts with "HDD:" disk_space_channel = next((channel for channel in guild.channels if channel.name.startswith("HDD:")), None) - - # Define the permission overwrite for @everyone role to disallow connect - overwrites = { - guild.default_role: discord.PermissionOverwrite(connect=False) - } - + if disk_space_channel is not None: - # Edit the existing channel with the updated permissions - await disk_space_channel.edit(name=f"HDD: {space}", overwrites=overwrites) + # Log found channel for debugging + logger.info(f"Found existing channel: {disk_space_channel.name}") + # Edit the existing channel + await disk_space_channel.edit(name=f"HDD: {space}") else: + # Log that no channel was found and a new one will be created + logger.info("No existing HDD: channel found, creating a new one.") + # Define the permission overwrite for @everyone role to disallow connect + overwrites = { + guild.default_role: discord.PermissionOverwrite(connect=False) + } # Create a new voice channel with the specified permissions await guild.create_voice_channel(name=f"HDD: {space}", overwrites=overwrites) else: From 8778aae95775484664d062672b950bf5ddffb9f4 Mon Sep 17 00:00:00 2001 From: zeroquinc Date: Thu, 27 Jun 2024 11:13:42 +0200 Subject: [PATCH 12/12] editing existing channel if channel is already made --- src/discord/cogs/tasks.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/discord/cogs/tasks.py b/src/discord/cogs/tasks.py index c3385e7..ffbf680 100644 --- a/src/discord/cogs/tasks.py +++ b/src/discord/cogs/tasks.py @@ -43,22 +43,22 @@ async def update_disk_space_channel(self): guild = await self.bot.fetch_guild(guild_id) if guild is not None: + # Explicitly fetch channels to ensure we have the latest state + channels = await guild.fetch_channels() + # Log all channel names for debugging + logger.debug(f"All channels: {[channel.name for channel in channels]}") + # Look for a channel that starts with "HDD:" - disk_space_channel = next((channel for channel in guild.channels if channel.name.startswith("HDD:")), None) + disk_space_channel = next((channel for channel in channels if channel.name.startswith("HDD:")), None) if disk_space_channel is not None: - # Log found channel for debugging logger.info(f"Found existing channel: {disk_space_channel.name}") - # Edit the existing channel await disk_space_channel.edit(name=f"HDD: {space}") else: - # Log that no channel was found and a new one will be created logger.info("No existing HDD: channel found, creating a new one.") - # Define the permission overwrite for @everyone role to disallow connect overwrites = { guild.default_role: discord.PermissionOverwrite(connect=False) } - # Create a new voice channel with the specified permissions await guild.create_voice_channel(name=f"HDD: {space}", overwrites=overwrites) else: logger.info(f"Guild with ID {guild_id} not found.")