import datetime import logging import os import discord from discord import Object from discord.ext import commands from discord.ext.commands import Greedy, Context from typing import Optional, Literal date = f'{datetime.datetime.now().year}-{datetime.datetime.now().month}-{datetime.datetime.now().day}' logging.basicConfig( filename=f'logs/{date}.log', format='%(asctime)s - %(levelname)s - %(message)s', level=logging.WARNING ) class Owner(commands.Cog): def __init__(self, bot): self.bot = bot @commands.command(hidden=True) @commands.is_owner() async def load(self, ctx, *, cog: str): try: await self.bot.load_extension(f'cogs.{cog}') logging.warning(f'Loaded {cog}') except Exception as e: await ctx.send(f'**ERROR:** {type(e).__name__} - {e}') else: await ctx.send('**SUCCESS**') @commands.command(hidden=True) @commands.is_owner() async def unload(self, ctx, *, cog: str): try: await self.bot.unload_extension(f'cogs.{cog}') logging.warning(f'Unloaded {cog}') except Exception as e: await ctx.send(f'**ERROR:** {type(e).__name__} - {e}') else: await ctx.send('**SUCCESS**') @commands.command(hidden=True) @commands.is_owner() async def reload(self, ctx, *, cog: str): try: await self.bot.unload_extension(f'cogs.{cog}') logging.warning(f'Unloaded {cog}') await self.bot.load_extension(f'cogs.{cog}') logging.warning(f'Reloaded {cog}') except Exception as e: await ctx.send(f'**ERROR:** {type(e).__name__} - {e}') else: await ctx.send('**SUCCESS**') @commands.command(hidden=True) @commands.is_owner() async def fullreset(self, ctx): cogs = ['helpers', 'admins', 'economy', 'players'] for x in cogs: try: await self.bot.unload_extension(f'cogs.{x}') logging.warning(f'Unloaded {x}') except Exception as e: await ctx.send(f'Failed to unload **{x}**') for x in cogs: try: await self.bot.load_extension(f'cogs.{x}') logging.warning(f'Loaded {x}') except Exception as e: await ctx.send(f'Failed to load **{x}**') await ctx.send('**SUCCESS**') # @commands.command(name='sync', hidden=True) # @commands.is_owner() # async def sync_command(self, ctx, sync_type: str = 'local'): # sync = None # await ctx.send('I will try to sync slash commands...') # try: # if sync_type == 'global': # sync = await self.bot.tree.sync() # else: # sync = await self.bot.tree.sync(guild=discord.Object(os.environ.get('GUILD_ID'))) # logging.warning(f'sync: {sync}') # except Exception as e: # logging.error(f'failed to sync: {e}') # # await ctx.send(f'Just ran the sync. Here is the output:\n{sync}') @commands.command(name='sync', help='~ current guild, * global -> current, ! clear and sync current') @commands.is_owner() async def sync(self, ctx: Context, guilds: Greedy[Object], spec: Optional[Literal['~', "*", '!']] = None) -> None: """ !sync -> global sync !sync ~ -> sync current guild !sync * -> copies all global app commands to current guild and syncs !sync id_1 id_2 -> syncs guilds with id 1 and 2 """ if not guilds: if spec == "~": fmt = await ctx.bot.tree.sync(guild=ctx.guild) elif spec == "*": ctx.bot.tree.copy_global_to(guild=ctx.guild) fmt = await ctx.bot.tree.sync(guild=ctx.guild) elif spec == '!': fmt = await ctx.bot.tree.sync() await ctx.send(f'Synced {len(fmt)} commands globally.') ctx.bot.tree.clear_commands(guild=ctx.guild) await ctx.send(f'Cleared all local commands.') if len(fmt) > 0: fmt = await ctx.bot.tree.sync(guild=ctx.guild) await ctx.send(f'Synced global commands to this guild.') else: fmt = await ctx.bot.tree.sync() await ctx.send( f"Synced {len(fmt)} commands {'globally' if spec is None else 'to the current guild.'}" ) return fmt = 0 for guild in guilds: try: await ctx.bot.tree.sync(guild=guild) except discord.HTTPException: pass else: fmt += 1 await ctx.send(f"Synced the tree to {fmt}/{len(guilds)} guilds.") async def setup(bot): await bot.add_cog(Owner(bot))