diff --git a/discord/ext/commands/bot.py b/discord/ext/commands/bot.py index 093f4a5dd..faa2c1098 100644 --- a/discord/ext/commands/bot.py +++ b/discord/ext/commands/bot.py @@ -38,20 +38,6 @@ from .context import Context from .errors import CommandNotFound, CommandError from .formatter import HelpFormatter -def _get_variable(name): - stack = inspect.stack() - try: - for frames in stack: - try: - frame = frames[0] - current_locals = frame.f_locals - if name in current_locals: - return current_locals[name] - finally: - del frame - finally: - del stack - def when_mentioned(bot, msg): """A callable that implements a command prefix equivalent to being mentioned, e.g. ``@bot ``.""" @@ -305,169 +291,6 @@ class Bot(GroupMixin, discord.Client): print('Ignoring exception in command {}'.format(context.command), file=sys.stderr) traceback.print_exception(type(exception), exception, exception.__traceback__, file=sys.stderr) - # utility "send_*" functions - - @asyncio.coroutine - def _augmented_msg(self, coro, **kwargs): - msg = yield from coro - delete_after = kwargs.get('delete_after') - if delete_after is not None: - @asyncio.coroutine - def delete(): - yield from asyncio.sleep(delete_after, loop=self.loop) - yield from self.delete_message(msg) - - discord.compat.create_task(delete(), loop=self.loop) - - return msg - - def say(self, *args, **kwargs): - """|coro| - - A helper function that is equivalent to doing - - .. code-block:: python - - self.send_message(message.channel, *args, **kwargs) - - The following keyword arguments are "extensions" that augment the - behaviour of the standard wrapped call. - - Parameters - ------------ - delete_after: float - Number of seconds to wait before automatically deleting the - message. - - See Also - --------- - :meth:`Client.send_message` - """ - destination = _get_variable('_internal_channel') - - extensions = ('delete_after',) - params = { - k: kwargs.pop(k, None) for k in extensions - } - - coro = self.send_message(destination, *args, **kwargs) - return self._augmented_msg(coro, **params) - - def whisper(self, *args, **kwargs): - """|coro| - - A helper function that is equivalent to doing - - .. code-block:: python - - self.send_message(message.author, *args, **kwargs) - - The following keyword arguments are "extensions" that augment the - behaviour of the standard wrapped call. - - Parameters - ------------ - delete_after: float - Number of seconds to wait before automatically deleting the - message. - - See Also - --------- - :meth:`Client.send_message` - """ - destination = _get_variable('_internal_author') - - extensions = ('delete_after',) - params = { - k: kwargs.pop(k, None) for k in extensions - } - - coro = self.send_message(destination, *args, **kwargs) - return self._augmented_msg(coro, **params) - - def reply(self, content, *args, **kwargs): - """|coro| - - A helper function that is equivalent to doing - - .. code-block:: python - - msg = '{0.mention}, {1}'.format(message.author, content) - self.send_message(message.channel, msg, *args, **kwargs) - - The following keyword arguments are "extensions" that augment the - behaviour of the standard wrapped call. - - Parameters - ------------ - delete_after: float - Number of seconds to wait before automatically deleting the - message. - - See Also - --------- - :meth:`Client.send_message` - """ - author = _get_variable('_internal_author') - destination = _get_variable('_internal_channel') - fmt = '{0.mention}, {1}'.format(author, str(content)) - - extensions = ('delete_after',) - params = { - k: kwargs.pop(k, None) for k in extensions - } - - coro = self.send_message(destination, fmt, *args, **kwargs) - return self._augmented_msg(coro, **params) - - def upload(self, *args, **kwargs): - """|coro| - - A helper function that is equivalent to doing - - .. code-block:: python - - self.send_file(message.channel, *args, **kwargs) - - The following keyword arguments are "extensions" that augment the - behaviour of the standard wrapped call. - - Parameters - ------------ - delete_after: float - Number of seconds to wait before automatically deleting the - message. - - See Also - --------- - :meth:`Client.send_file` - """ - destination = _get_variable('_internal_channel') - - extensions = ('delete_after',) - params = { - k: kwargs.pop(k, None) for k in extensions - } - - coro = self.send_file(destination, *args, **kwargs) - return self._augmented_msg(coro, **params) - - def type(self): - """|coro| - - A helper function that is equivalent to doing - - .. code-block:: python - - self.send_typing(message.channel) - - See Also - --------- - The :meth:`Client.send_typing` function. - """ - destination = _get_variable('_internal_channel') - return self.send_typing(destination) - # global check registration def check(self, func): diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 13f476fee..13168e3d7 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -39,13 +39,10 @@ __all__ = [ 'Command', 'Group', 'GroupMixin', 'command', 'group', 'bot_has_role', 'bot_has_permissions', 'bot_has_any_role', 'cooldown' ] -def inject_context(ctx, coro): +def wrap_callback(coro): @functools.wraps(coro) @asyncio.coroutine def wrapped(*args, **kwargs): - _internal_channel = ctx.message.channel - _internal_author = ctx.message.author - try: ret = yield from coro(*args, **kwargs) except CommandError: @@ -155,7 +152,7 @@ class Command: pass else: loop = ctx.bot.loop - injected = inject_context(ctx, coro) + injected = wrap_callback(coro) if self.instance is not None: discord.compat.create_task(injected(self.instance, error, ctx), loop=loop) else: @@ -365,7 +362,7 @@ class Command: # since we're in a regular command (and not a group) then # the invoked subcommand is None. ctx.invoked_subcommand = None - injected = inject_context(ctx, self.callback) + injected = wrap_callback(self.callback) yield from injected(*ctx.args, **ctx.kwargs) def error(self, coro): @@ -598,7 +595,7 @@ class Group(GroupMixin, Command): ctx.invoked_subcommand = self.commands.get(trigger, None) if early_invoke: - injected = inject_context(ctx, self.callback) + injected = wrap_callback(self.callback) yield from injected(*ctx.args, **ctx.kwargs) if trigger and ctx.invoked_subcommand: