feat: add notification channel selector
This commit is contained in:
5 files changed
+109
-1
No files matched your search
@@ -0,0 +1,7 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class NotificationChannelRequest(BaseModel):
|
||||
"""Kanał tekstowy używany do powiadomień o odtwarzaniu."""
|
||||
|
||||
channel_id: str
|
||||
@@ -5,19 +5,48 @@ from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from zyrarbot.music.music_player import MusicPlayer
|
||||
from zyrarbot.music.notification_channel_resolver import NotificationChannelResolver
|
||||
from zyrarbot.storage.guild_config_repository import GuildConfigRepository
|
||||
from zyrarbot.ui.api.dependencies import (
|
||||
get_active_guild,
|
||||
get_broadcaster,
|
||||
get_config,
|
||||
get_guilds,
|
||||
get_player,
|
||||
)
|
||||
from zyrarbot.ui.api.requests.connect_request import ConnectRequest
|
||||
from zyrarbot.ui.api.requests.notification_channel_request import NotificationChannelRequest
|
||||
from zyrarbot.ui.api.status_broadcaster import StatusBroadcaster
|
||||
from zyrarbot.ui.bot.guild_resolver import GuildResolver
|
||||
|
||||
router = APIRouter(prefix="/api", tags=["voice"])
|
||||
|
||||
|
||||
@router.post("/notification_channel")
|
||||
async def set_notification_channel(
|
||||
request: NotificationChannelRequest,
|
||||
guild: discord.Guild = Depends(get_active_guild),
|
||||
guilds: GuildResolver = Depends(get_guilds),
|
||||
player: MusicPlayer = Depends(get_player),
|
||||
config: GuildConfigRepository = Depends(get_config),
|
||||
broadcaster: StatusBroadcaster = Depends(get_broadcaster),
|
||||
) -> dict[str, str]:
|
||||
try:
|
||||
channel_id = int(request.channel_id)
|
||||
except ValueError as error:
|
||||
raise HTTPException(status_code=400, detail="Invalid text channel ID") from error
|
||||
|
||||
channel = guilds.channel(channel_id)
|
||||
if not isinstance(channel, discord.TextChannel) or channel.guild.id != guild.id:
|
||||
raise HTTPException(status_code=404, detail="Text channel not found on this server")
|
||||
if channel not in NotificationChannelResolver.writable_channels(guild):
|
||||
raise HTTPException(status_code=403, detail="Bot cannot send messages to this channel")
|
||||
|
||||
player.set_notification_channel(guild.id, channel)
|
||||
config.set_notification_channel_id(guild.id, channel.id)
|
||||
await broadcaster.broadcast()
|
||||
return {"status": "success", "channel_id": str(channel.id)}
|
||||
|
||||
|
||||
@router.post("/connect")
|
||||
async def connect(
|
||||
request: ConnectRequest,
|
||||
|
||||
Reference in new issue
Block a user