add redundancy to config check
This commit is contained in:
parent
8a96017155
commit
1c04a90f82
6 changed files with 76 additions and 43 deletions
|
|
@ -31,9 +31,14 @@ def select_platform(config):
|
||||||
|
|
||||||
console = Console()
|
console = Console()
|
||||||
|
|
||||||
fillers = ["YOUR_FLUXER_TOKEN", "FLUXER_BOT_TOKEN", "YOUR_STOAT_TOKEN", "STOAT_BOT_TOKEN", ""]
|
fillers = [ "DISCORD_BOT_TOKEN", "FLUXER_BOT_TOKEN", "STOAT_BOT_TOKEN",
|
||||||
fluxer_set = config.fluxer_bot_token not in fillers
|
"000000000000000000",
|
||||||
stoat_set = config.stoat_bot_token not in fillers
|
"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:
|
if fluxer_set and not stoat_set:
|
||||||
return "fluxer"
|
return "fluxer"
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,12 @@ class MigrationContext:
|
||||||
else:
|
else:
|
||||||
server_id = config.fluxer_community_id
|
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(
|
self.state = MigrationState(
|
||||||
state_file=f"{server_id}.state.json",
|
state_file=f"{server_id}.state.json",
|
||||||
messages_file=f"{server_id}.messages.json"
|
messages_file=f"{server_id}.messages.json"
|
||||||
|
|
@ -34,15 +40,15 @@ class MigrationContext:
|
||||||
)
|
)
|
||||||
|
|
||||||
self.fluxer_writer = FluxerWriter(
|
self.fluxer_writer = FluxerWriter(
|
||||||
token=config.fluxer_bot_token,
|
token=config.fluxer_bot_token or "",
|
||||||
community_id=config.fluxer_community_id,
|
community_id=config.fluxer_community_id or "",
|
||||||
api_url=config.fluxer_api_url
|
api_url=config.fluxer_api_url or "default"
|
||||||
)
|
)
|
||||||
|
|
||||||
self.stoat_writer = StoatWriter(
|
self.stoat_writer = StoatWriter(
|
||||||
token=config.stoat_bot_token,
|
token=config.stoat_bot_token or "",
|
||||||
community_id=config.stoat_server_id,
|
community_id=config.stoat_server_id or "",
|
||||||
api_url=config.stoat_api_url
|
api_url=config.stoat_api_url or "default"
|
||||||
)
|
)
|
||||||
|
|
||||||
self.writer = self.fluxer_writer if target_platform == "fluxer" else self.stoat_writer
|
self.writer = self.fluxer_writer if target_platform == "fluxer" else self.stoat_writer
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
from typing import Optional, Union
|
||||||
import yaml
|
import yaml
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
|
|
@ -10,26 +11,26 @@ class MigrationSettings(BaseModel):
|
||||||
class AppConfig(BaseModel):
|
class AppConfig(BaseModel):
|
||||||
discord_bot_token: str
|
discord_bot_token: str
|
||||||
discord_server_id: str
|
discord_server_id: str
|
||||||
fluxer_bot_token: str
|
fluxer_bot_token: Optional[str] = Field(default=None)
|
||||||
fluxer_community_id: str
|
fluxer_community_id: Optional[str] = Field(default=None)
|
||||||
fluxer_api_url: str = Field(default="default")
|
fluxer_api_url: Optional[str] = Field(default=None)
|
||||||
stoat_bot_token: str
|
stoat_bot_token: Optional[str] = Field(default=None)
|
||||||
stoat_server_id: str
|
stoat_server_id: Optional[str] = Field(default=None)
|
||||||
stoat_api_url: str = Field(default="default")
|
stoat_api_url: Optional[str] = Field(default=None)
|
||||||
migration: MigrationSettings = Field(default_factory=MigrationSettings)
|
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)
|
path = Path(config_path)
|
||||||
if not path.exists():
|
if not path.exists():
|
||||||
# Create dummy config if missing
|
# Create dummy config if missing
|
||||||
config = AppConfig(
|
config = AppConfig(
|
||||||
discord_bot_token="YOUR_DISCORD_TOKEN",
|
discord_bot_token="DISCORD_BOT_TOKEN",
|
||||||
discord_server_id="000000000000000000",
|
discord_server_id="DISCORD_SERVER_ID",
|
||||||
fluxer_bot_token="YOUR_FLUXER_TOKEN",
|
fluxer_bot_token="FLUXER_BOT_TOKEN",
|
||||||
fluxer_community_id="000000000000000000",
|
fluxer_community_id="FLUXER_COMMUNITY_ID",
|
||||||
fluxer_api_url="default",
|
fluxer_api_url="default",
|
||||||
stoat_bot_token="YOUR_STOAT_TOKEN",
|
stoat_bot_token="STOAT_BOT_TOKEN",
|
||||||
stoat_server_id="000000000000000000",
|
stoat_server_id="STOAT_SERVER_ID",
|
||||||
stoat_api_url="default"
|
stoat_api_url="default"
|
||||||
)
|
)
|
||||||
save_config(config, path)
|
save_config(config, path)
|
||||||
|
|
@ -44,9 +45,9 @@ def load_config(config_path: str | Path = "config.yaml") -> AppConfig:
|
||||||
|
|
||||||
return AppConfig(**data)
|
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)
|
path = Path(config_path)
|
||||||
# Dump model to dictionary
|
# Dump model to dictionary, excluding None values to keep the YAML clean
|
||||||
data = config.model_dump()
|
data = config.model_dump(exclude_none=True)
|
||||||
with open(path, "w", encoding="utf-8") as f:
|
with open(path, "w", encoding="utf-8") as f:
|
||||||
yaml.safe_dump(data, f, default_flow_style=False, sort_keys=False)
|
yaml.safe_dump(data, f, default_flow_style=False, sort_keys=False)
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,11 @@ from typing import AsyncGenerator, Dict, Any
|
||||||
class DiscordReader:
|
class DiscordReader:
|
||||||
def __init__(self, token: str, server_id: str):
|
def __init__(self, token: str, server_id: str):
|
||||||
self.token = token
|
self.token = token
|
||||||
self.server_id = int(server_id)
|
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.guild: discord.Guild | None = None
|
||||||
self.client: discord.Client | None = None
|
self.client: discord.Client | None = None
|
||||||
|
|
|
||||||
|
|
@ -122,10 +122,16 @@ class FluxerWriter:
|
||||||
# Calculate total permissions
|
# Calculate total permissions
|
||||||
# In Discord/Fluxer, permissions are additive
|
# In Discord/Fluxer, permissions are additive
|
||||||
total_perms = 0
|
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:
|
for r_data in all_roles_data:
|
||||||
r_id = int(r_data["id"])
|
r_id = int(r_data["id"])
|
||||||
# Add @everyone permissions (role ID same as guild 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))
|
total_perms |= int(r_data.get("permissions", 0))
|
||||||
|
|
||||||
# Debugging
|
# 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):
|
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."""
|
"""Sets a permission overwrite for a channel or category."""
|
||||||
assert self.client is not None
|
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:
|
try:
|
||||||
await self.client.edit_channel_permissions(
|
await self.client.edit_channel_permissions(
|
||||||
channel_id=int(channel_id),
|
channel_id=target_channel_id,
|
||||||
overwrite_id=int(overwrite_id),
|
overwrite_id=target_overwrite_id,
|
||||||
allow=allow,
|
allow=allow,
|
||||||
deny=deny,
|
deny=deny,
|
||||||
type=0 if is_role else 1
|
type=0 if is_role else 1
|
||||||
|
|
|
||||||
|
|
@ -112,9 +112,13 @@ class MigrationCLI:
|
||||||
f_token = self.config.fluxer_bot_token
|
f_token = self.config.fluxer_bot_token
|
||||||
s_token = self.config.stoat_bot_token
|
s_token = self.config.stoat_bot_token
|
||||||
|
|
||||||
discord_dummy = d_token in ["YOUR_DISCORD_TOKEN", "DISCORD_BOT_TOKEN", ""]
|
fillers = ["DISCORD_BOT_TOKEN", "FLUXER_BOT_TOKEN", "STOAT_BOT_TOKEN",
|
||||||
fluxer_dummy = f_token in ["YOUR_FLUXER_TOKEN", "FLUXER_BOT_TOKEN", "YOUR_STOAT_TOKEN", "STOAT_BOT_TOKEN", ""]
|
"000000000000000000", "DISCORD_SERVER_ID", "FLUXER_COMMUNITY_ID", "STOAT_SERVER_ID",
|
||||||
stoat_dummy = s_token in ["YOUR_STOAT_TOKEN", "STOAT_BOT_TOKEN", "YOUR_FLUXER_TOKEN", "FLUXER_BOT_TOKEN", ""]
|
"", 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
|
discord_task = None
|
||||||
if not discord_dummy:
|
if not discord_dummy:
|
||||||
|
|
@ -144,7 +148,7 @@ class MigrationCLI:
|
||||||
|
|
||||||
# Process Discord Result
|
# Process Discord Result
|
||||||
if discord_dummy:
|
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:
|
elif discord_task in done:
|
||||||
try:
|
try:
|
||||||
res = discord_task.result()
|
res = discord_task.result()
|
||||||
|
|
@ -171,7 +175,7 @@ class MigrationCLI:
|
||||||
# Process Fluxer Result
|
# Process Fluxer Result
|
||||||
if self.target_platform == "fluxer":
|
if self.target_platform == "fluxer":
|
||||||
if fluxer_dummy:
|
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:
|
elif fluxer_task in done:
|
||||||
try:
|
try:
|
||||||
res = fluxer_task.result()
|
res = fluxer_task.result()
|
||||||
|
|
@ -195,7 +199,7 @@ class MigrationCLI:
|
||||||
# Process Stoat Result
|
# Process Stoat Result
|
||||||
if self.target_platform == "stoat":
|
if self.target_platform == "stoat":
|
||||||
if stoat_dummy:
|
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:
|
elif stoat_task in done:
|
||||||
try:
|
try:
|
||||||
res = stoat_task.result()
|
res = stoat_task.result()
|
||||||
|
|
@ -253,7 +257,7 @@ class MigrationCLI:
|
||||||
if t is not None and not t.done(): t.cancel()
|
if t is not None and not t.done(): t.cancel()
|
||||||
|
|
||||||
async def run(self):
|
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]")
|
console.print("\n[bold yellow]First time setup detected. Redirecting to configuration...[/bold yellow]")
|
||||||
self.validation_results = {
|
self.validation_results = {
|
||||||
"discord_token": False, "discord_bot_name": None,
|
"discord_token": False, "discord_bot_name": None,
|
||||||
|
|
@ -486,7 +490,6 @@ class MigrationCLI:
|
||||||
f_token = self.config.fluxer_bot_token
|
f_token = self.config.fluxer_bot_token
|
||||||
f_comm = self.config.fluxer_community_id
|
f_comm = self.config.fluxer_community_id
|
||||||
|
|
||||||
# Only rewrite if changed
|
|
||||||
if (d_token != self.config.discord_bot_token or
|
if (d_token != self.config.discord_bot_token or
|
||||||
f_token != self.config.fluxer_bot_token or
|
f_token != self.config.fluxer_bot_token or
|
||||||
d_server != self.config.discord_server_id or
|
d_server != self.config.discord_server_id or
|
||||||
|
|
@ -494,12 +497,13 @@ class MigrationCLI:
|
||||||
s_token != self.config.stoat_bot_token or
|
s_token != self.config.stoat_bot_token or
|
||||||
s_comm != self.config.stoat_server_id):
|
s_comm != self.config.stoat_server_id):
|
||||||
|
|
||||||
self.config.discord_bot_token = d_token
|
# Treat empty string as None to unconfigure
|
||||||
self.config.fluxer_bot_token = f_token
|
self.config.discord_bot_token = d_token if d_token != "" else None
|
||||||
self.config.discord_server_id = d_server
|
self.config.discord_server_id = d_server if d_server != "" else None
|
||||||
self.config.fluxer_community_id = f_comm
|
self.config.fluxer_bot_token = f_token if f_token != "" else None
|
||||||
self.config.stoat_bot_token = s_token
|
self.config.fluxer_community_id = f_comm if f_comm != "" else None
|
||||||
self.config.stoat_server_id = s_comm
|
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)
|
save_config(self.config, self.config_path)
|
||||||
# Recreate engine with new config
|
# Recreate engine with new config
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue