ia_bot/cogs/admin.py
2026-08-27 16:17:12 +10:00

154 lines
5.5 KiB
Python

"""Admin cog for emoji management commands."""
import discord
from discord.ext import commands
from discord.ext.commands import Cog, Context, has_permissions
import logging
logger = logging.getLogger(__name__)
class AdminCog(Cog):
"""Admin commands for managing emojis."""
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
def cog_check(self, ctx: Context) -> bool:
"""Check if the user has permission to use admin commands."""
return ctx.author.guild_permissions.manage_emojis_and_stickers
@commands.command(name="list-emojis", aliases=["emojis", "list"])
@commands.has_permissions(manage_emojis_and_stickers=True)
async def list_emojis(self, ctx: Context) -> None:
"""List all custom emojis in the guild."""
emojis = await self.bot.fetch_emojis(ctx.guild)
emoji_list = []
for emoji in emojis:
emoji_list.append(f"👍 `{emoji.name}` (ID: {emoji.id})")
if not emoji_list:
await ctx.send("No custom emojis found in this guild.")
else:
await ctx.send("\n".join(emoji_list))
@commands.command(name="create-emoji", aliases=["upload", "add"])
@commands.has_permissions(manage_emojis_and_stickers=True)
async def create_emoji(self, ctx: Context, name: str, *, file_path: str) -> None:
"""Upload a custom emoji (JPG, PNG, or GIF)."""
if not ctx.guild:
await ctx.send("This command can only be used in a server.")
return
if not ctx.guild.permissions_for(ctx.author).manage_emojis_and_stickers:
await ctx.send("You need manage_emojis_and_stickers permission.")
return
try:
with open(file_path, "rb") as f:
image_bytes = f.read()
await ctx.guild.create_custom_emoji(name=name, image=image_bytes)
await ctx.send(f"Successfully uploaded emoji `{name}`!")
except FileNotFoundError:
await ctx.send("Error: File not found.")
except Exception as e:
await ctx.send(f"Error uploading emoji: {e}")
@commands.command(name="delete-emoji", aliases=["remove", "delete"])
@commands.has_permissions(manage_emojis_and_stickers=True)
async def delete_emoji(self, ctx: Context, *, name: str) -> None:
"""Delete a custom emoji."""
if not ctx.guild:
await ctx.send("This command can only be used in a server.")
return
if not ctx.guild.permissions_for(ctx.author).manage_emojis_and_stickers:
await ctx.send("You need manage_emojis_and_stickers permission.")
return
try:
emojis = await self.bot.fetch_emojis(ctx.guild)
emoji = None
for e in emojis:
if e.name.lower() == name.lower():
emoji = e
break
if emoji is None:
await ctx.send(f"Emoji `{name}` not found.")
return
await emoji.delete()
await ctx.send(f"Successfully deleted emoji `{name}`!")
except discord.NotFound:
await ctx.send(f"Emoji `{name}` not found.")
except Exception as e:
await ctx.send(f"Error deleting emoji: {e}")
@commands.command(name="edit-emoji", aliases=["rename", "edit"])
@commands.has_permissions(manage_emojis_and_stickers=True)
async def edit_emoji(self, ctx: Context, *, name: str) -> None:
"""Edit an emoji's name or image."""
if not ctx.guild:
await ctx.send("This command can only be used in a server.")
return
if not ctx.guild.permissions_for(ctx.author).manage_emojis_and_stickers:
await ctx.send("You need manage_emojis_and_stickers permission.")
return
try:
emojis = await self.bot.fetch_emojis(ctx.guild)
emoji = None
for e in emojis:
if e.name.lower() == name.lower():
emoji = e
break
if emoji is None:
await ctx.send(f"Emoji `{name}` not found.")
return
await ctx.send(f"Emoji `{name}` found.\nUse `/rename` to change the name.")
except discord.NotFound:
await ctx.send(f"Emoji `{name}` not found.")
except Exception as e:
await ctx.send(f"Error: {e}")
@commands.command(name="rename", aliases=["edit"])
@commands.has_permissions(manage_emojis_and_stickers=True)
async def rename(self, ctx: Context, *, name: str) -> None:
"""Rename an emoji."""
if not ctx.guild:
await ctx.send("This command can only be used in a server.")
return
if not ctx.guild.permissions_for(ctx.author).manage_emojis_and_stickers:
await ctx.send("You need manage_emojis_and_stickers permission.")
return
try:
emojis = await self.bot.fetch_emojis(ctx.guild)
emoji = None
for e in emojis:
if e.name.lower() == name.lower():
emoji = e
break
if emoji is None:
await ctx.send(f"Emoji `{name}` not found.")
return
await ctx.send(f"Emoji `{name}` found.\nUse `/delete` to delete it.")
except discord.NotFound:
await ctx.send(f"Emoji `{name}` not found.")
except Exception as e:
await ctx.send(f"Error: {e}")
async def setup(bot: commands.Bot) -> None:
"""Register the admin cog with the bot."""
await bot.add_cog(AdminCog(bot))