From 8965238a986383d428f32851e6940a550be8a159 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 21 Oct 2019 13:32:35 +0200 Subject: [PATCH 1/3] Start work on roll and dice commands --- requirements.txt | 3 ++- royalnet/packs/rpg/__init__.py | 7 +++++++ royalnet/packs/rpg/commands/__init__.py | 10 ++++++++++ royalnet/packs/rpg/commands/dice.py | 15 ++++++++++++++ royalnet/packs/rpg/commands/roll.py | 26 +++++++++++++++++++++++++ royalnet/packs/rpg/tables/__init__.py | 10 ++++++++++ 6 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 royalnet/packs/rpg/__init__.py create mode 100644 royalnet/packs/rpg/commands/__init__.py create mode 100644 royalnet/packs/rpg/commands/dice.py create mode 100644 royalnet/packs/rpg/commands/roll.py create mode 100644 royalnet/packs/rpg/tables/__init__.py diff --git a/requirements.txt b/requirements.txt index 970cc74d..db842be4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,11 +3,12 @@ async-timeout==3.0.1 attrs==19.3.0 bcrypt==3.1.7 certifi==2019.9.11 -cffi==1.13.0 +cffi==1.13.01 chardet==3.0.4 Click==7.0 cryptography==2.8 dateparser==0.7.2 +dice==2.4.2 discord.py==1.2.4 dnspython==1.15.0 dnspython3==1.15.0 diff --git a/royalnet/packs/rpg/__init__.py b/royalnet/packs/rpg/__init__.py new file mode 100644 index 00000000..4d839b28 --- /dev/null +++ b/royalnet/packs/rpg/__init__.py @@ -0,0 +1,7 @@ +# This is a template Pack __init__. You can use this without changing anything in other packages too! + +from .commands import available_commands +from .tables import available_tables + +__all__ = ["commands", "tables", "available_commands", "available_tables"] + diff --git a/royalnet/packs/rpg/commands/__init__.py b/royalnet/packs/rpg/commands/__init__.py new file mode 100644 index 00000000..3e6d10ca --- /dev/null +++ b/royalnet/packs/rpg/commands/__init__.py @@ -0,0 +1,10 @@ +# Imports go here! + + +# Enter the commands of your Pack here! +available_commands = [ + +] + +# Don't change this, it should automatically generate __all__ +__all__ = [command.__class__.__qualname__ for command in available_commands] diff --git a/royalnet/packs/rpg/commands/dice.py b/royalnet/packs/rpg/commands/dice.py new file mode 100644 index 00000000..5981c73f --- /dev/null +++ b/royalnet/packs/rpg/commands/dice.py @@ -0,0 +1,15 @@ +import typing +import random +import dice +from royalnet.commands import * + + +class DiceCommand(Command): + name: str = "dice" + + description: str = "Roll a dice, using 'dice'." + + aliases = ["d"] + + async def run(self, args: CommandArgs, data: CommandData) -> None: + ... diff --git a/royalnet/packs/rpg/commands/roll.py b/royalnet/packs/rpg/commands/roll.py new file mode 100644 index 00000000..1c140cd5 --- /dev/null +++ b/royalnet/packs/rpg/commands/roll.py @@ -0,0 +1,26 @@ +import typing +import random +from royalnet.commands import * + + +class RollCommand(Command): + name: str = "roll" + + description: str = "Roll a dice, from N to M (defaults to 1-100)." + + aliases = ["r", "random"] + + async def run(self, args: CommandArgs, data: CommandData) -> None: + first: typing.Optional[str] = args.optional(0) + second: typing.Optional[str] = args.optional(1) + if second: + minimum = int(first) + maximum = int(second) + elif first: + minimum = 1 + maximum = int(first) + else: + minimum = 1 + maximum = 100 + result = random.randrange(minimum, maximum+1) + await data.reply(f"🎲 Dice roll [{minimum}-{maximum}]: {result}") diff --git a/royalnet/packs/rpg/tables/__init__.py b/royalnet/packs/rpg/tables/__init__.py new file mode 100644 index 00000000..23bc7c24 --- /dev/null +++ b/royalnet/packs/rpg/tables/__init__.py @@ -0,0 +1,10 @@ +# Imports go here! + + +# Enter the tables of your Pack here! +available_tables = [ + +] + +# Don't change this, it should automatically generate __all__ +__all__ = [table.__class__.__qualname__ for table in available_tables] From b9a5a918735814f05cc0fb8126ebe5abaf745704 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 21 Oct 2019 18:32:13 +0200 Subject: [PATCH 2/3] Complete dice command --- royalnet/bots/generic.py | 2 ++ royalnet/packs/royal/tables/__init__.py | 6 ---- royalnet/packs/royal/tables/activekvgroups.py | 29 ----------------- royalnet/packs/royal/tables/keygroups.py | 15 --------- royalnet/packs/royal/tables/keyvalues.py | 31 ------------------- royalnet/packs/rpg/commands/__init__.py | 6 ++-- royalnet/packs/rpg/commands/dice.py | 22 +++++++++++-- royalnet/packs/rpg/commands/roll.py | 4 ++- 8 files changed, 28 insertions(+), 87 deletions(-) delete mode 100644 royalnet/packs/royal/tables/activekvgroups.py delete mode 100644 royalnet/packs/royal/tables/keygroups.py delete mode 100644 royalnet/packs/royal/tables/keyvalues.py diff --git a/royalnet/bots/generic.py b/royalnet/bots/generic.py index 71c40989..ca05a54f 100644 --- a/royalnet/bots/generic.py +++ b/royalnet/bots/generic.py @@ -74,6 +74,8 @@ class GenericBot: request: rh.Request = rh.Request(handler=event_name, data=args) response: rh.Response = await self.network.request(destination=destination, request=request) if isinstance(response, rh.ResponseFailure): + if response.extra_info["type"] == "CommandError": + raise CommandError(response.extra_info["message"]) raise CommandError(f"Herald action call failed:\n" f"[p]{response}[/p]") elif isinstance(response, rh.ResponseSuccess): diff --git a/royalnet/packs/royal/tables/__init__.py b/royalnet/packs/royal/tables/__init__.py index f20c16fe..d0201a73 100644 --- a/royalnet/packs/royal/tables/__init__.py +++ b/royalnet/packs/royal/tables/__init__.py @@ -5,9 +5,6 @@ from royalnet.packs.common.tables import Discord from .diario import Diario from .aliases import Alias -from .activekvgroups import ActiveKvGroup -from .keyvalues import Keyvalue -from .keygroups import Keygroup from .wikipages import WikiPage from .wikirevisions import WikiRevision from .bios import Bio @@ -24,9 +21,6 @@ available_tables = [ Discord, Diario, Alias, - ActiveKvGroup, - Keyvalue, - Keygroup, WikiPage, WikiRevision, Bio, diff --git a/royalnet/packs/royal/tables/activekvgroups.py b/royalnet/packs/royal/tables/activekvgroups.py deleted file mode 100644 index a4f72cff..00000000 --- a/royalnet/packs/royal/tables/activekvgroups.py +++ /dev/null @@ -1,29 +0,0 @@ -from sqlalchemy import Column, \ - String, \ - Integer, \ - ForeignKey -from sqlalchemy.orm import relationship -from sqlalchemy.ext.declarative import declared_attr - - -class ActiveKvGroup: - __tablename__ = "activekvgroups" - - @declared_attr - def royal_id(self): - return Column(Integer, ForeignKey("users.uid"), primary_key=True) - - @declared_attr - def group_name(self): - return Column(String, ForeignKey("keygroups.group_name"), nullable=False) - - @declared_attr - def royal(self): - return relationship("User", backref="active_kv_group") - - @declared_attr - def group(self): - return relationship("Keygroup", backref="users_with_this_active") - - def __repr__(self): - return f"" diff --git a/royalnet/packs/royal/tables/keygroups.py b/royalnet/packs/royal/tables/keygroups.py deleted file mode 100644 index a1dbf9c8..00000000 --- a/royalnet/packs/royal/tables/keygroups.py +++ /dev/null @@ -1,15 +0,0 @@ -from sqlalchemy import Column, \ - String, \ - ForeignKey -from sqlalchemy.ext.declarative import declared_attr - - -class Keygroup: - __tablename__ = "keygroups" - - @declared_attr - def group_name(self): - return Column(String, ForeignKey("keygroups.group_name"), primary_key=True) - - def __repr__(self): - return f"" diff --git a/royalnet/packs/royal/tables/keyvalues.py b/royalnet/packs/royal/tables/keyvalues.py deleted file mode 100644 index 6d3565d8..00000000 --- a/royalnet/packs/royal/tables/keyvalues.py +++ /dev/null @@ -1,31 +0,0 @@ -from sqlalchemy import Column, \ - String, \ - ForeignKey -from sqlalchemy.orm import relationship -from sqlalchemy.ext.declarative import declared_attr - - -class Keyvalue: - __tablename__ = "keyvalues" - - @declared_attr - def group_name(self): - return Column(String, ForeignKey("keygroups.group_name"), primary_key=True) - - @declared_attr - def key(self): - return Column(String, primary_key=True) - - @declared_attr - def value(self): - return Column(String, nullable=False) - - @declared_attr - def group(self): - return relationship("Keygroup") - - def __repr__(self): - return f"" - - def __str__(self): - return f"{self.key}: [b]{self.value}[/b]" diff --git a/royalnet/packs/rpg/commands/__init__.py b/royalnet/packs/rpg/commands/__init__.py index 3e6d10ca..b58f35ab 100644 --- a/royalnet/packs/rpg/commands/__init__.py +++ b/royalnet/packs/rpg/commands/__init__.py @@ -1,9 +1,11 @@ # Imports go here! - +from .roll import RollCommand +from .dice import DiceCommand # Enter the commands of your Pack here! available_commands = [ - + RollCommand, + DiceCommand, ] # Don't change this, it should automatically generate __all__ diff --git a/royalnet/packs/rpg/commands/dice.py b/royalnet/packs/rpg/commands/dice.py index 5981c73f..80483154 100644 --- a/royalnet/packs/rpg/commands/dice.py +++ b/royalnet/packs/rpg/commands/dice.py @@ -1,5 +1,3 @@ -import typing -import random import dice from royalnet.commands import * @@ -12,4 +10,22 @@ class DiceCommand(Command): aliases = ["d"] async def run(self, args: CommandArgs, data: CommandData) -> None: - ... + dice_str = args.joined(require_at_least=1) + roll = dice.roll(dice_str) + try: + result = list(roll) + except TypeError: + result = [roll] + message = f"🎲 {dice_str}" + total = 0 + if len(result) > 1: + message += f" = " + for index, die in enumerate(result): + message += f"{die}" + total += int(die) + if (index + 1) < len(result): + message += "+" + else: + total += int(result[0]) + message += f" = [b]{total}[/b]" + await data.reply(message) diff --git a/royalnet/packs/rpg/commands/roll.py b/royalnet/packs/rpg/commands/roll.py index 1c140cd5..bb69dd8f 100644 --- a/royalnet/packs/rpg/commands/roll.py +++ b/royalnet/packs/rpg/commands/roll.py @@ -8,6 +8,8 @@ class RollCommand(Command): description: str = "Roll a dice, from N to M (defaults to 1-100)." + syntax = "[min] [max]" + aliases = ["r", "random"] async def run(self, args: CommandArgs, data: CommandData) -> None: @@ -23,4 +25,4 @@ class RollCommand(Command): minimum = 1 maximum = 100 result = random.randrange(minimum, maximum+1) - await data.reply(f"🎲 Dice roll [{minimum}-{maximum}]: {result}") + await data.reply(f"🎲 Dice roll [{minimum}-{maximum}]: [b]{result}[/b]") From 3c84a9bb3c0bc3b1ae01f1845cb09071c682adae Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 21 Oct 2019 18:52:32 +0200 Subject: [PATCH 3/3] Bump version --- royalnet/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/royalnet/version.py b/royalnet/version.py index 9a00a37b..23334d27 100644 --- a/royalnet/version.py +++ b/royalnet/version.py @@ -1 +1 @@ -semantic = "5.0a69" +semantic = "5.0a70"