# Exploring the Dicebot Core Code

# Exploring the Dicebot Core Code#

## Dicebot code explained - non-Lambda portion#

To get the dice rolling, we've provided the core "dicebot" library code below. We'll talk through some of the details, but to get oriented, here's the full listing:

1

`# dicebot.py`

2

`# !/usr/bin/env python3`

3

`"""Library for the dice-rolling chat bot used in chapter1 of`

4

Fullstack Serverless - Python on AWS Lambda

5

6

Logic for interpreting text as a command to roll one or more dice.

7

Includes functionality to format roll output for slack.

8

9

Available functions:

10

- parse_text: Determine if a String value is a valid dice roll command

11

- roll_dice: Simulate the rolling of one or more dice with an arbitrary number of sides

12

- format_slack_response: Turn the result of a roll_dice into a slack compatible output dict

13

`"""`

14

`# imports`

15

from random import randint

16

17

`# globals`

18

DEFAULT_NUM_DICE = 1 # Default to rolling 1 dice when not specified

19

DEFAULT_DICE_SIDES = 20 # Role-playing game enthusiasts unite!

20

DEFAULT_DIVIDER_STR = "d" # string to divide num_dice and dice_sides

21

22

23

def parse_text(text):

24

` """Take a roll definition string, checks if it's valid, and then returns`

25

a dict to be used as kwargs for the roll_dice function.

26

27

` Args:`

28

text: a String value defining a roll according to SRD standard (ie "2d10")

29

30

Returns: a dict with the output required for roll_dice function

31

num_dice: integer value for how many dice will be rolled

32

dice_sides: integer value for sides on each dice. represents maximum roll value.

33

ex: {"num_dice":1, "dice_sides":20}

34

` """`

35

num_dice = DEFAULT_NUM_DICE

36

num_sides = DEFAULT_DICE_SIDES

37

divider = DEFAULT_DIVIDER_STR.lower()

38

39

if text:

40

lower_text = str(text).lower() # ex: convert "D" to "d"

41

` # if a divider is present, attempt to parse the values before and after it`

42

if divider in lower_text:

43

` # returns partitiioned tuple: (num_dice,"d",num_sides)`

44

parted_text = lower_text.partition(divider)

45

` # if num_dice is defined, convert to int and use it`

46

if parted_text[0]:

47

num_dice = int(parted_text[0]) # throws ValueError

48

` # if num_sides is defined, convert to int and use it`

49

if parted_text[2]:

50

num_sides = int(parted_text[2]) # throws ValueError

51

` else:`

52

` # if no divider is present, as single int is accepted as num_dice`

53

num_dice = int(text) # throws ValueError

54

55

output = {"num_dice": num_dice, "dice_sides": num_sides}

56

return output

57

58

59

def roll_dice(num_dice=DEFAULT_NUM_DICE, dice_sides=DEFAULT_DICE_SIDES):

60

` """Creates a dict with random roll values`

61

62

` Args:`

63

num_dice: integer value for how many dice will be rolled

64

dice_sides: integer value for sides on each dice. represents maximum roll value.

65

66

Returns: dict with that contains the roll definition and results

67

keys and values in the output inlcude:

68

dice_roll: a string defining the dice roll (ie, "1d20")

69

results: a list of ints representing rolled values (ie, [20])

70

ex: {"dice_roll": "1d20", "results":[20]}

71

` """`

72

dice_results = [randint(1, dice_sides) for i in range(num_dice)]

73

dice_result_dict = {"dice_roll": "{}d{}".format(num_dice, dice_sides), "results": dice_results}

74

return dice_result_dict

75

76

77

def format_slack_response(dice_result):

78

` """Takes the output from the roll_dice function and creates a slack-formatted dict with output`

79

80

` Args:`

81

dice_result: a dict with the output from roll_dice.

82

expects keys and values for:

83

dice_roll: a