1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

Complete dice command

This commit is contained in:
Steffo 2019-10-21 18:32:13 +02:00
parent 8965238a98
commit b9a5a91873
8 changed files with 28 additions and 87 deletions

View file

@ -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):

View file

@ -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,

View file

@ -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"<ActiveKvGroup royal={self.royal} group={self.group_name}>"

View file

@ -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"<Keygroup {self.group_name}>"

View file

@ -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"<Keyvalue group={self.group_name} key={self.key} value={self.value}>"
def __str__(self):
return f"{self.key}: [b]{self.value}[/b]"

View file

@ -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__

View file

@ -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)

View file

@ -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]")