2023-05-06 16:21:15 +03:00
from discord import ApplicationContext , Embed , option
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
2024-06-23 13:05:03 +03:00
from libbot . pycord . classes import PycordBot
2024-12-16 00:36:48 +02:00
from libbot . sync import config_get as sync_config_get
2023-05-06 16:21:15 +03:00
2023-05-04 17:09:47 +03:00
from classes . holo_user import HoloUser
from enums . colors 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
2023-05-04 17:14:23 +03:00
class CustomChannels ( commands . Cog ) :
2024-06-23 13:05:03 +03:00
def __init__ ( self , client : PycordBot ) :
2023-05-04 17:09:47 +03:00
self . client = client
2023-05-04 17:14:23 +03:00
2023-05-06 19:31:07 +03:00
@commands.Cog.listener ( )
async def on_guild_channel_delete ( self , channel : GuildChannel ) :
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
2023-05-04 17:13:29 +03:00
customchannel = SlashCommandGroup ( " customchannel " , " Керування особистим каналом " )
2023-05-04 17:09:47 +03:00
2023-05-04 17:14:23 +03:00
@customchannel.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 = " Дозволити гілки " )
2023-05-06 18:09:06 +03:00
async def customchannel_get_cmd (
2023-05-04 17:14:23 +03:00
self , ctx : ApplicationContext , name : str , reactions : bool , threads : bool
) :
2023-05-04 17:09:47 +03:00
holo_user_ctx = HoloUser ( ctx . user )
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` а б о просто відкрийте меню керування вашим каналом. " ,
2023-05-04 17:14:23 +03:00
color = Color . fail ,
)
)
2024-12-16 00:21:41 +02:00
return
await ctx . defer ( )
created_channel = await ctx . user . guild . create_text_channel (
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` " ,
color = Color . success ,
)
)
bots = await config_get ( " bots " )
for bot in bots :
await created_channel . set_permissions (
ds_utils . get ( ctx . user . guild . roles , id = bots [ bot ] [ " role " ] ) ,
view_channel = False ,
)
2023-05-04 17:09:47 +03:00
2023-05-04 17:14:23 +03:00
@customchannel.command (
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 = " Дозволити гілки " )
2023-05-04 17:14:23 +03:00
async def customchannel_edit_cmd (
self , ctx : ApplicationContext , name : str , reactions : bool , threads : bool
) :
2023-05-04 17:09:47 +03:00
holo_user_ctx = HoloUser ( ctx . user )
2023-05-06 19:56:01 +03:00
custom_channel = ds_utils . get (
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 = " Канал, вказаний як ваш, не існує. Можливо, його було вручну видалено раніше. " ,
2023-05-04 17:14:23 +03:00
color = Color . fail ,
)
)
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 } ` " ,
color = Color . fail ,
)
)
2023-05-04 17:09:47 +03:00
2023-05-04 17:14:23 +03:00
@customchannel.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 = " Підтвердження операції " )
2023-05-06 19:56:01 +03:00
async def customchannel_remove_cmd (
self , ctx : ApplicationContext , confirm : bool = False
) :
2023-05-04 17:09:47 +03:00
holo_user_ctx = HoloUser ( ctx . user )
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 = " У вас немає особистого каналу." ,
color = Color . fail ,
2023-05-06 18:09:06 +03:00
)
2024-12-16 00:21:41 +02:00
)
return
await ctx . defer ( )
custom_channel = ds_utils . get (
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 = " Канал, вказаний як ваш, не існує. Можливо, його було вручну видалено раніше. " ,
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`. " ,
2023-05-04 17:14:23 +03:00
color = Color . fail ,
)
)
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 20:54:53 +02:00
if ctx . channel_id != custom_channel . id :
await ctx . respond (
embed = Embed (
title = " Канал знищено " ,
description = " Ви відмовились від каналу та видалили його. " ,
color = Color . default ,
)
2024-12-16 00:21:41 +02:00
)
2024-06-23 13:05:03 +03:00
def setup ( client : PycordBot ) :
client . add_cog ( CustomChannels ( client ) )