83 lines
3.1 KiB
Python
83 lines
3.1 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
import discord
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
|
|
from zyrarbot.music.music_player import MusicPlayer
|
|
from zyrarbot.settings import Settings
|
|
from zyrarbot.storage.guild_config_repository import GuildConfigRepository
|
|
from zyrarbot.ui.api.dependencies import (
|
|
get_broadcaster,
|
|
get_config,
|
|
get_guilds,
|
|
get_player,
|
|
get_settings,
|
|
)
|
|
from zyrarbot.ui.api.requests.default_plays_request import DefaultPlaysRequest
|
|
from zyrarbot.ui.api.requests.text_channel_request import TextChannelRequest
|
|
from zyrarbot.ui.api.status_broadcaster import StatusBroadcaster
|
|
from zyrarbot.ui.bot.guild_resolver import GuildResolver
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
router = APIRouter(prefix="/api/settings", tags=["settings"])
|
|
|
|
|
|
@router.post("/text_channel")
|
|
async def set_text_channel(
|
|
request: TextChannelRequest,
|
|
guilds: GuildResolver = Depends(get_guilds),
|
|
player: MusicPlayer = Depends(get_player),
|
|
config: GuildConfigRepository = Depends(get_config),
|
|
broadcaster: StatusBroadcaster = Depends(get_broadcaster),
|
|
) -> dict[str, Any]:
|
|
if request.channel_id in ("", "0"):
|
|
if not request.guild_id:
|
|
raise HTTPException(status_code=400, detail="guild_id required to reset channel")
|
|
guild_id = int(request.guild_id)
|
|
player.set_notification_channel(guild_id, None)
|
|
config.set_notification_channel_id(guild_id, None)
|
|
await broadcaster.broadcast()
|
|
return {"status": "success", "channel_id": None, "guild_id": str(guild_id)}
|
|
|
|
channel = guilds.channel(int(request.channel_id))
|
|
if not isinstance(channel, discord.TextChannel):
|
|
raise HTTPException(status_code=404, detail="Text channel not found")
|
|
|
|
guild_id = channel.guild.id
|
|
if request.guild_id and int(request.guild_id) != guild_id:
|
|
raise HTTPException(
|
|
status_code=400, detail="Channel does not belong to the specified guild"
|
|
)
|
|
|
|
player.set_notification_channel(guild_id, channel)
|
|
config.set_notification_channel_id(guild_id, channel.id)
|
|
|
|
try:
|
|
await channel.send(
|
|
"🔔 Ten kanał został ustawiony jako główny kanał powiadomień dla ŻyrarBOT "
|
|
f"na serwerze **{channel.guild.name}** z panelu webowego."
|
|
)
|
|
except discord.DiscordException as error:
|
|
logger.error("Nie udało się wysłać wiadomości testowej: %s", error)
|
|
|
|
await broadcaster.broadcast()
|
|
return {"status": "success", "channel_id": request.channel_id, "guild_id": str(guild_id)}
|
|
|
|
|
|
@router.post("/default_plays")
|
|
async def set_default_plays(
|
|
request: DefaultPlaysRequest,
|
|
player: MusicPlayer = Depends(get_player),
|
|
settings: Settings = Depends(get_settings),
|
|
broadcaster: StatusBroadcaster = Depends(get_broadcaster),
|
|
) -> dict[str, Any]:
|
|
if not settings.is_valid_plays(request.default_plays):
|
|
raise HTTPException(status_code=400, detail="Invalid default plays value")
|
|
player.set_default_plays(request.default_plays)
|
|
await broadcaster.broadcast()
|
|
return {"status": "success", "default_plays": request.default_plays}
|