-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.py
90 lines (80 loc) · 5.1 KB
/
commands.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from exceptions import CommandNotFound, NotACommand
from handlers.roll import handler as roll_handler
from handlers.charsheet import handler as charsheet_handler
from handlers.character import handler as character_handler
from handlers.turns import handler as turn_handler
from handlers.dm import handler as dm_handler
from handlers.campaign import handler as campaign_handler
# Each command should be defined using the expression below:
#
# "/cmd": (handler, args, description)
#
# where:
# cmd: the command string
# handler: is the method that will handle the action requested with the command
# args: is an array of arguments the command need. Args wrapped between `<>` are mandatory and wrapped
# between `()` are optional. Can be None if no arguments are required.
# description: the text that will describe the command
GENERAL_COMMANDS = {
"/start": (None, None, "starts the DnDCompanionBot"),
"/roll": (roll_handler, ["<expression>"], "rolls the dice using the [dice notation](https://en.wikipedia.org/wiki/Dice_notation)"),
"/charsheet": (charsheet_handler, ["<username>"], "returns the character sheet associated with username"),
"/help": (None, None, "shows this help message")
}
CAMPAIGN_COMMANDS = {
"/start_campaign": (campaign_handler, None, "starts a new campaign in the invoked group"),
"/close_campaign": (campaign_handler, None, "closes an active campaign"),
"/set_turns": (turn_handler, ["<username1>", "...", "<usernameN>"], "creates a list with the order of players for a given round"),
"/turn": (turn_handler, None, "shows the current player in the turns list"),
"/next_turn": (turn_handler, None, "moves to the next player in the turns list"),
"/set_dm": (dm_handler, ["<username>"], "sets the username of the DM for the current campaign"),
"/dm": (dm_handler, None, "shows the DM for the current campaign"),
"/start_battle": (campaign_handler, ["<width>", "<height>"], "generates a new battle field"),
"/set_positions": (campaign_handler, ["<expression>"], "set characters positions at the battle field"),
"/map": (campaign_handler, None, "set characters positions at the battle field"),
}
CHARACTER_COMMANDS = {
"/import_char": (character_handler, ["<url>"], "imports the JSON data of a character from a URL"),
"/link_char": (character_handler, ["<char_id>", "(username)"], "links character to target username or self username"),
"/status": (character_handler, ["<username|character>"], "shows the list of weapons of a character"),
"/weapons": (character_handler, ["<username|character>"], "shows the list of weapons of a character"),
"/spells": (character_handler, ["<username|character>"], "shows the list of damage spells of a character"),
"/currency": (character_handler, ["<username|character>"], "shows the currency pouch of a character"),
"/damage": (character_handler, ["<username|character>", "<hp>"], "apply damage to a character"),
"/heal": (character_handler, ["<username|character>", "<hp>"], "apply heal to a character"),
"/attack_roll": (character_handler, ["<weapon|spell>", "<melee|range>", "(distance)", "(adv|disadv)"], "performs an attack roll on a character"),
"/initiative_roll": (character_handler, ["<character>"], "performs an initiative roll for a character"),
"/short_rest_roll": (character_handler, ["<username|character>"], "performs an short rest roll for a character"),
"/ability_check": (character_handler, ["<ability>", "(skill)"], "performs an ability check or a skill check if skill is specified"),
"/say": (character_handler, ["<character>", "<message>"], "prints a message using in-game conversation format"),
"/whisper": (character_handler, ["<character>", "<message>"], "prints a whisper message using in-game conversation format"),
"/yell": (character_handler, ["<character>", "<message>"], "prints a yell message using in-game conversation format"),
"/move": (character_handler, ["<character for dm>"], "moves your character on the battle field"),
"/set_currency": (character_handler, ["<username|character>", "<expression>"], "set the currency pouch of a character"),
"/add_xp": (dm_handler, ["<username|character>", "<xp>"], "adds points of experience to a character"),
}
ALL_COMMANDS = {}
ALL_COMMANDS.update(GENERAL_COMMANDS)
ALL_COMMANDS.update(CAMPAIGN_COMMANDS)
ALL_COMMANDS.update(CHARACTER_COMMANDS)
def command_handler(command):
if command == "/help" or command == '/start':
raise CommandNotFound
# return default_handler
#elif command == "/start":
elif command in ALL_COMMANDS:
return ALL_COMMANDS[command][0]
else:
raise CommandNotFound
def default_handler(bot, update, message):
bot.send_message(chat_id=update.message.chat_id, text=message, parse_mode="Markdown", disable_web_page_preview=True)
def is_command(update):
if update.message is None:
return False
return update.message.text != None and update.message.text != '' \
and update.message.text.startswith('/')
def parse_command(txt_message):
cmd = txt_message.split(' ')[0]
if cmd.find('@') >= 0:
cmd = cmd.split('@')[0]
return cmd