first attempt to shortcuts

This commit is contained in:
iDutchy 2021-07-13 19:28:52 -05:00
parent 2df88ccc27
commit 8e6601c2c5
3 changed files with 32 additions and 11 deletions

View File

@ -13,7 +13,7 @@ __title__ = 'discord'
__author__ = 'Rapptz' __author__ = 'Rapptz'
__license__ = 'MIT' __license__ = 'MIT'
__copyright__ = 'Copyright 2015-present Rapptz' __copyright__ = 'Copyright 2015-present Rapptz'
__version__ = '1.7.3.7' __version__ = '1.7.3.7.post1'
__path__ = __import__('pkgutil').extend_path(__path__, __name__) __path__ = __import__('pkgutil').extend_path(__path__, __name__)

View File

@ -24,8 +24,8 @@ DEALINGS IN THE SOFTWARE.
import asyncio import asyncio
import collections import collections
import inspect
import importlib.util import importlib.util
import inspect
import itertools import itertools
import sys import sys
import traceback import traceback
@ -33,12 +33,12 @@ import types
import discord import discord
from .core import GroupMixin
from .view import StringView
from .context import Context
from . import errors from . import errors
from .help import HelpCommand, DefaultHelpCommand
from .cog import Cog from .cog import Cog
from .context import Context
from .core import GroupMixin
from .help import DefaultHelpCommand, HelpCommand
from .view import StringView
__all__ = ( __all__ = (
'when_mentioned', 'when_mentioned',
@ -107,6 +107,7 @@ class BotBase(GroupMixin):
self.extra_events = {} self.extra_events = {}
self.__cogs = {} self.__cogs = {}
self.__extensions = {} self.__extensions = {}
self.__shortcuts = {}
self._checks = [] self._checks = []
self._check_once = [] self._check_once = []
self._before_invoke = None self._before_invoke = None
@ -132,7 +133,23 @@ class BotBase(GroupMixin):
self.help_command = DefaultHelpCommand() self.help_command = DefaultHelpCommand()
else: else:
self.help_command = help_command 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.<shortcut> 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 @property
def owner(self): def owner(self):
""":class:`discord.User`: The owner, retrieved from owner_id. In case of improper caching, this can return None """: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: if owner:
owners.append(owner) owners.append(owner)
return owners return owners
# internal helpers # internal helpers
def dispatch(self, event_name, *args, **kwargs): def dispatch(self, event_name, *args, **kwargs):

View File

@ -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 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. DEALINGS IN THE SOFTWARE.
""" """
import re import re
import discord.abc import discord.abc
import discord.utils import discord.utils
@ -99,7 +99,7 @@ class Context(discord.abc.Messageable):
@property @property
def clean_prefix(self): def clean_prefix(self):
""":class:`str`: The cleaned up invoke prefix. i.e. mentions are ``@name`` instead of ``<@id>``. """:class:`str`: The cleaned up invoke prefix. i.e. mentions are ``@name`` instead of ``<@id>``.
.. versionadded:: 1.5.1.4""" .. versionadded:: 1.5.1.4"""
user = self.guild.me if self.guild else self.bot.user user = self.guild.me if self.guild else self.bot.user
pattern = re.compile(r"<@!?%s>" % user.id) pattern = re.compile(r"<@!?%s>" % user.id)
@ -227,7 +227,11 @@ class Context(discord.abc.Messageable):
@discord.utils.cached_property @discord.utils.cached_property
def guild(self): def guild(self):
"""Optional[:class:`.Guild`]: Returns the guild associated with this context's command. None if not available.""" """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 @discord.utils.cached_property
def channel(self): def channel(self):