Spaces:
Runtime error
Runtime error
File size: 5,090 Bytes
b93b587 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
from discord.ext import commands
import discord
import os
from utils.decorator import auto_delete
from utils.func import mapping_cog
from utils.color_printer import cpr
from utils.config import config
class Admin(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
@commands.hybrid_command(
name="sync", description="Sync hybrid commands.", hidden=True
)
@commands.is_owner()
@auto_delete(delay=0)
async def sync(self, ctx: commands.Context):
await self.bot.tree.sync()
await ctx.send("Synced hybrid commands.", ephemeral=True, delete_after=5)
@commands.hybrid_command(
name="list",
description="List all loaded cogs.",
hidden=True,
)
@commands.is_owner()
@auto_delete(delay=0)
async def list(self, ctx: commands.Context):
cogs = [cog for cog in self.bot.cogs]
await ctx.send(
f"Loaded cogs: {', '.join(cogs)}", ephemeral=True, delete_after=5
)
@commands.hybrid_command(name="load", description="加载指定的cog")
@commands.is_owner()
@auto_delete(delay=0)
async def load(self, ctx: commands.Context, cog: str):
try:
# 先发送消息
await ctx.send(f"正在加载 cog: {cog}...", ephemeral=True, delete_after=5)
# 然后加载cog
await self.bot.load_extension(f"cogs.{mapping_cog(cog)}")
except Exception as e:
await ctx.channel.send(f"加载 {cog} 失败: {str(e)}", delete_after=5)
@commands.hybrid_command(name="unload", description="卸载指定的cog")
@commands.is_owner()
@auto_delete(delay=0)
async def unload(self, ctx: commands.Context, cog: str):
try:
# 先发送消息
await ctx.send(f"正在卸载 cog: {cog}...", ephemeral=True, delete_after=5)
# 然后卸载cog
await self.bot.unload_extension(f"cogs.{mapping_cog(cog)}")
except Exception as e:
await ctx.channel.send(f"卸载 {cog} 失败: {str(e)}", delete_after=5)
@commands.hybrid_command(name="reload", description="重新加载指定的cog")
@commands.is_owner()
@auto_delete(delay=0)
async def reload(self, ctx: commands.Context, cog: str):
try:
# 先发送消息
await ctx.send(f"正在重载 cog: {cog}...", ephemeral=True, delete_after=5)
# 然后重载cog
await self.bot.reload_extension(f"cogs.{mapping_cog(cog)}")
except Exception as e:
await ctx.channel.send(f"重载 {cog} 失败: {str(e)}", delete_after=5)
@commands.hybrid_command(
name="reload_all",
description="重新加载所有已加载的cog",
hidden=True,
)
@commands.is_owner()
@auto_delete(delay=0)
async def reload_all(self, ctx: commands.Context):
success_cogs = []
failed_cogs = []
# 获取当前已加载的cog列表
loaded_cogs = list(self.bot.cogs.keys())
for cog_name in loaded_cogs:
try:
# 将cog名称转换为文件名格式
file_name = mapping_cog(cog_name)
await self.bot.reload_extension(f"cogs.{file_name}")
success_cogs.append(cog_name)
except Exception as e:
failed_cogs.append(f"{cog_name} ({str(e)})")
# 构建响应消息
response = []
if success_cogs:
response.append(f"✅ 成功重载的cog: {', '.join(success_cogs)}")
if failed_cogs:
response.append(f"❌ 重载失败的cog: {', '.join(failed_cogs)}")
await ctx.send("\n".join(response), ephemeral=True, delete_after=10)
@commands.hybrid_command(
name="nickname", description="Change nickname.", hidden=True
)
@commands.is_owner()
@commands.guild_only()
@auto_delete(delay=0)
async def nickname(self, ctx: commands.Context, *, nickname: str):
await ctx.guild.me.edit(nick=nickname)
await ctx.send(f"Hola, I'm now {nickname}, どうぞよろしく!")
@commands.hybrid_command(
name="reload_config", description="重新加载配置文件", hidden=True
)
@commands.is_owner()
@auto_delete(delay=0)
async def reload_config(self, ctx: commands.Context):
config.reload()
await ctx.send("Reloaded config.", ephemeral=True, delete_after=5)
@commands.hybrid_command(name="status", description="Change status.", hidden=True)
@commands.is_owner()
@auto_delete(delay=0)
async def status(self, ctx: commands.Context, *, status: str):
await self.bot.change_presence(activity=discord.Game(name=status))
await ctx.send(f"Changed status to {status}.", ephemeral=True, delete_after=5)
async def setup(bot: commands.Bot):
await bot.add_cog(Admin(bot))
print(cpr.success("Cog loaded: Admin"))
|