From d8710f0c7dcff4525af32936fa6aa2aa3896ce17 Mon Sep 17 00:00:00 2001 From: Will Date: Wed, 12 Jul 2017 17:41:22 -0400 Subject: [PATCH] [Downloader] [p]findcog: Find a cog from a command (#855) --- cogs/downloader/downloader.py | 70 ++++++++++++++++++++++++++++++++++- core/core_commands.py | 4 ++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/cogs/downloader/downloader.py b/cogs/downloader/downloader.py index e1d7c4188..54bd0da0c 100644 --- a/cogs/downloader/downloader.py +++ b/cogs/downloader/downloader.py @@ -1,6 +1,6 @@ import os import shutil -from typing import Tuple +from typing import Tuple, Union import discord from discord.ext import commands @@ -318,3 +318,71 @@ class Downloader: msg = "Information on {}:\n{}".format(cog.name, cog.description or "") await ctx.send(box(msg)) + + def is_installed(self, cog_name: str) -> (bool, Union[Installable, None]): + """ + Checks to see if a cog with the given name was installed + through Downloader. + :param cog_name: + :return: is_installed, Installable + """ + for installable in self.installed_cogs: + if installable.name == cog_name: + return True, installable + return False, None + + def format_findcog_info(self, command_name: str, + cog_installable: Union[Installable, object]=None) -> str: + """ + Formats the info for output to discord + :param command_name: + :param cog_installable: Can be an Installable instance or a Cog instance. + :return: str + """ + if isinstance(cog_installable, Installable): + made_by = ", ".join(cog_installable.author) or "Missing from info.json" + repo = self._repo_manager.get_repo(cog_installable.repo_name) + repo_url = repo.url + cog_name = cog_installable.name + else: + made_by = "26 & co." + repo_url = "https://github.com/Twentysix26/Red-DiscordBot" + cog_name = cog_installable.__class__.__name__ + + msg = "Command: {}\nMade by: {}\nRepo: {}\nCog name: {}" + + return msg.format(command_name, made_by, repo_url, cog_name) + + def cog_name_from_instance(self, instance: object) -> str: + """ + Determines the cog name that Downloader knows from the cog instance. + + Probably. + :param instance: + :return: + """ + splitted = instance.__module__.split('.') + return splitted[-2] + + @commands.command() + async def findcog(self, ctx: commands.Context, command_name: str): + """ + Figures out which cog a command comes from. Only works with loaded + cogs. + """ + command = ctx.bot.all_commands.get(command_name) + + if command is None: + await ctx.send("That command doesn't seem to exist.") + return + + # Check if in installed cogs + cog_name = self.cog_name_from_instance(command.instance) + installed, cog_installable = self.is_installed(cog_name) + if installed: + msg = self.format_findcog_info(command_name, cog_installable) + else: + # Assume it's in a base cog + msg = self.format_findcog_info(command_name, command.instance) + + await ctx.send(box(msg)) diff --git a/core/core_commands.py b/core/core_commands.py index 8d1de8a5d..7a889fd98 100644 --- a/core/core_commands.py +++ b/core/core_commands.py @@ -55,6 +55,10 @@ class Core: @checks.is_owner() async def _reload(self, ctx, *, cog_name: str): """Reloads a package""" + if cog_name == "downloader": + await ctx.send("DONT RELOAD DOWNLOADER.") + return + if not cog_name.startswith("cogs."): cog_name = "cogs." + cog_name