From 7909c4d6c764b33705d1b9bebdbc38bed188bb5d Mon Sep 17 00:00:00 2001 From: Gustav Odinger Date: Sun, 4 Oct 2020 22:33:54 +0200 Subject: Add space between dice in roll command - Looks much better - Cleans up the code of the roll command --- bot/exts/evergreen/fun.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index de6a92c6..9d6ee29c 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -60,15 +60,13 @@ class Fun(Cog): @commands.command() async def roll(self, ctx: Context, num_rolls: int = 1) -> None: """Outputs a number of random dice emotes (up to 6).""" - output = "" if num_rolls > 6: num_rolls = 6 - elif num_rolls < 1: - output = ":no_entry: You must roll at least once." - for _ in range(num_rolls): - dice = f"dice_{random.randint(1, 6)}" - output += getattr(Emojis, dice, '') - await ctx.send(output) + + dice = (f"dice_{random.randint(1, 6)}" for _ in range(num_rolls)) + output = " ".join(getattr(Emojis, die, '') for die in dice) + + await ctx.send(output or ":no_entry: You must roll at least once.") @commands.command(name="uwu", aliases=("uwuwize", "uwuify",)) async def uwu_command(self, ctx: Context, *, text: clean_content(fix_channel_mentions=True)) -> None: -- cgit v1.2.3 From 6969fc1973261bcba51b7346c8e168bcc011b833 Mon Sep 17 00:00:00 2001 From: Gustav Odinger Date: Sun, 4 Oct 2020 23:30:47 +0200 Subject: Revert and update roll command - Returns to previous version of code - Improves readability - Adds spaced between dice --- bot/exts/evergreen/fun.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 9d6ee29c..23715906 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -60,13 +60,15 @@ class Fun(Cog): @commands.command() async def roll(self, ctx: Context, num_rolls: int = 1) -> None: """Outputs a number of random dice emotes (up to 6).""" + output = "" if num_rolls > 6: num_rolls = 6 - - dice = (f"dice_{random.randint(1, 6)}" for _ in range(num_rolls)) - output = " ".join(getattr(Emojis, die, '') for die in dice) - - await ctx.send(output or ":no_entry: You must roll at least once.") + elif num_rolls < 1: + output = ":no_entry: You must roll at least once." + for _ in range(num_rolls): + dice = f"dice_{random.randint(1, 6)}" + output += getattr(Emojis, dice, '') + " " + await ctx.send(output.rstrip()) @commands.command(name="uwu", aliases=("uwuwize", "uwuify",)) async def uwu_command(self, ctx: Context, *, text: clean_content(fix_channel_mentions=True)) -> None: -- cgit v1.2.3 From cfb9237d38b2e2961d1aa2f7dd41f81c7346fb57 Mon Sep 17 00:00:00 2001 From: Gustav Odinger Date: Mon, 5 Oct 2020 18:54:12 +0200 Subject: Rewrite roll command - Improves readability - Sends dice separated by single spaces --- bot/exts/evergreen/fun.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 23715906..09400d23 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -7,7 +7,7 @@ from typing import Callable, Iterable, Tuple, Union from discord import Embed, Message from discord.ext import commands -from discord.ext.commands import Bot, Cog, Context, MessageConverter, clean_content +from discord.ext.commands import BadArgument, Bot, Cog, Context, MessageConverter, clean_content from bot import utils from bot.constants import Colours, Emojis @@ -60,15 +60,17 @@ class Fun(Cog): @commands.command() async def roll(self, ctx: Context, num_rolls: int = 1) -> None: """Outputs a number of random dice emotes (up to 6).""" - output = "" - if num_rolls > 6: - num_rolls = 6 - elif num_rolls < 1: - output = ":no_entry: You must roll at least once." - for _ in range(num_rolls): - dice = f"dice_{random.randint(1, 6)}" - output += getattr(Emojis, dice, '') + " " - await ctx.send(output.rstrip()) + def _get_random_die() -> str: + """Generate a random die emoji, ready to be sent on Discord.""" + die_name = f"dice_{random.randint(1, 6)}" + return getattr(Emojis, die_name) + + # Only support between 1 and 6 rolls + if 1 <= num_rolls <= 6: + dice = " ".join(_get_random_die() for _ in range(num_rolls)) + await ctx.send(dice) + else: + raise BadArgument("`!roll` only supports between 1 and 6 rolls.") @commands.command(name="uwu", aliases=("uwuwize", "uwuify",)) async def uwu_command(self, ctx: Context, *, text: clean_content(fix_channel_mentions=True)) -> None: -- cgit v1.2.3 From ebf41d3edd012378bb3b150c355ab726310d1681 Mon Sep 17 00:00:00 2001 From: Gustav Odinger Date: Mon, 5 Oct 2020 19:22:34 +0200 Subject: Move _get_random_die to me a separate function - The function is only created once, instead of every time the roll command is run --- bot/exts/evergreen/fun.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 09400d23..49c5d69e 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -57,17 +57,17 @@ class Fun(Cog): with Path("bot/resources/evergreen/caesar_info.json").open("r", encoding="UTF-8") as f: self._caesar_cipher_embed = json.load(f) + @staticmethod + def _get_random_die() -> str: + """Generate a random die emoji, ready to be sent on Discord.""" + die_name = f"dice_{random.randint(1, 6)}" + return getattr(Emojis, die_name) + @commands.command() async def roll(self, ctx: Context, num_rolls: int = 1) -> None: """Outputs a number of random dice emotes (up to 6).""" - def _get_random_die() -> str: - """Generate a random die emoji, ready to be sent on Discord.""" - die_name = f"dice_{random.randint(1, 6)}" - return getattr(Emojis, die_name) - - # Only support between 1 and 6 rolls if 1 <= num_rolls <= 6: - dice = " ".join(_get_random_die() for _ in range(num_rolls)) + dice = " ".join(Fun._get_random_die() for _ in range(num_rolls)) await ctx.send(dice) else: raise BadArgument("`!roll` only supports between 1 and 6 rolls.") -- cgit v1.2.3 From 5d247d4ebdd81d3e937cf395ddcba162b8b4c621 Mon Sep 17 00:00:00 2001 From: gustavwilliam <65498475+gustavwilliam@users.noreply.github.com> Date: Wed, 7 Oct 2020 16:48:43 +0200 Subject: Change call of `_get_random_dice` call to use self MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Considered best practice Co-authored-by: Leon Sandøy --- bot/exts/evergreen/fun.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 49c5d69e..231e6d54 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -67,7 +67,7 @@ class Fun(Cog): async def roll(self, ctx: Context, num_rolls: int = 1) -> None: """Outputs a number of random dice emotes (up to 6).""" if 1 <= num_rolls <= 6: - dice = " ".join(Fun._get_random_die() for _ in range(num_rolls)) + dice = " ".join(self._get_random_die() for _ in range(num_rolls)) await ctx.send(dice) else: raise BadArgument("`!roll` only supports between 1 and 6 rolls.") -- cgit v1.2.3