2024-12-16 21:49:58 +02:00
import logging
from typing import Any , Dict , Union
2024-12-16 21:34:37 +02:00
2024-12-16 21:49:58 +02:00
from discord import ApplicationContext , Embed , option , TextChannel , Role
2023-05-06 16:21:15 +03:00
from discord import utils as ds_utils
2023-05-06 19:56:01 +03:00
from discord . abc import GuildChannel
2023-05-04 17:09:47 +03:00
from discord . commands import SlashCommandGroup
2023-05-06 16:21:15 +03:00
from discord . ext import commands
2024-12-16 00:36:48 +02:00
from libbot import config_get
from libbot . sync import config_get as sync_config_get
2023-05-06 16:21:15 +03:00
2024-12-17 23:14:06 +02:00
from classes . holo_bot import HoloBot
2023-05-04 17:09:47 +03:00
from classes . holo_user import HoloUser
2024-12-16 21:34:37 +02:00
from enums import Color
2023-05-06 19:56:01 +03:00
from modules . database import col_users
2024-12-16 00:36:48 +02:00
from modules . utils_sync import guild_name
2023-05-04 17:09:47 +03:00
2024-12-16 21:49:58 +02:00
logger = logging . getLogger ( __name__ )
2023-05-04 17:09:47 +03:00
2023-05-04 17:14:23 +03:00
class CustomChannels ( commands . Cog ) :
2024-12-17 23:14:06 +02:00
def __init__ ( self , client : HoloBot ) :
self . client : HoloBot = client
2023-05-04 17:14:23 +03:00
2023-05-06 19:31:07 +03:00
@commands.Cog.listener ( )
2024-12-16 21:34:37 +02:00
async def on_guild_channel_delete ( self , channel : GuildChannel ) - > None :
2024-12-16 17:25:35 +02:00
await col_users . find_one_and_update (
2023-05-06 19:56:01 +03:00
{ " customchannel " : channel . id } , { " $set " : { " customchannel " : None } }
)
2023-05-06 19:31:07 +03:00
2024-12-16 21:34:37 +02:00
custom_channel_group : SlashCommandGroup = SlashCommandGroup (
" customchannel " , " Керування особистим каналом "
)
2023-05-04 17:09:47 +03:00
2024-12-16 21:34:37 +02:00
@custom_channel_group.command (
2023-05-06 18:09:06 +03:00
name = " get " ,
2023-05-04 17:14:23 +03:00
description = " Отримати персональний текстовий канал " ,
2024-12-16 00:36:48 +02:00
guild_ids = [ sync_config_get ( " guild " ) ] ,
2023-05-04 17:14:23 +03:00
)
2023-05-04 17:09:47 +03:00
@option ( " name " , description = " Назва каналу " )
@option ( " reactions " , description = " Дозволити реакції " )
@option ( " threads " , description = " Дозволити гілки " )
2024-12-16 21:49:58 +02:00
async def custom_channel_get_cmd (
2023-05-04 17:14:23 +03:00
self , ctx : ApplicationContext , name : str , reactions : bool , threads : bool
2024-12-16 21:34:37 +02:00
) - > None :
holo_user_ctx : HoloUser = HoloUser ( ctx . user )
2023-05-04 17:09:47 +03:00
2024-12-16 21:49:58 +02:00
# Return if the user is using the command outside of a guild
if not hasattr ( ctx . author , " guild " ) :
await ctx . defer ( ephemeral = True )
await ctx . respond (
embed = Embed (
title = " Помилка виконання " ,
description = " Виконання за межами сервера не є можливим. " ,
color = Color . FAIL ,
)
)
return
2024-12-16 00:21:41 +02:00
# Return if the user already has a custom channel
if holo_user_ctx . customchannel is not None :
2023-05-04 17:09:47 +03:00
await ctx . defer ( ephemeral = True )
2023-05-04 17:14:23 +03:00
await ctx . respond (
embed = Embed (
title = " Помилка виконання " ,
2024-06-23 13:05:03 +03:00
description = " У вас вже є особистий канал.\n Для редагування каналу є `/customchannel edit` а б о просто відкрийте меню керування вашим каналом. " ,
2024-12-16 21:34:37 +02:00
color = Color . FAIL ,
2023-05-04 17:14:23 +03:00
)
)
2024-12-16 00:21:41 +02:00
return
await ctx . defer ( )
2024-12-16 21:34:37 +02:00
created_channel : TextChannel = await ctx . user . guild . create_text_channel (
2024-12-16 00:21:41 +02:00
name = name ,
reason = f " Користувач { guild_name ( ctx . user ) } отримав власний приватний канал " ,
category = ds_utils . get (
ctx . author . guild . categories ,
id = await config_get ( " customchannels " , " categories " ) ,
) ,
)
await created_channel . set_permissions (
ctx . user . guild . default_role ,
send_messages = False ,
add_reactions = reactions ,
create_public_threads = threads ,
create_private_threads = threads ,
)
await created_channel . set_permissions (
ctx . user ,
attach_files = True ,
manage_messages = True ,
send_messages = True ,
embed_links = True ,
manage_channels = True ,
)
2024-12-16 17:25:35 +02:00
await holo_user_ctx . set ( " customchannel " , created_channel . id )
2024-12-16 00:21:41 +02:00
await ctx . respond (
embed = Embed (
title = " Створено канал " ,
description = f " Вітаємо! Ви створили канал { created_channel . mention } . Для керування ним користуйтесь меню налаштувань каналу а також командою `/customchannel edit` " ,
2024-12-16 21:34:37 +02:00
color = Color . SUCCESS ,
2024-12-16 00:21:41 +02:00
)
)
2024-12-16 21:49:58 +02:00
bots : Dict [ str , Any ] = await config_get ( " bots " )
2024-12-16 00:21:41 +02:00
for bot in bots :
2024-12-16 21:49:58 +02:00
role : Union [ Role , None ] = ds_utils . get (
ctx . user . guild . roles , id = bots [ bot ] [ " role " ]
2024-12-16 00:21:41 +02:00
)
2023-05-04 17:09:47 +03:00
2024-12-16 21:49:58 +02:00
if role is not None :
await created_channel . set_permissions (
role ,
view_channel = False ,
)
2024-12-16 21:34:37 +02:00
@custom_channel_group.command (
2023-05-04 17:14:23 +03:00
name = " edit " ,
description = " Змінити параметри особистого каналу " ,
2024-12-16 00:36:48 +02:00
guild_ids = [ sync_config_get ( " guild " ) ] ,
2023-05-04 17:14:23 +03:00
)
2023-05-04 17:09:47 +03:00
@option ( " name " , description = " Назва каналу " )
@option ( " reactions " , description = " Дозволити реакції " )
@option ( " threads " , description = " Дозволити гілки " )
2024-12-16 21:49:58 +02:00
async def custom_channel_edit_cmd (
2023-05-04 17:14:23 +03:00
self , ctx : ApplicationContext , name : str , reactions : bool , threads : bool
2024-12-16 21:34:37 +02:00
) - > None :
holo_user_ctx : HoloUser = HoloUser ( ctx . user )
2023-05-04 17:09:47 +03:00
2024-12-16 21:34:37 +02:00
custom_channel : Union [ TextChannel , None ] = ds_utils . get (
2023-05-06 19:56:01 +03:00
ctx . guild . channels , id = holo_user_ctx . customchannel
)
2024-12-16 00:21:41 +02:00
# Return if the channel was not found
2023-05-04 17:09:47 +03:00
if custom_channel is None :
2023-05-04 17:14:23 +03:00
await ctx . respond (
embed = Embed (
title = " Канал не знайдено " ,
2024-06-23 13:05:03 +03:00
description = " Канал, вказаний як ваш, не існує. Можливо, його було вручну видалено раніше. " ,
2024-12-16 21:34:37 +02:00
color = Color . FAIL ,
2023-05-04 17:14:23 +03:00
)
)
2023-05-04 17:09:47 +03:00
return
2024-12-16 00:21:41 +02:00
2023-05-04 17:09:47 +03:00
await custom_channel . edit ( name = name )
2023-05-04 17:14:23 +03:00
await custom_channel . set_permissions (
ctx . user . guild . default_role ,
send_messages = False ,
add_reactions = reactions ,
create_public_threads = threads ,
create_private_threads = threads ,
)
2024-12-16 00:21:41 +02:00
2023-05-04 17:14:23 +03:00
await ctx . respond (
embed = Embed (
title = " Канал змінено " ,
description = f " Назва каналу тепер ` { name } `, реакції ` { reactions } ` та дозволено треди ` { threads } ` " ,
2024-12-16 21:34:37 +02:00
color = Color . FAIL ,
2023-05-04 17:14:23 +03:00
)
)
2023-05-04 17:09:47 +03:00
2024-12-16 21:34:37 +02:00
@custom_channel_group.command (
2023-05-06 18:09:06 +03:00
name = " remove " ,
2023-05-04 17:14:23 +03:00
description = " Відібрати канал, знищуючи його, та частково повернути кошти " ,
2024-12-16 00:36:48 +02:00
guild_ids = [ sync_config_get ( " guild " ) ] ,
2023-05-04 17:14:23 +03:00
)
2023-05-06 18:09:06 +03:00
@option ( " confirm " , description = " Підтвердження операції " )
2024-12-16 21:49:58 +02:00
async def custom_channel_remove_cmd (
2023-05-06 19:56:01 +03:00
self , ctx : ApplicationContext , confirm : bool = False
2024-12-16 21:34:37 +02:00
) - > None :
holo_user_ctx : HoloUser = HoloUser ( ctx . user )
2023-05-04 17:09:47 +03:00
2024-12-16 00:21:41 +02:00
# Return if the user does not have a custom channel
if holo_user_ctx . customchannel is None :
await ctx . defer ( ephemeral = True )
await ctx . respond (
embed = Embed (
title = " Помилка виконання " ,
description = " У вас немає особистого каналу." ,
2024-12-16 21:34:37 +02:00
color = Color . FAIL ,
2023-05-06 18:09:06 +03:00
)
2024-12-16 00:21:41 +02:00
)
return
await ctx . defer ( )
2024-12-16 21:34:37 +02:00
custom_channel : Union [ TextChannel , None ] = ds_utils . get (
2024-12-16 00:21:41 +02:00
ctx . guild . channels , id = holo_user_ctx . customchannel
)
# Return if the channel was not found
if custom_channel is None :
2023-05-04 17:14:23 +03:00
await ctx . respond (
embed = Embed (
2024-12-16 00:21:41 +02:00
title = " Канал не знайдено " ,
description = " Канал, вказаний як ваш, не існує. Можливо, його було вручну видалено раніше. " ,
2024-12-16 21:34:37 +02:00
color = Color . FAIL ,
2023-05-04 17:14:23 +03:00
)
)
2024-12-16 17:25:35 +02:00
await holo_user_ctx . set ( " customchannel " , None )
2024-12-16 00:21:41 +02:00
return
# Return if the confirmation is missing
if not confirm :
2023-05-04 17:14:23 +03:00
await ctx . respond (
embed = Embed (
2024-12-16 00:21:41 +02:00
title = " Підтвердження не надано " ,
description = " Для підтвердження операції додайте до команди параметр `confirm` зі значенням `True`. " ,
2024-12-16 21:34:37 +02:00
color = Color . FAIL ,
2023-05-04 17:14:23 +03:00
)
)
2024-12-16 00:21:41 +02:00
return
await custom_channel . delete ( reason = " Власник запросив видалення " )
2024-12-16 17:25:35 +02:00
await holo_user_ctx . set ( " customchannel " , None )
2024-12-16 00:21:41 +02:00
2024-12-16 21:49:58 +02:00
try :
2024-12-16 20:54:53 +02:00
await ctx . respond (
embed = Embed (
title = " Канал знищено " ,
description = " Ви відмовились від каналу та видалили його. " ,
2024-12-16 21:34:37 +02:00
color = Color . DEFAULT ,
2024-12-16 20:54:53 +02:00
)
2024-12-16 00:21:41 +02:00
)
2024-12-16 21:49:58 +02:00
except Exception as exc :
logger . warning (
" Could not send a custom channel removal confirmation due to: %s " , exc
)
2024-06-23 13:05:03 +03:00
2024-12-17 23:14:06 +02:00
def setup ( client : HoloBot ) - > None :
2024-06-23 13:05:03 +03:00
client . add_cog ( CustomChannels ( client ) )