[Alias] Create caching to call config less frequently (#3788)

This commit is contained in:
TrustyJAID
2020-04-26 18:25:41 -06:00
committed by GitHub
parent a1095285e4
commit 6f6c536236
6 changed files with 269 additions and 239 deletions

View File

@@ -1,16 +1,15 @@
from copy import copy
from re import findall, search
from re import search
from string import Formatter
from typing import Generator, Tuple, Iterable, Optional
from typing import Dict
import discord
from discord.ext.commands.view import StringView
from redbot.core import Config, commands, checks
from redbot.core.i18n import Translator, cog_i18n
from redbot.core.utils.chat_formatting import box
from redbot.core.bot import Red
from .alias_entry import AliasEntry
from .alias_entry import AliasEntry, AliasCache, ArgParseError
_ = Translator("Alias", __file__)
@@ -26,10 +25,6 @@ class _TrackingFormatter(Formatter):
return super().get_value(key, args, kwargs)
class ArgParseError(Exception):
pass
@cog_i18n(_)
class Alias(commands.Cog):
"""Create aliases for commands.
@@ -42,9 +37,9 @@ class Alias(commands.Cog):
and append them to the stored alias.
"""
default_global_settings = {"entries": []}
default_global_settings: Dict[str, list] = {"entries": []}
default_guild_settings = {"enabled": False, "entries": []} # Going to be a list of dicts
default_guild_settings: Dict[str, list] = {"entries": []} # Going to be a list of dicts
def __init__(self, bot: Red):
super().__init__()
@@ -53,40 +48,12 @@ class Alias(commands.Cog):
self.config.register_global(**self.default_global_settings)
self.config.register_guild(**self.default_guild_settings)
self._aliases: AliasCache = AliasCache(config=self.config, cache_enabled=True)
async def unloaded_aliases(self, guild: discord.Guild) -> Generator[AliasEntry, None, None]:
return (AliasEntry.from_json(d) for d in (await self.config.guild(guild).entries()))
async def unloaded_global_aliases(self) -> Generator[AliasEntry, None, None]:
return (AliasEntry.from_json(d) for d in (await self.config.entries()))
async def loaded_aliases(self, guild: discord.Guild) -> Generator[AliasEntry, None, None]:
return (
AliasEntry.from_json(d, bot=self.bot)
for d in (await self.config.guild(guild).entries())
)
async def loaded_global_aliases(self) -> Generator[AliasEntry, None, None]:
return (AliasEntry.from_json(d, bot=self.bot) for d in (await self.config.entries()))
async def is_alias(
self,
guild: Optional[discord.Guild],
alias_name: str,
server_aliases: Iterable[AliasEntry] = (),
) -> Tuple[bool, Optional[AliasEntry]]:
if not server_aliases and guild is not None:
server_aliases = await self.unloaded_aliases(guild)
global_aliases = await self.unloaded_global_aliases()
for aliases in (server_aliases, global_aliases):
for alias in aliases:
if alias.name == alias_name:
return True, alias
return False, None
async def initialize(self):
# This can be where we set the cache_enabled attribute later
if not self._aliases._loaded:
await self._aliases.load_aliases()
def is_command(self, alias_name: str) -> bool:
"""
@@ -100,56 +67,6 @@ class Alias(commands.Cog):
def is_valid_alias_name(alias_name: str) -> bool:
return not bool(search(r"\s", alias_name)) and alias_name.isprintable()
async def add_alias(
self, ctx: commands.Context, alias_name: str, command: str, global_: bool = False
) -> AliasEntry:
indices = findall(r"{(\d*)}", command)
if indices:
try:
indices = [int(a[0]) for a in indices]
except IndexError:
raise ArgParseError(_("Arguments must be specified with a number."))
low = min(indices)
indices = [a - low for a in indices]
high = max(indices)
gaps = set(indices).symmetric_difference(range(high + 1))
if gaps:
raise ArgParseError(
_("Arguments must be sequential. Missing arguments: ")
+ ", ".join(str(i + low) for i in gaps)
)
command = command.format(*(f"{{{i}}}" for i in range(-low, high + low + 1)))
alias = AliasEntry(alias_name, command, ctx.author, global_=global_)
if global_:
settings = self.config
else:
settings = self.config.guild(ctx.guild)
await settings.enabled.set(True)
async with settings.entries() as curr_aliases:
curr_aliases.append(alias.to_json())
return alias
async def delete_alias(
self, ctx: commands.Context, alias_name: str, global_: bool = False
) -> bool:
if global_:
settings = self.config
else:
settings = self.config.guild(ctx.guild)
async with settings.entries() as aliases:
for alias in aliases:
alias_obj = AliasEntry.from_json(alias)
if alias_obj.name == alias_name:
aliases.remove(alias)
return True
return False
async def get_prefix(self, message: discord.Message) -> str:
"""
Tries to determine what prefix is used in a message object.
@@ -167,57 +84,11 @@ class Alias(commands.Cog):
return p
raise ValueError(_("No prefix found."))
def get_extra_args_from_alias(
self, message: discord.Message, prefix: str, alias: AliasEntry
) -> str:
"""
When an alias is executed by a user in chat this function tries
to get any extra arguments passed in with the call.
Whitespace will be trimmed from both ends.
:param message:
:param prefix:
:param alias:
:return:
"""
known_content_length = len(prefix) + len(alias.name)
extra = message.content[known_content_length:]
view = StringView(extra)
view.skip_ws()
extra = []
while not view.eof:
prev = view.index
word = view.get_quoted_word()
if len(word) < view.index - prev:
word = "".join((view.buffer[prev], word, view.buffer[view.index - 1]))
extra.append(word)
view.skip_ws()
return extra
async def maybe_call_alias(
self, message: discord.Message, aliases: Iterable[AliasEntry] = None
):
try:
prefix = await self.get_prefix(message)
except ValueError:
return
try:
potential_alias = message.content[len(prefix) :].split(" ")[0]
except IndexError:
return False
is_alias, alias = await self.is_alias(
message.guild, potential_alias, server_aliases=aliases
)
if is_alias:
await self.call_alias(message, prefix, alias)
async def call_alias(self, message: discord.Message, prefix: str, alias: AliasEntry):
new_message = copy(message)
try:
args = self.get_extra_args_from_alias(message, prefix, alias)
except commands.BadArgument as bae:
args = alias.get_extra_args_from_alias(message, prefix)
except commands.BadArgument:
return
trackform = _TrackingFormatter()
@@ -257,8 +128,8 @@ class Alias(commands.Cog):
)
return
is_alias, something_useless = await self.is_alias(ctx.guild, alias_name)
if is_alias:
alias = await self._aliases.get_alias(ctx.guild, alias_name)
if alias:
await ctx.send(
_(
"You attempted to create a new alias"
@@ -292,7 +163,7 @@ class Alias(commands.Cog):
# and that the alias name is valid.
try:
await self.add_alias(ctx, alias_name, command)
await self._aliases.add_alias(ctx, alias_name, command)
except ArgParseError as e:
return await ctx.send(" ".join(e.args))
@@ -316,8 +187,8 @@ class Alias(commands.Cog):
)
return
is_alias, something_useless = await self.is_alias(ctx.guild, alias_name)
if is_alias:
alias = await self._aliases.get_alias(ctx.guild, alias_name)
if alias:
await ctx.send(
_(
"You attempted to create a new global alias"
@@ -341,7 +212,7 @@ class Alias(commands.Cog):
# endregion
try:
await self.add_alias(ctx, alias_name, command, global_=True)
await self._aliases.add_alias(ctx, alias_name, command, global_=True)
except ArgParseError as e:
return await ctx.send(" ".join(e.args))
@@ -355,8 +226,8 @@ class Alias(commands.Cog):
@commands.guild_only()
async def _help_alias(self, ctx: commands.Context, alias_name: str):
"""Try to execute help for the base command of the alias."""
is_alias, alias = await self.is_alias(ctx.guild, alias_name=alias_name)
if is_alias:
alias = await self._aliases.get_alias(ctx.guild, alias_name=alias_name)
if alias:
if self.is_command(alias.command):
base_cmd = alias.command
else:
@@ -372,9 +243,9 @@ class Alias(commands.Cog):
@commands.guild_only()
async def _show_alias(self, ctx: commands.Context, alias_name: str):
"""Show what command the alias executes."""
is_alias, alias = await self.is_alias(ctx.guild, alias_name)
alias = await self._aliases.get_alias(ctx.guild, alias_name)
if is_alias:
if alias:
await ctx.send(
_("The `{alias_name}` alias will execute the command `{command}`").format(
alias_name=alias_name, command=alias.command
@@ -388,14 +259,11 @@ class Alias(commands.Cog):
@commands.guild_only()
async def _del_alias(self, ctx: commands.Context, alias_name: str):
"""Delete an existing alias on this server."""
aliases = await self.unloaded_aliases(ctx.guild)
try:
next(aliases)
except StopIteration:
if not await self._aliases.get_guild_aliases(ctx.guild):
await ctx.send(_("There are no aliases on this server."))
return
if await self.delete_alias(ctx, alias_name):
if await self._aliases.delete_alias(ctx, alias_name):
await ctx.send(
_("Alias with the name `{name}` was successfully deleted.").format(name=alias_name)
)
@@ -406,14 +274,11 @@ class Alias(commands.Cog):
@global_.command(name="delete", aliases=["del", "remove"])
async def _del_global_alias(self, ctx: commands.Context, alias_name: str):
"""Delete an existing global alias."""
aliases = await self.unloaded_global_aliases()
try:
next(aliases)
except StopIteration:
await ctx.send(_("There are no aliases on this bot."))
if not await self._aliases.get_global_aliases():
await ctx.send(_("There are no global aliases on this bot."))
return
if await self.delete_alias(ctx, alias_name, global_=True):
if await self._aliases.delete_alias(ctx, alias_name, global_=True):
await ctx.send(
_("Alias with the name `{name}` was successfully deleted.").format(name=alias_name)
)
@@ -424,32 +289,34 @@ class Alias(commands.Cog):
@commands.guild_only()
async def _list_alias(self, ctx: commands.Context):
"""List the available aliases on this server."""
names = [_("Aliases:")] + sorted(
["+ " + a.name for a in (await self.unloaded_aliases(ctx.guild))]
)
if len(names) == 0:
await ctx.send(_("There are no aliases on this server."))
else:
await ctx.send(box("\n".join(names), "diff"))
guild_aliases = await self._aliases.get_guild_aliases(ctx.guild)
if not guild_aliases:
return await ctx.send(_("There are no aliases on this server."))
names = [_("Aliases:")] + sorted(["+ " + a.name for a in guild_aliases])
await ctx.send(box("\n".join(names), "diff"))
@global_.command(name="list")
async def _list_global_alias(self, ctx: commands.Context):
"""List the available global aliases on this bot."""
names = [_("Aliases:")] + sorted(
["+ " + a.name for a in await self.unloaded_global_aliases()]
)
if len(names) == 0:
await ctx.send(_("There are no aliases on this server."))
else:
await ctx.send(box("\n".join(names), "diff"))
global_aliases = await self._aliases.get_global_aliases()
if not global_aliases:
return await ctx.send(_("There are no global aliases."))
names = [_("Aliases:")] + sorted(["+ " + a.name for a in global_aliases])
await ctx.send(box("\n".join(names), "diff"))
@commands.Cog.listener()
async def on_message(self, message: discord.Message):
aliases = list(await self.unloaded_global_aliases())
if message.guild is not None:
aliases = aliases + list(await self.unloaded_aliases(message.guild))
if len(aliases) == 0:
async def on_message_without_command(self, message: discord.Message):
try:
prefix = await self.get_prefix(message)
except ValueError:
return
await self.maybe_call_alias(message, aliases=aliases)
try:
potential_alias = message.content[len(prefix) :].split(" ")[0]
except IndexError:
return
alias = await self._aliases.get_alias(message.guild, potential_alias)
if alias:
await self.call_alias(message, prefix, alias)