|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +The MIT License (MIT) |
| 5 | +
|
| 6 | +Copyright (c) 2015-2019 Rapptz |
| 7 | +
|
| 8 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 9 | +copy of this software and associated documentation files (the "Software"), |
| 10 | +to deal in the Software without restriction, including without limitation |
| 11 | +the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 12 | +and/or sell copies of the Software, and to permit persons to whom the |
| 13 | +Software is furnished to do so, subject to the following conditions: |
| 14 | +
|
| 15 | +The above copyright notice and this permission notice shall be included in |
| 16 | +all copies or substantial portions of the Software. |
| 17 | +
|
| 18 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 19 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 24 | +DEALINGS IN THE SOFTWARE. |
| 25 | +""" |
| 26 | + |
| 27 | +from .errors import MissingRequiredArgument |
| 28 | + |
| 29 | +__all__ = ( |
| 30 | + 'CustomDefault', |
| 31 | + 'Author', |
| 32 | + 'CurrentChannel', |
| 33 | + 'CurrentGuild', |
| 34 | + 'Call', |
| 35 | +) |
| 36 | + |
| 37 | +class CustomDefault: |
| 38 | + """The base class of custom defaults that require the :class:`.Context`. |
| 39 | +
|
| 40 | + Classes that derive from this should override the :meth:`~.CustomDefault.default` |
| 41 | + method to do its conversion logic. This method must be a coroutine. |
| 42 | + """ |
| 43 | + |
| 44 | + async def default(self, ctx, param): |
| 45 | + """|coro| |
| 46 | +
|
| 47 | + The method to override to do conversion logic. |
| 48 | +
|
| 49 | + If an error is found while converting, it is recommended to |
| 50 | + raise a :exc:`.CommandError` derived exception as it will |
| 51 | + properly propagate to the error handlers. |
| 52 | +
|
| 53 | + Parameters |
| 54 | + ----------- |
| 55 | + ctx: :class:`.Context` |
| 56 | + The invocation context that the argument is being used in. |
| 57 | + """ |
| 58 | + raise NotImplementedError('Derived classes need to implement this.') |
| 59 | + |
| 60 | + |
| 61 | +class Author(CustomDefault): |
| 62 | + """Default parameter which returns the author for this context.""" |
| 63 | + |
| 64 | + async def default(self, ctx, param): |
| 65 | + return ctx.author |
| 66 | + |
| 67 | +class CurrentChannel(CustomDefault): |
| 68 | + """Default parameter which returns the channel for this context.""" |
| 69 | + |
| 70 | + async def default(self, ctx, param): |
| 71 | + return ctx.channel |
| 72 | + |
| 73 | +class CurrentGuild(CustomDefault): |
| 74 | + """Default parameter which returns the guild for this context.""" |
| 75 | + |
| 76 | + async def default(self, ctx, param): |
| 77 | + if ctx.guild: |
| 78 | + return ctx.guild |
| 79 | + raise MissingRequiredArgument(param) |
| 80 | + |
| 81 | +class Call(CustomDefault): |
| 82 | + """Easy wrapper for lambdas/inline defaults.""" |
| 83 | + |
| 84 | + def __init__(self, callback): |
| 85 | + self._callback = callback |
| 86 | + |
| 87 | + async def default(self, ctx, param): |
| 88 | + return self._callback(ctx, param) |
0 commit comments