add redundancy to config check

This commit is contained in:
rambros 2026-02-27 01:21:11 +05:30
parent 8a96017155
commit 1c04a90f82
6 changed files with 76 additions and 43 deletions

View file

@ -31,9 +31,14 @@ def select_platform(config):
console = Console()
fillers = ["YOUR_FLUXER_TOKEN", "FLUXER_BOT_TOKEN", "YOUR_STOAT_TOKEN", "STOAT_BOT_TOKEN", ""]
fluxer_set = config.fluxer_bot_token not in fillers
stoat_set = config.stoat_bot_token not in fillers
fillers = [ "DISCORD_BOT_TOKEN", "FLUXER_BOT_TOKEN", "STOAT_BOT_TOKEN",
"000000000000000000",
"DISCORD_SERVER_ID", "FLUXER_COMMUNITY_ID", "STOAT_SERVER_ID",
"", None
]
fluxer_set = config.fluxer_bot_token not in fillers and config.fluxer_community_id not in fillers
stoat_set = config.stoat_bot_token not in fillers and config.stoat_server_id not in fillers
if fluxer_set and not stoat_set:
return "fluxer"

View file

@ -23,6 +23,12 @@ class MigrationContext:
else:
server_id = config.fluxer_community_id
# Fallback for unconfigured platforms
if not server_id or server_id in [
"000000000000000000", "DISCORD_SERVER_ID", "FLUXER_COMMUNITY_ID", "STOAT_SERVER_ID"
]:
server_id = "unconfigured"
self.state = MigrationState(
state_file=f"{server_id}.state.json",
messages_file=f"{server_id}.messages.json"
@ -34,15 +40,15 @@ class MigrationContext:
)
self.fluxer_writer = FluxerWriter(
token=config.fluxer_bot_token,
community_id=config.fluxer_community_id,
api_url=config.fluxer_api_url
token=config.fluxer_bot_token or "",
community_id=config.fluxer_community_id or "",
api_url=config.fluxer_api_url or "default"
)
self.stoat_writer = StoatWriter(
token=config.stoat_bot_token,
community_id=config.stoat_server_id,
api_url=config.stoat_api_url
token=config.stoat_bot_token or "",
community_id=config.stoat_server_id or "",
api_url=config.stoat_api_url or "default"
)
self.writer = self.fluxer_writer if target_platform == "fluxer" else self.stoat_writer

View file

@ -1,3 +1,4 @@
from typing import Optional, Union
import yaml
from pathlib import Path
from pydantic import BaseModel, Field
@ -10,26 +11,26 @@ class MigrationSettings(BaseModel):
class AppConfig(BaseModel):
discord_bot_token: str
discord_server_id: str
fluxer_bot_token: str
fluxer_community_id: str
fluxer_api_url: str = Field(default="default")
stoat_bot_token: str
stoat_server_id: str
stoat_api_url: str = Field(default="default")
fluxer_bot_token: Optional[str] = Field(default=None)
fluxer_community_id: Optional[str] = Field(default=None)
fluxer_api_url: Optional[str] = Field(default=None)
stoat_bot_token: Optional[str] = Field(default=None)
stoat_server_id: Optional[str] = Field(default=None)
stoat_api_url: Optional[str] = Field(default=None)
migration: MigrationSettings = Field(default_factory=MigrationSettings)
def load_config(config_path: str | Path = "config.yaml") -> AppConfig:
def load_config(config_path: Union[str, Path] = "config.yaml") -> AppConfig:
path = Path(config_path)
if not path.exists():
# Create dummy config if missing
config = AppConfig(
discord_bot_token="YOUR_DISCORD_TOKEN",
discord_server_id="000000000000000000",
fluxer_bot_token="YOUR_FLUXER_TOKEN",
fluxer_community_id="000000000000000000",
discord_bot_token="DISCORD_BOT_TOKEN",
discord_server_id="DISCORD_SERVER_ID",
fluxer_bot_token="FLUXER_BOT_TOKEN",
fluxer_community_id="FLUXER_COMMUNITY_ID",
fluxer_api_url="default",
stoat_bot_token="YOUR_STOAT_TOKEN",
stoat_server_id="000000000000000000",
stoat_bot_token="STOAT_BOT_TOKEN",
stoat_server_id="STOAT_SERVER_ID",
stoat_api_url="default"
)
save_config(config, path)
@ -44,9 +45,9 @@ def load_config(config_path: str | Path = "config.yaml") -> AppConfig:
return AppConfig(**data)
def save_config(config: AppConfig, config_path: str | Path = "config.yaml"):
def save_config(config: AppConfig, config_path: Union[str, Path] = "config.yaml"):
path = Path(config_path)
# Dump model to dictionary
data = config.model_dump()
# Dump model to dictionary, excluding None values to keep the YAML clean
data = config.model_dump(exclude_none=True)
with open(path, "w", encoding="utf-8") as f:
yaml.safe_dump(data, f, default_flow_style=False, sort_keys=False)

View file

@ -4,7 +4,11 @@ from typing import AsyncGenerator, Dict, Any
class DiscordReader:
def __init__(self, token: str, server_id: str):
self.token = token
try:
self.server_id = int(server_id)
except (ValueError, TypeError):
# Fallback for placeholder strings like 'DISCORD_SERVER_ID'
self.server_id = 0
self.guild: discord.Guild | None = None
self.client: discord.Client | None = None

View file

