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-06-23 13:05:03 +03:00
from libbot . pycord . classes import PycordBot
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
2023-05-04 17:09:47 +03:00
from modules . utils import config_get
from modules . utils_sync import config_get_sync , guild_name
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 ) :
2023-05-06 19:56:01 +03:00
col_users . find_one_and_update (
{ " 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 = " Отримати персональний текстовий канал " ,
guild_ids = [ config_get_sync ( " guild " ) ] ,
)
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 )
if holo_user_ctx . customchannel == None :
await ctx . defer ( )
2023-05-04 17:14:23 +03:00
created_channel = await ctx . user . guild . create_text_channel (
name = name ,
2023-05-06 18:09:06 +03:00
reason = f " Користувач { guild_name ( ctx . user ) } отримав власний приватний канал " ,
2023-05-06 16:21:15 +03:00
category = ds_utils . get (
2023-05-04 17:14:23 +03:00
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 ,
2023-05-04 17:09:47 +03:00
)
holo_user_ctx . set ( " customchannel " , created_channel . id )
2023-05-04 17:14:23 +03:00
await ctx . respond (
embed = Embed (
title = " Створено канал " ,
description = f " Вітаємо! Ви створили канал { created_channel . mention } . Для керування ним користуйтесь меню налаштувань каналу а також командою `/customchannel edit` " ,
color = Color . success ,
)
)
2023-05-04 17:09:47 +03:00
bots = await config_get ( " bots " )
for bot in bots :
2023-05-04 17:14:23 +03:00
await created_channel . set_permissions (
2023-05-06 16:21:15 +03:00
ds_utils . get ( ctx . user . guild . roles , id = bots [ bot ] [ " role " ] ) ,
2023-05-04 17:14:23 +03:00
view_channel = False ,
)
2023-05-04 17:09:47 +03:00
else :
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 ,
)
)
2023-05-04 17:09:47 +03:00
2023-05-04 17:14:23 +03:00
@customchannel.command (
name = " edit " ,
description = " Змінити параметри особистого каналу " ,
guild_ids = [ config_get_sync ( " guild " ) ] ,
)
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
)
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
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 ,
)
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 = " Відібрати канал, знищуючи його, та частково повернути кошти " ,
guild_ids = [ config_get_sync ( " guild " ) ] ,
)
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 )
if holo_user_ctx . customchannel is not None :
await ctx . defer ( )
2023-05-06 16:21:15 +03:00
custom_channel = ds_utils . get (
2023-05-04 17:14:23 +03:00
ctx . guild . channels , id = holo_user_ctx . customchannel
)
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
holo_user_ctx . set ( " customchannel " , None )
return
2023-05-06 18:09:06 +03:00
if not confirm :
await ctx . respond (
embed = Embed (
title = " Підтвердження не надано " ,
2024-06-23 13:05:03 +03:00
description = " Для підтвердження операції додайте до команди параметр `confirm` зі значенням `True`. " ,
2023-05-06 18:09:06 +03:00
color = Color . fail ,
)
)
return
await custom_channel . delete ( reason = " Власник запросив видалення " )
2023-05-04 17:09:47 +03:00
holo_user_ctx . set ( " customchannel " , 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 . default ,
)
)
2023-05-04 17:09:47 +03:00
else :
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 = " У вас немає особистого каналу." ,
2023-05-04 17:14:23 +03:00
color = Color . fail ,
)
)
2024-06-23 13:05:03 +03:00
def setup ( client : PycordBot ) :
client . add_cog ( CustomChannels ( client ) )