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/exts/evergreen/fun.py') 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