diff options
author | 2020-09-20 12:13:30 +0530 | |
---|---|---|
committer | 2020-09-20 12:13:30 +0530 | |
commit | 06e6c0d10bd3ff8ac610e291b8a9416b4ddfedcd (patch) | |
tree | fa5c515bb510b25df987fad803562b95062f3235 /bot | |
parent | added static method and added genrator expression and list comprehension (diff) |
removed while loop and added for loop
Diffstat (limited to 'bot')
-rw-r--r-- | bot/exts/evergreen/wikipedia.py | 55 |
1 files changed, 30 insertions, 25 deletions
diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 8cee46c4..a4284aaa 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -72,36 +72,41 @@ class WikipediaCog(commands.Cog): total_chances = Wikipedia.total_chance titles_len = len(titles_no_underscore) # getting length of list - while chances <= total_chances: - chances += 1 - if chances < total_chances: - error_msg = f'You have `{total_chances - chances}/{total_chances}` chances left' - else: - error_msg = 'Please try again by using `.wiki` command' - try: - message = await ctx.bot.wait_for('message', timeout=60.0, check=check) - response_from_user = await self.bot.get_context(message) - if response_from_user.command: - return - response = int(message.content) - if response < 0: - await ctx.send(f"Sorry, but you can't give negative index, {error_msg}") - elif response == 0: - await ctx.send(f"Sorry, please give an integer between `1` to `{titles_len}`, {error_msg}") + for _ in range(Wikipedia.total_chance): + if chances <= total_chances: + chances += 1 + if chances < total_chances: + error_msg = f'You have `{total_chances - chances}/{total_chances}` chances left' else: - await ctx.send(WIKIPEDIA_URL.format(title=titles_no_underscore[response - 1])) + error_msg = 'Please try again by using `.wiki` command' + try: + message = await ctx.bot.wait_for('message', timeout=60.0, check=check) + response_from_user = await self.bot.get_context(message) + if response_from_user.command: + return + response = int(message.content) + if response < 0: + await ctx.send(f"Sorry, but you can't give negative index, {error_msg}") + elif response == 0: + await ctx.send(f"Sorry, please give an integer between `1` to `{titles_len}`, {error_msg}") + else: + await ctx.send(WIKIPEDIA_URL.format(title=titles_no_underscore[response - 1])) + break + + except asyncio.TimeoutError: + embed = Embed(colour=Color.red(), description=f"Time's up {ctx.author.mention}") + await msg.edit(embed=embed) break - except asyncio.TimeoutError: - embed = Embed(colour=Color.red(), description=f"Time's up {ctx.author.mention}") - await msg.edit(embed=embed) - break + except ValueError: + await ctx.send(f"Sorry, but you cannot do that, I will only accept an integer, {error_msg}") - except ValueError: - await ctx.send(f"Sorry, but you cannot do that, I will only accept an integer, {error_msg}") + except IndexError: + await ctx.send(f"Sorry, please give an integer between `1` to `{titles_len}`, {error_msg}") - except IndexError: - await ctx.send(f"Sorry, please give an integer between `1` to `{titles_len}`, {error_msg}") + except Exception as e: + log.info(e) + break def setup(bot: commands.Bot) -> None: |