From 8e6601c2c59b9e86bf44ff4b2453e7e191891447 Mon Sep 17 00:00:00 2001 From: iDutchy Date: Tue, 13 Jul 2021 19:28:52 -0500 Subject: [PATCH] first attempt to shortcuts --- discord/__init__.py | 2 +- discord/ext/commands/bot.py | 31 ++++++++++++++++++++++++------- discord/ext/commands/context.py | 10 +++++++--- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/discord/__init__.py b/discord/__init__.py index f120329d..8832aa7d 100644 --- a/discord/__init__.py +++ b/discord/__init__.py @@ -13,7 +13,7 @@ __title__ = 'discord' __author__ = 'Rapptz' __license__ = 'MIT' __copyright__ = 'Copyright 2015-present Rapptz' -__version__ = '1.7.3.7' +__version__ = '1.7.3.7.post1' __path__ = __import__('pkgutil').extend_path(__path__, __name__) diff --git a/discord/ext/commands/bot.py b/discord/ext/commands/bot.py index 4f3850bd..39f67a54 100644 --- a/discord/ext/commands/bot.py +++ b/discord/ext/commands/bot.py @@ -24,8 +24,8 @@ DEALINGS IN THE SOFTWARE. import asyncio import collections -import inspect import importlib.util +import inspect import itertools import sys import traceback @@ -33,12 +33,12 @@ import types import discord -from .core import GroupMixin -from .view import StringView -from .context import Context from . import errors -from .help import HelpCommand, DefaultHelpCommand from .cog import Cog +from .context import Context +from .core import GroupMixin +from .help import DefaultHelpCommand, HelpCommand +from .view import StringView __all__ = ( 'when_mentioned', @@ -107,6 +107,7 @@ class BotBase(GroupMixin): self.extra_events = {} self.__cogs = {} self.__extensions = {} + self.__shortcuts = {} self._checks = [] self._check_once = [] self._before_invoke = None @@ -132,7 +133,23 @@ class BotBase(GroupMixin): self.help_command = DefaultHelpCommand() else: self.help_command = help_command - + + def add_guild_shortcut(self, name, config_dict): + """Add a shortcut attribute to context.guild + + Parameters + ----------- + name: :class:`str` + The name of the shortcut you want to add to context.guild + config_dict: :class:`dict` + The dict of {guild.id: other_data} where context.guild. will get the data from + """ + if not isinstance(name, str): + raise ValueError("Name must be a string") + if not isinstance(config_dict, dict): + raise ValueError("config_dict must be a dict") + self.__shortcuts[name] = config_dict + @property def owner(self): """:class:`discord.User`: The owner, retrieved from owner_id. In case of improper caching, this can return None @@ -155,7 +172,7 @@ class BotBase(GroupMixin): if owner: owners.append(owner) return owners - + # internal helpers def dispatch(self, event_name, *args, **kwargs): diff --git a/discord/ext/commands/context.py b/discord/ext/commands/context.py index 761669a6..4c3e7afe 100644 --- a/discord/ext/commands/context.py +++ b/discord/ext/commands/context.py @@ -21,7 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import re +import re import discord.abc import discord.utils @@ -99,7 +99,7 @@ class Context(discord.abc.Messageable): @property def clean_prefix(self): """:class:`str`: The cleaned up invoke prefix. i.e. mentions are ``@name`` instead of ``<@id>``. - + .. versionadded:: 1.5.1.4""" user = self.guild.me if self.guild else self.bot.user pattern = re.compile(r"<@!?%s>" % user.id) @@ -227,7 +227,11 @@ class Context(discord.abc.Messageable): @discord.utils.cached_property def guild(self): """Optional[:class:`.Guild`]: Returns the guild associated with this context's command. None if not available.""" - return self.message.guild + guild = self.message.guild + if self.bot.__shortcuts: + for name, config in self.bot.__shortcuts.keys(): + setattr(guild, name, config.get(guild.id)) + return guild @discord.utils.cached_property def channel(self):