@ -122,10 +122,16 @@ class FluxerWriter:
# Calculate total permissions
# In Discord/Fluxer, permissions are additive
total_perms = 0
fluxer_guild_id = 0
try:
fluxer_guild_id = int(self.community_id)
except (ValueError, TypeError):
pass
for r_data in all_roles_data:
r_id = int(r_data["id"])
# Add @everyone permissions (role ID same as guild ID)
if r_id == int(self.community_id) or r_id in member_role_ids:
if r_id == fluxer_guild_id or r_id in member_role_ids:
total_perms |= int(r_data.get("permissions", 0))
# Debugging
@ -509,10 +515,17 @@ class FluxerWriter:
async def set_channel_permission(self, channel_id: str, overwrite_id: str, allow: int, deny: int, is_role: bool = True):
"""Sets a permission overwrite for a channel or category."""
assert self.client is not None
try:
target_channel_id = int(channel_id)
target_overwrite_id = int(overwrite_id)
except (ValueError, TypeError):
logger.warning(f"Fluxer: Skipping permission set for non-numeric ID: channel={channel_id}, overwrite={overwrite_id}")
return
try:
await self.client.edit_channel_permissions(
channel_id=int(channel_id),
overwrite_id=int(overwrite_id),
channel_id=target_channel_id,
overwrite_id=target_overwrite_id,
allow=allow,
deny=deny,
type=0 if is_role else 1

View file

@ -112,9 +112,13 @@ class MigrationCLI:
f_token = self.config.fluxer_bot_token
s_token = self.config.stoat_bot_token
discord_dummy = d_token in ["YOUR_DISCORD_TOKEN", "DISCORD_BOT_TOKEN", ""]
fluxer_dummy = f_token in ["YOUR_FLUXER_TOKEN", "FLUXER_BOT_TOKEN", "YOUR_STOAT_TOKEN", "STOAT_BOT_TOKEN", ""]
stoat_dummy = s_token in ["YOUR_STOAT_TOKEN", "STOAT_BOT_TOKEN", "YOUR_FLUXER_TOKEN", "FLUXER_BOT_TOKEN", ""]
fillers = ["DISCORD_BOT_TOKEN", "FLUXER_BOT_TOKEN", "STOAT_BOT_TOKEN",
"000000000000000000", "DISCORD_SERVER_ID", "FLUXER_COMMUNITY_ID", "STOAT_SERVER_ID",
"", None
]
discord_dummy = d_token in fillers or self.config.discord_server_id in fillers
fluxer_dummy = f_token in fillers or self.config.fluxer_community_id in fillers
stoat_dummy = s_token in fillers or self.config.stoat_server_id in fillers
discord_task = None
if not discord_dummy:
@ -144,7 +148,7 @@ class MigrationCLI:
# Process Discord Result
if discord_dummy:
console.print("[bold yellow]Discord setup incomplete (Using default token).[/bold yellow]")
console.print("[bold yellow]Discord setup incomplete.[/bold yellow]")
elif discord_task in done:
try:
res = discord_task.result()
@ -171,7 +175,7 @@ class MigrationCLI:
# Process Fluxer Result
if self.target_platform == "fluxer":
if fluxer_dummy:
console.print("[dim yellow]Fluxer platform setup incomplete (Using default token).[/dim yellow]")
console.print("[dim yellow]Fluxer platform setup incomplete.[/dim yellow]")
elif fluxer_task in done:
try:
res = fluxer_task.result()
@ -195,7 +199,7 @@ class MigrationCLI:
# Process Stoat Result
if self.target_platform == "stoat":
if stoat_dummy:
console.print("[dim yellow]Stoat platform setup incomplete (Using default token).[/dim yellow]")
console.print("[dim yellow]Stoat platform setup incomplete.[/dim yellow]")
elif stoat_task in done:
try:
res = stoat_task.result()
@ -253,7 +257,7 @@ class MigrationCLI:
if t is not None and not t.done(): t.cancel()
async def run(self):
if self.config.discord_bot_token == "YOUR_DISCORD_TOKEN":
if self.config.discord_bot_token in ["YOUR_DISCORD_TOKEN", "DISCORD_BOT_TOKEN"]:
console.print("\n[bold yellow]First time setup detected. Redirecting to configuration...[/bold yellow]")
self.validation_results = {
"discord_token": False, "discord_bot_name": None,
@ -486,7 +490,6 @@ class MigrationCLI:
f_token = self.config.fluxer_bot_token
f_comm = self.config.fluxer_community_id
# Only rewrite if changed
if (d_token != self.config.discord_bot_token or
f_token != self.config.fluxer_bot_token or
d_server != self.config.discord_server_id or
@ -494,12 +497,13 @@ class MigrationCLI:
s_token != self.config.stoat_bot_token or
s_comm != self.config.stoat_server_id):
self.config.discord_bot_token = d_token
self.config.fluxer_bot_token = f_token
self.config.discord_server_id = d_server
self.config.fluxer_community_id = f_comm
self.config.stoat_bot_token = s_token
self.config.stoat_server_id = s_comm
# Treat empty string as None to unconfigure
self.config.discord_bot_token = d_token if d_token != "" else None
self.config.discord_server_id = d_server if d_server != "" else None
self.config.fluxer_bot_token = f_token if f_token != "" else None
self.config.fluxer_community_id = f_comm if f_comm != "" else None
self.config.stoat_bot_token = s_token if s_token != "" else None
self.config.stoat_server_id = s_comm if s_comm != "" else None
save_config(self.config, self.config_path)
# Recreate engine with new config