aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Leon Sandøy <[email protected]>2020-07-29 23:46:23 +0200
committerGravatar Leon Sandøy <[email protected]>2020-07-29 23:46:23 +0200
commit0f8a89bd8be9b5bd6fbad989ad3aa57103a1f9da (patch)
tree4859534f64814070571f9e0626ee49dccb0d9724
parentMake sure file formats have leading dots. (diff)
Dynamically amend types to filterlist docstrings.
We want the !help invocations to give you all the information you need in order to use the command. That also means we need to provide the valid filterlist types, which are subject to change. So, we fetch the valid ones from the API and then dynamically insert them into the docstrings.
-rw-r--r--bot/cogs/filter_lists.py24
-rw-r--r--bot/converters.py19
2 files changed, 38 insertions, 5 deletions
diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py
index 8831a2143..fbd070bb9 100644
--- a/bot/cogs/filter_lists.py
+++ b/bot/cogs/filter_lists.py
@@ -17,8 +17,32 @@ log = logging.getLogger(__name__)
class FilterLists(Cog):
"""Commands for blacklisting and whitelisting things."""
+ methods_with_filterlist_types = [
+ "allow_add",
+ "allow_delete",
+ "allow_get",
+ "deny_add",
+ "deny_delete",
+ "deny_get",
+ ]
+
def __init__(self, bot: Bot) -> None:
self.bot = bot
+ self.bot.loop.create_task(self._amend_docstrings())
+
+ async def _amend_docstrings(self) -> None:
+ """Add the valid FilterList types to the docstrings, so they'll appear in !help invocations."""
+ await self.bot.wait_until_guild_available()
+
+ # Add valid filterlist types to the docstrings
+ valid_types = await ValidFilterListType.get_valid_types(self.bot)
+ valid_types = [f"`{type_.lower()}`" for type_ in valid_types]
+
+ for method_name in self.methods_with_filterlist_types:
+ command = getattr(self, method_name)
+ command.help = (
+ f"{command.help}\n\nValid **list_type** values are {', '.join(valid_types)}."
+ )
async def _add_data(
self,
diff --git a/bot/converters.py b/bot/converters.py
index 5912e3e61..c9f525dd1 100644
--- a/bot/converters.py
+++ b/bot/converters.py
@@ -9,7 +9,7 @@ import dateutil.tz
import discord
from aiohttp import ClientConnectorError
from dateutil.relativedelta import relativedelta
-from discord.ext.commands import BadArgument, Context, Converter, IDConverter, UserConverter
+from discord.ext.commands import BadArgument, Bot, Context, Converter, IDConverter, UserConverter
from bot.api import ResponseCodeError
from bot.constants import URLs
@@ -81,14 +81,23 @@ class ValidFilterListType(Converter):
passes through the given argument otherwise.
"""
- async def convert(self, ctx: Context, list_type: str) -> str:
- """Checks whether the given string is a valid FilterList type."""
+ @staticmethod
+ async def get_valid_types(bot: Bot) -> list:
+ """
+ Try to get a list of valid filter list types.
+
+ Raise a BadArgument if the API can't respond.
+ """
try:
- valid_types = await ctx.bot.api_client.get('bot/filter-lists/get-types')
+ valid_types = await bot.api_client.get('bot/filter-lists/get-types')
except ResponseCodeError:
raise BadArgument("Cannot validate list_type: Unable to fetch valid types from API.")
- valid_types = [enum for enum, classname in valid_types]
+ return [enum for enum, classname in valid_types]
+
+ async def convert(self, ctx: Context, list_type: str) -> str:
+ """Checks whether the given string is a valid FilterList type."""
+ valid_types = await self.get_valid_types(ctx.bot)
list_type = list_type.upper()
if list_type not in valid_types: