diff options
author | HombreLaser <sebastian-440@live.com> | 2022-06-15 15:31:08 -0500 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2022-06-15 15:31:08 -0500 |
commit | a9ca4aec1234d0c21b0a47a8181645f92226738e (patch) | |
tree | 17627d2b79eeac64e5771ac25b880d43b88120d0 /src | |
parent | caee74a9de80c94ee959909871961dab768a7430 (diff) |
Diffstat (limited to 'src')
-rw-r--r-- | src/command_parser.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/command_parser.py b/src/command_parser.py new file mode 100644 index 0000000..094e367 --- /dev/null +++ b/src/command_parser.py @@ -0,0 +1,48 @@ +from enum import Enum + +class Commands(Enum): + PLAY = 0 + ADD_TO_PLAYLIST = 1 + CLEAR = 2 + SEARCH_SONG = 3 + SEARCH_ALBUM = 4 + + +class CommandException(Exception): + """ + The user has introduced an invalid command. + """ + pass + + +def parse_command(message): + """ + Parse a mumble message into a possible command. + If it isn't a command (i.e. a plain message intended for + another user) we'll return None. If it is a message (starts with !) + but it is an invalid one, we wil throw an exception. + """ + current = 0 + + if message[current] == '!': + current += 1 + + if message[current:5].tolower() == 'play': + current += 5 + return (Commands.PLAY, message[current:]) + elif message[current:4].tolower() == 'add': + current +=4 + return (Commands.ADD_TO_PLAYLIST, message[current:]) + elif message[current:6].tolower() == 'clear': + current += 6 + return (Commands.CLEAR, message[current:]) + elif message[current:12].tolower() == 'search_song': + current += 13 + return (Commands.SEARCH_SONG, message[current:]) + elif message[current:13].tolower() == 'search_album': + current += 14 + return (Commands.SEARCH_ALBUM, message[current:]) + else: + raise CommandException('Invalid command.') + + return None |