paper-dynasty-discord/cogs/owner.py
Cal Corum 073bd04b4b Added RosterLinks to remove card_id from setup process
Add SelectStartingPitcher dropdown
New .sync function
2024-11-23 19:53:48 -06:00

139 lines
5.0 KiB
Python

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
logger = logging.getLogger('discord_app')
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}')
logger.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}')
logger.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}')
logger.warning(f'Unloaded {cog}')
await self.bot.load_extension(f'cogs.{cog}')
logger.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}')
logger.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}')
logger.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')))
# logger.warning(f'sync: {sync}')
# except Exception as e:
# logger.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
This takes all global commands within the CommandTree and sends them to Discord. (see CommandTree for more info.)
!sync ~
This will sync all guild commands for the current contexts guild.
!sync *
This command copies all global commands to the current guild (within the CommandTree) and syncs.
!sync ^
This command will remove all guild commands from the CommandTree and syncs, which effectively removes all commands from the guild.
!sync 123 456 789
This command will sync the 3 guild ids we passed: 123, 456 and 789. Only their guilds and guild-bound commands.
"""
logger.info(f'{ctx.author.name} has initiated a sync from guild ID {ctx.guild.id}')
if not guilds:
if spec == "~":
synced = await ctx.bot.tree.sync(guild=ctx.guild)
elif spec == "*":
ctx.bot.tree.copy_global_to(guild=ctx.guild)
synced = await ctx.bot.tree.sync(guild=ctx.guild)
elif spec == "^":
ctx.bot.tree.clear_commands(guild=ctx.guild)
await ctx.bot.tree.sync(guild=ctx.guild)
synced = []
else:
synced = await ctx.bot.tree.sync()
await ctx.send(
f"Synced {len(synced)} commands {'globally' if spec is None else 'to the current guild.'}"
)
return
ret = 0
for guild in guilds:
try:
await ctx.bot.tree.sync(guild=guild)
except discord.HTTPException:
pass
else:
ret += 1
await ctx.send(f"Synced the tree to {ret}/{len(guilds)}.")
async def setup(bot):
await bot.add_cog(Owner(bot))