Last active
June 7, 2020 19:22
-
-
Save crazygmr101/08b23685cdb07fd1011498601ebaf5df to your computer and use it in GitHub Desktop.
a `input` like function for discord.py bots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ctx - required - a commands.Context of the commands | |
| # typ - required - the type you want to return | |
| # cancel_str - the string that a user will type to cancel | |
| # ch - a function/lambda that is checked for validity | |
| # err - error string | |
| # del_error - the time to wait before deleting the error message | |
| # del_response - delete the user's response | |
| # return_author - return (result, author) otherwise just return result | |
| # check_author - whether to wait for an author matching ctx.author | |
| async def _input(self, ctx, typ: type, cancel_str: str = "cancel", ch: Callable = None, err=None, check_author=True, | |
| return_author=False, del_error=60, del_response=False): | |
| def check(m): | |
| return ((m.author == ctx.author and m.channel == ctx.channel) or not check_author) and not m.author.bot | |
| while True: | |
| try: | |
| inp: discord.Message = await self.bot.wait_for('message', check=check, timeout=60.0) | |
| if del_response: | |
| await inp.delete() | |
| if inp.content.lower() == cancel_str.lower(): | |
| return (None, None) if return_author else None | |
| res = typ(inp.content.lower()) | |
| if ch: | |
| if not ch(res): raise ValueError | |
| return (res, inp.author) if return_author else res | |
| except ValueError: | |
| await ctx.send(err or "That's not a valid response, try again" + | |
| ("" if not cancel_str else f" or type `{cancel_str}` to quit"), delete_after=del_error) | |
| continue | |
| except asyncio.TimeoutError: | |
| await ctx.send("You took too long to respond ): Try to start over", delete_after=del_error) | |
| return (None, None) if return_author else None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @commands.command() | |
| async def demo(): | |
| # inputting an integer | |
| i: int = await _input(ctx, int) | |
| # inputting a float between 1.5 and 6.7 | |
| f: float = await _input(ctx, float, ch=lambda x: 1.5 <= x <= 6.7) | |
| # inputting a string with a custom error message, and delete the response. The string | |
| # must be "a" or "b" (upper or lower) | |
| choice: str = await _input(ctx, str, ch=lambda x: lower(x) in ["a","b"], | |
| err="Oops, you must input a or b", | |
| del_error=30, | |
| del_reponse=True) | |
| # get the author of the first person who responds | |
| ans: str, author: discord.Member = await _input(ctx, check_author=True, retrun_author=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment