-
Notifications
You must be signed in to change notification settings - Fork 0
/
slashcommands.py
executable file
·50 lines (43 loc) · 1.64 KB
/
slashcommands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from dice import DiceParser, DiceRoller
from models.sessionmodels import *
# Creates SlashCommandHandlers
class SlashCommandHandlerFactory:
@staticmethod
def create(command, session, user):
if command == "/roll":
return SlashRollCommandHandler()
elif command == "/nick":
return SlashNickCommandHandler(session, user)
else:
return UnknownSlashCommandHandler()
# Handles slash commands
class SlashCommandHandler:
def handle(self, command, args):
abstract
class SlashNickCommandHandler(SlashCommandHandler):
def __init__(self, session, user):
self.session = session
self.user = user
def handle(self, command, args):
for player in self.session.players:
if player.user == self.user:
old_nickname = player.nickname
player.nickname = args
player.put()
return old_nickname + " changed name to " + player.nickname
return "unknown player"
# Handles a "/roll 1d20" styled command
class SlashRollCommandHandler(SlashCommandHandler):
parser = DiceParser()
roller = DiceRoller()
def handle(self, command, args):
dc, sc = SlashRollCommandHandler.parser.parse(args)
total, individuals = SlashRollCommandHandler.roller.roll(dc, sc)
if dc > 1:
return "Roll %s: %s (%i)" % (args, str(individuals), total)
else:
return "Roll %s: %i" % (args, total)
# Handles an unknown command
class UnknownSlashCommandHandler(SlashCommandHandler):
def handle(self, command, args):
return "Unknown slash command: %s" % command