2021-07-15 22:54:09 +01:00
import os
from dotenv import load_dotenv # Import OS variables from Dotenv file.
load_dotenv ( ) # Load Dotenv. Delete this for production
import asyncio # Discord Py Dependency
import discord # Main Lib
from discord . ext import commands # Commands module
from discord_slash import SlashCommand , SlashContext , cog_ext , utils # Slash Command Library
from discord_slash . utils . manage_commands import create_choice , create_option # Slash Command features
2021-07-18 23:16:58 +01:00
from pprint import pprint
2021-07-15 22:54:09 +01:00
2021-07-18 23:16:58 +01:00
from bot import clearConfig , configFile , loadCog , loadCogs , setConfig , unloadCog , unloadCogs , yaml_dump , yaml_load , reloadCog , reloadCogs
2021-07-15 22:54:09 +01:00
##### Debug Cog
2021-07-16 23:53:31 +01:00
class Debug ( commands . Cog , name = ' Debug Commands ' ) :
2021-07-15 22:54:09 +01:00
def __init__ ( self , client ) :
self . client = client
2021-07-16 23:53:31 +01:00
#### Permission Check: Only available to the bot's maintainer.
async def cog_check ( self , ctx ) :
return ctx . author . id == int ( os . getenv ( ' BOT_MAINTAINER_ID ' ) )
2021-07-15 22:54:09 +01:00
@commands.command (
name = ' reloadcogs ' ,
description = ' Reloads cogs within the specified category, or provide `all` for all cogs. Default: `all`. ' ,
brief = ' Reload multiple cogs by category. '
)
2021-07-18 23:16:58 +01:00
async def _reload ( self , ctx , cog_category : str = ' --all ' ) :
reloadCogs ( cog_category )
2021-07-17 13:56:04 +01:00
await ctx . reply ( f ' ```` { cog_category } ` cogs have been reloaded.``` ' )
2021-07-15 22:54:09 +01:00
@commands.command (
name = ' unloadcogs ' ,
description = ' Unload cogs within the specified category, or provide `all` for all cogs. Default: `all`. ' ,
brief = ' Unload multiple cogs by category. '
)
2021-07-18 23:16:58 +01:00
async def _unloadcogs ( self , ctx , cog_category : str = ' --all ' ) :
2021-07-15 22:54:09 +01:00
unloadCogs ( cog_category )
loadCogs ( cog_category )
2021-07-17 13:56:04 +01:00
await ctx . reply ( f ' ```` { cog_category } ` cogs have been unloaded.``` ' )
2021-07-15 22:54:09 +01:00
@commands.command (
name = ' loadcogs ' ,
description = ' Load cogs within the specified category, or provide `all` for all cogs. Default: `all`. ' ,
brief = ' Load multiple cogs by category. '
)
2021-07-18 23:16:58 +01:00
async def _loadcogs ( self , ctx , cog_category : str = ' --all ' ) :
2021-07-15 22:54:09 +01:00
unloadCogs ( cog_category )
loadCogs ( cog_category )
2021-07-17 13:56:04 +01:00
await ctx . reply ( f ' ```` { cog_category } ` cogs have been loaded.``` ' )
2021-07-15 22:54:09 +01:00
@commands.command (
name = ' retrievecommands ' ,
2021-07-16 23:53:31 +01:00
aliases = [ ' slashcommands ' , ' retrieveslashcommands ' ] ,
description = ' Debugging command that retrieves all slash commands currently registered for this guild and this bot to the Python console. ' ,
brief = ' Retrieves registered slash commands to console. '
2021-07-15 22:54:09 +01:00
)
async def _retrievecommands ( self , ctx : commands . Context ) :
2021-07-18 23:16:58 +01:00
c = await utils . manage_commands . get_all_commands (
bot_id = self . client . user . id ,
bot_token = os . getenv ( ' TEST_3_TOKEN ' ) ,
guild_id = ctx . guild . id
)
pprint ( c )
2021-07-17 13:56:04 +01:00
await ctx . reply ( f ' ```All registered `/commands` have been fetched and sent to the Python console.``` ' )
2021-07-15 22:54:09 +01:00
2021-07-18 23:16:58 +01:00
@commands.command (
name = ' deletecommand ' ,
aliases = [ ' removecommand ' , ' delcommand ' , ' removeslashcommand ' , ' clearcommand ' , ' clearslashcommand ' ] ,
description = ' Debugging command that deletes a specified slash command. Key parameters `--all` for all commands in guild and `--global` for all commands globally ' ,
brief = ' Deletes slash command. Default: all local commands. '
)
async def _deleteCommand ( self , ctx : commands . Context , command : str = ' --all ' ) :
if command == ' --all ' or command == ' -a ' :
await utils . manage_commands . remove_all_commands (
bot_id = self . client . user . id ,
bot_token = os . getenv ( ' TEST_3_TOKEN ' ) ,
guild_ids = [ ctx . guild . id ]
)
2021-07-19 15:30:04 +01:00
await ctx . reply ( f ' ```All slash commands have been deleted for the guild ` { ctx . guild . name } ``.``` ' )
2021-07-18 23:16:58 +01:00
elif command == ' --global ' or command == ' -g ' :
await utils . manage_commands . remove_all_commands (
bot_id = self . client . user . id ,
bot_token = os . getenv ( ' TEST_3_TOKEN ' ) ,
guild_ids = None
)
await utils . manage_commands . remove_all_commands (
bot_id = self . client . user . id ,
bot_token = os . getenv ( ' TEST_3_TOKEN ' ) ,
guild_ids = [ int ( g ) for g in yaml_load ( configFile ) ]
)
await ctx . reply ( ' ```All slash commands have been deleted globally.``` ' )
else :
c = await utils . manage_commands . get_all_commands (
bot_id = self . client . user . id ,
bot_token = os . getenv ( ' TEST_3_TOKEN ' ) ,
guild_id = ctx . guild . id
)
target = list ( filter ( lambda t : t [ ' name ' ] == command , c ) ) [ 0 ] [ ' id ' ]
await utils . manage_commands . remove_slash_command (
bot_id = self . client . user . id ,
bot_token = os . getenv ( ' TEST_3_TOKEN ' ) ,
guild_id = ctx . guild . id ,
cmd_id = target
)
await ctx . reply ( f ' ```Slash command { command } has been deleted for the guild { ctx . guild . name } .``` ' )
@commands.command (
name = ' addcommand ' ,
aliases = [ ' installcommand ' , ' addslashcommand ' ] ,
description = ' Adds a slash command to the guild. Use keyword `--global` to add command globally. ' ,
brief = ' Adds slash command '
)
async def _addCommand ( self , ctx : commands . Context , command : str , key : str = ' ' ) :
await utils . manage_commands . add_slash_command (
bot_id = self . client . user . id ,
bot_token = os . getenv ( ' TEST_3_TOKEN ' ) ,
guild_id = None if key == ' --global ' or key == ' -g ' else ctx . guild . id ,
cmd_name = command ,
description = ' No Description '
)
await ctx . reply ( f ' ```The command / { command } has been added for the guild { ctx . guild . name } .``` ' )
2021-07-15 22:54:09 +01:00
@commands.command (
name = ' clearconfig ' ,
aliases = [ ' configclear ' ] ,
description = ' Clears any redundant entries in the config file of guilds the bot is not in. Does not require any argument. ' ,
brief = ' Clears redundant entries from config file. '
)
async def _clearconfig ( self , ctx : commands . Context ) :
conf = yaml_load ( configFile )
for key in list ( conf ) :
clearConfig ( key )
2021-07-17 13:56:04 +01:00
await ctx . reply ( f ' ```Config entries for unknown guilds have been cleared.``` ' )
2021-07-15 22:54:09 +01:00
@commands.command (
name = ' setconfig ' ,
aliases = [ ' configsetup ' ] ,
description = ' Creates a config entry for the current guild, and if there are existing entries sets default values. Ignores any values that have already been set. Does not require any argument as it infers the guild from the context in which it was called. ' ,
brief = ' Sets config entry for the current guild. '
)
async def _setconfig ( self , ctx : commands . Context ) :
setConfig ( ctx . guild )
2021-07-17 13:56:04 +01:00
await ctx . reply ( f ' ```Config entry has been added for guild ` { ctx . guild . name } `.``` ' )
2021-07-15 22:54:09 +01:00
2021-07-18 23:16:58 +01:00
@commands.command (
name = ' synccommands ' ,
aliases = [ ' syncallcommands ' , ' syncslashcommands ' , ' resynccommands ' , ' sync ' , ' resync ' , ' syncall ' , ' resyncall ' ] ,
description = ' Syncs all slash commands between the bot and the Server. ' ,
brief = ' Resyncs slash commands. '
)
async def _synccommands ( self , ctx : commands . Context ) :
await self . client . slash . sync_all_commands ( )
await ctx . reply ( f ' ```All slash commands have been synced with the Server.``` ' )
2021-07-15 22:54:09 +01:00
def setup ( client ) :
client . add_cog ( Debug ( client ) )