from __future__ import annotations import logging from collections.abc import Callable import discord from discord import app_commands from core import economy import strings as S def register_ops_channel_commands( tree: app_commands.CommandTree, bot: discord.Client, log: logging.Logger, get_allowed_channels: Callable[[], list[int]], set_allowed_channels: Callable[[list[int]], None], ) -> None: @tree.command(name="send", description=S.CMD["send"]) @app_commands.guild_only() @app_commands.describe( kanal=S.OPT["send_kanal"], sõnum=S.OPT["send_sõnum"], ) @app_commands.default_permissions(manage_guild=True) async def cmd_send(interaction: discord.Interaction, kanal: discord.TextChannel, sõnum: str): try: await kanal.send(sõnum) await interaction.response.send_message( S.SEND_UI["sent"].format(channel=kanal.mention), ephemeral=True ) except discord.Forbidden: await interaction.response.send_message( S.SEND_UI["forbidden"].format(channel=kanal.mention), ephemeral=True ) except Exception as e: await interaction.response.send_message( S.ERR["send_failed"].format(error=e), ephemeral=True ) @tree.command(name="economysetup", description=S.CMD["economysetup"]) @app_commands.guild_only() @app_commands.default_permissions(manage_guild=True) async def cmd_economysetup(interaction: discord.Interaction): await interaction.response.defer(ephemeral=True) guild = interaction.guild bot_member = guild.get_member(bot.user.id) bot_top_pos = max((r.position for r in bot_member.roles if r.managed), default=1) all_role_names = [economy.ECONOMY_ROLE] + [name for _, name in economy.LEVEL_ROLES] created, existing = [], [] for name in all_role_names: role = discord.utils.find(lambda r, n=name: r.name == n, guild.roles) if role is None: await guild.create_role(name=name, reason="/economysetup") created.append(name) else: existing.append(name) positions: dict[discord.Role, int] = {} base = max(bot_top_pos - 1, 1) for i, name in enumerate(all_role_names): role = discord.utils.find(lambda r, n=name: r.name == n, guild.roles) if role: positions[role] = max(base - i, 1) if positions: try: await guild.edit_role_positions(positions=positions) except discord.Forbidden: pass embed = discord.Embed(title=S.TITLE["economysetup"], color=0x57F287) if created: embed.add_field(name=S.ECONOMYSETUP_UI["created_field"], value="\n".join(created), inline=True) if existing: embed.add_field(name=S.ECONOMYSETUP_UI["existing_field"], value="\n".join(existing), inline=True) embed.set_footer(text=S.ECONOMYSETUP_UI["footer"]) await interaction.followup.send(embed=embed, ephemeral=True) log.info("/economysetup triggered by %s", interaction.user) @tree.command(name="allowchannel", description=S.CMD["allowchannel"]) @app_commands.guild_only() @app_commands.describe(kanal=S.OPT["allowchannel_kanal"]) @app_commands.default_permissions(manage_guild=True) async def cmd_allowchannel(interaction: discord.Interaction, kanal: discord.TextChannel): allowed = get_allowed_channels() if kanal.id in allowed: await interaction.response.send_message( S.CHANNEL_UI["already_allowed"].format(channel=kanal.mention), ephemeral=True ) return allowed.append(kanal.id) set_allowed_channels(allowed) log.info("ALLOWCHANNEL +%s by %s", kanal, interaction.user) await interaction.response.send_message( S.CHANNEL_UI["added"].format(channel=kanal.mention), ephemeral=True ) @tree.command(name="denychannel", description=S.CMD["denychannel"]) @app_commands.guild_only() @app_commands.describe(kanal=S.OPT["denychannel_kanal"]) @app_commands.default_permissions(manage_guild=True) async def cmd_denychannel(interaction: discord.Interaction, kanal: discord.TextChannel): allowed = get_allowed_channels() if kanal.id not in allowed: await interaction.response.send_message( S.CHANNEL_UI["not_in_list"].format(channel=kanal.mention), ephemeral=True ) return allowed.remove(kanal.id) set_allowed_channels(allowed) log.info("DENYCHANNEL -%s by %s", kanal, interaction.user) if allowed: await interaction.response.send_message( S.CHANNEL_UI["removed"].format(channel=kanal.mention), ephemeral=True ) else: await interaction.response.send_message( S.CHANNEL_UI["removed_last"].format(channel=kanal.mention), ephemeral=True ) @tree.command(name="channels", description=S.CMD["channels"]) @app_commands.guild_only() @app_commands.default_permissions(manage_guild=True) async def cmd_channels(interaction: discord.Interaction): allowed = get_allowed_channels() if not allowed: desc = S.CHANNEL_UI["list_empty"] else: lines = "\n".join(f"• <#{cid}>" for cid in allowed) desc = S.CHANNEL_UI["list_filled"].format(lines=lines) embed = discord.Embed(title=S.CHANNEL_UI["list_title"], description=desc, color=0x5865F2) await interaction.response.send_message(embed=embed, ephemeral=True)