Discord/cogs/custom_channels.py

76 lines
5.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from discord import ApplicationContext, option, utils, Embed
from discord.ext import commands
from discord.commands import SlashCommandGroup
from classes.holo_user import HoloUser
from enums.colors import Color
from modules.utils import config_get
from modules.utils_sync import config_get_sync, guild_name
class CustomChannels(commands.Cog):
def __init__(self, client):
self.client = client
customchannel = SlashCommandGroup("customchannel", "Керування особистим каналом") # discord.create_group("customrole", description="Керування особистою роллю")
@customchannel.command(name="buy", description="Отримати персональний текстовий канал", guild_ids=config_get_sync("guilds"))
@option("name", description="Назва каналу")
@option("reactions", description="Дозволити реакції")
@option("threads", description="Дозволити гілки")
async def customchannel_buy_cmd(self, ctx: ApplicationContext, name: str, reactions: bool, threads: bool):
holo_user_ctx = HoloUser(ctx.user)
if holo_user_ctx.customchannel == None:
await ctx.defer()
created_channel = await ctx.user.guild.create_text_channel(name=name,
reason=f"Користувач {guild_name(ctx.user)} купив канал",
category=utils.get(ctx.author.guild.categories, id=await config_get("customchannel", "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)
# col_users.update_one(filter={"_id": holo_user_ctx.db_id}, update={ "$set": { "customchannel": created_channel.id } })
holo_user_ctx.set("customchannel", created_channel.id)
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(utils.get(ctx.user.guild.roles, id=bots[bot]["role"]), view_channel=False)
else:
await ctx.defer(ephemeral=True)
await ctx.respond(embed=Embed(title="Помилка виконання", description=f"У вас вже є особистий канал.\nДля редагування каналу є `/customchannel edit` або просто відкрийте меню керування вашим каналом.", color=Color.fail))
@customchannel.command(name="edit", description="Змінити параметри особистого каналу", guild_ids=config_get_sync("guilds"))
@option("name", description="Назва каналу")
@option("reactions", description="Дозволити реакції")
@option("threads", description="Дозволити гілки")
async def customchannel_edit_cmd(self, ctx: ApplicationContext, name:str, reactions: bool, threads: bool):
holo_user_ctx = HoloUser(ctx.user)
custom_channel = utils.get(ctx.guild.channels, id=holo_user_ctx.customchannel)
if custom_channel is None:
await ctx.respond(embed=Embed(title="Канал не знайдено", description=f"Канал, вказаний як ваш, не існує. Можливо, його було вручну видалено раніше.", color=Color.fail))
return
await custom_channel.edit(name=name)
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))
@customchannel.command(name="refund", description="Відібрати канал, знищуючи його, та частково повернути кошти", guild_ids=config_get_sync("guilds"))
async def customchannel_refund_cmd(self, ctx: ApplicationContext):
holo_user_ctx = HoloUser(ctx.user)
if holo_user_ctx.customchannel is not None:
await ctx.defer()
custom_channel = utils.get(ctx.guild.channels, id=holo_user_ctx.customchannel)
if custom_channel is None:
await ctx.respond(embed=Embed(title="Канал не знайдено", description=f"Канал, вказаний як ваш, не існує. Можливо, його було вручну видалено раніше.", color=Color.fail))
holo_user_ctx.set("customchannel", None)
return
await custom_channel.delete(reason="Повернення коштів")
holo_user_ctx.set("customchannel", None)
await ctx.respond(embed=Embed(title="Канал знищено", description=f"Ви відмовились від каналу.", color=Color.default))
else:
await ctx.defer(ephemeral=True)
await ctx.respond(embed=Embed(title="Помилка виконання", description=f"У вас немає особистого каналу.", color=Color.fail))