Skip to content

Commit

Permalink
fixed bugs, code improvements. community clone is working again
Browse files Browse the repository at this point in the history
  • Loading branch information
itskekoff committed Aug 21, 2024
1 parent 0af5f83 commit 082d235
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
9 changes: 3 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import os
import sys
import time

from datetime import datetime

Expand All @@ -13,9 +12,9 @@
from modules.logger import Logger
from modules.configuration import Configuration, check_missing_keys
from modules.updater import Updater
from modules.utilities import get_command_info, format_time
from modules.utilities import get_command_info

VERSION = "1.4.6"
VERSION = "1.4.7"

config_path = "config.json"
data: Configuration = Configuration(config_path)
Expand Down Expand Up @@ -120,9 +119,7 @@

if clone_channels and (not clone_roles and clone_overwrites):
clone_roles = True
logger.warning(
"Clone roles enabled because clone overwrites and channels are enabled."
)
logger.warning("Clone roles enabled because clone overwrites and channels are enabled.")

if live_update_enabled and not clone_channels:
logger.error("Live update disabled because clone channels is disabled.")
Expand Down
46 changes: 31 additions & 15 deletions modules/cloner.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ async def populate_queue(self, limit: int = 512):
try:
original_channel: discord.TextChannel = await self.guild.fetch_channel(channel_id)

if isinstance(original_channel, discord.ForumChannel) or isinstance(original_channel, discord.StageChannel):
if isinstance(original_channel, discord.ForumChannel) or isinstance(original_channel,
discord.StageChannel):
continue

async for message in original_channel.history(limit=limit, oldest_first=self.clone_oldest_first):
self.message_queue.append((new_channel, message))
except discord.Forbidden:
logger.debug(f"Can't fetch channel message history (no permissions): {channel_id}")
self.logger.debug(f"Can't fetch channel message history (no permissions): {channel_id}")
continue

async def prepare_server(self) -> None:
Expand Down Expand Up @@ -246,7 +247,7 @@ async def clone_channels(self, perms: bool = True) -> None:
try:
channel = await self.guild.fetch_channel(channel.id)
except discord.Forbidden:
logger.debug(f"Can't fetch channel {channel.name} | {channel.id}")
self.logger.debug(f"Can't fetch channel {channel.name} | {channel.id}")
continue

category = None
Expand Down Expand Up @@ -284,35 +285,38 @@ async def clone_channels(self, perms: bool = True) -> None:

if self.enabled_community:
self.logger.info("Processing community settings")
await self.process_community()

self.logger.info("Processing community channels")
await self.add_community_channels(perms=perms)
if await self.process_community():
self.logger.info("Processing community channels")
await self.add_community_channels(perms=perms)

async def process_community(self) -> None:
async def process_community(self) -> bool:
"""
Applies community-related settings to the new guild such as AFK settings, verification level, notification settings, and system channel flags.
"""
if self.enabled_community:
afk_channel, system_channel = None, None
if self.guild.afk_channel is not None:
afk_channel = self.mappings["channels"][self.guild.afk_channel.id]
if self.guild.system_channel is not None:
system_channel = self.mappings["channels"][self.guild.system_channel]
afk_channel = self._get_channel_from_mapping(self.guild.afk_channel)
system_channel = self._get_channel_from_mapping(self.guild.system_channel)
public_updates = self._get_channel_from_mapping(self.guild.public_updates_channel)
rules_channel = self._get_channel_from_mapping(self.guild.rules_channel)

if not public_updates:
self.logger.error("Can't create community: missing access to public updates channel")
return False

await self.new_guild.edit(community=True, verification_level=self.guild.verification_level,
default_notifications=self.guild.default_notifications, afk_channel=afk_channel,
afk_timeout=self.guild.afk_timeout,
system_channel=system_channel,
system_channel_flags=self.guild.system_channel_flags,
rules_channel=self.mappings["channels"][self.guild.rules_channel.id],
public_updates_channel=self.mappings["channels"][
self.guild.public_updates_channel.id],
rules_channel=rules_channel,
public_updates_channel=public_updates,
explicit_content_filter=self.guild.explicit_content_filter,
preferred_locale=self.guild.preferred_locale)
if self.debug:
self.logger.debug("Updated guild community settings")
await asyncio.sleep(self.delay)
return True

async def add_community_channels(self, perms: bool = True) -> None:
"""
Expand Down Expand Up @@ -527,6 +531,18 @@ async def clone_messages_from_queue(self, clear_webhooks: bool = main.messages_w

await self.cleanup_after_cloning(clear=clear_webhooks)

def _get_channel_from_mapping(self, channel):
"""
Retrieves the corresponding mapped channel if it exists and the channel is not None.
Args:
channel (discord.Channel or None): The channel object to retrieve from the mappings.
If None, the method returns None.
"""
if channel:
return self.mappings["channels"].get(channel.id)
return None

async def _process_messages_channel_map(self, channel_messages_map):
"""
Processes messages for each channel in the given map.
Expand Down
7 changes: 4 additions & 3 deletions modules/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ async def get_first_frame(image: discord.Asset) -> bytes:

def format_time(delta: timedelta) -> str:
"""
Formats a timedelta object into a readable English string.
Formats a timedelta object into a readable string.
Args:
delta (timedelta): A timedelta object representing a duration.
Returns:
str: A formatted time string in the format "X days Y hours Z minutes W seconds".
The singular or plural form of words (day/hour/minute/second) is used
str: A formatted time string in the format "X years X days Y hours Z minutes W seconds".
The singular or plural form of words (year/day/hour/minute/second) is used
based on their quantity. Only non-zero time units are included.
"""
years = delta.days // 365
Expand All @@ -123,6 +123,7 @@ def format_time(delta: timedelta) -> str:
hours = seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60

time_parts = [
('year', years),
("day", days),
Expand Down

0 comments on commit 082d235

Please sign in to comment.