-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTmChat.py
107 lines (94 loc) · 3.13 KB
/
TmChat.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from PluginInterface import *
import os
import re
"""
\file TmChat.pd
\brief Contains the chat manager plugin
"""
class TmChat(PluginInterface):
"""
\brief The chat manager plugin
This plugin handles chat commands
"""
def __init__(self, pipes, args):
"""
\brief Construct the plugin
\param pipes The communication pipes to the pluginManager
\param args Additional startup arguments
"""
self.commands = {}
super(TmChat, self).__init__(pipes)
def initialize(self, args):
"""
\brief Initialize the plugin
\param args Startup arguments (should be emtpy)
"""
self.callMethod((None , 'subscribeEvent'), 'TmConnector', 'PlayerChat', 'PlayerChat')
self.registerChatCommand('help', ('TmChat', 'chat_help'), 'List all available chat commands.')
def PlayerChat(self, Uid, login, text, isRegisteredCmd):
"""
\brief Callback on player chat
\param Uid The player Uid
\param login The login of the player
\param text The text the player wrote
\param isRegisteredCmd Is this a registered command (leading /)
"""
text = text.strip()
if len(text) > 0:
if text[0] == '/':
self.processCommand(login, text[1:])
def processCommand(self, login, text):
"""
\brief Finds the callback for the command
\param login The login of the calling player
\param text The text of the command
"""
parts = text.split(' ', 1)
if parts[0] in self.commands:
cb = self.commands[parts[0]][0]
if len(parts) > 1:
arg = parts[1]
else:
arg = None
self.callMethod(cb, login, arg)
return True
else:
self.log('Error: Chat command ' + parts[0] + ' not known')
self.callMethod(('TmConnector', 'ChatSendServerMessageToLogin'), 'Error: Chat command ' + parts[0] + ' unnknown', login)
return False
def registerChatCommand(self, name, callback, description = 'No description'):
"""
\brief Register a command callback
\param name The name of the chatcommand
\param callback The callback
\param description A short description of the chat command
"""
if name in self.commands:
callback = self.commands[name]
self.callMethod(('Logger', 'log'), 'Plugin Chat: Could not register chat-command"' + name \
+ '" as it already is attached to ' + callback[0] + '.' + callback[1] + '"', 1)
else:
self.commands[name] = (callback, description)
self.callMethod(('Logger', 'log'), 'Plugin Chat: Registered chat-command "' \
+ name + '"', 0)
def unregisterChatCommand(self, name):
"""
\brief Unregister a registered chat command
\param name THe name of the command to unregister
"""
if name in self.commands:
del self.commands[name]
def chat_help(self, login, args):
"""
\brief Display a help text in the chat
\param login The player who needs help
\param args Additional args for help
"""
try:
args = args.strip()
except AttributeError:
pass
if not args in self.commands:
self.callMethod(('TmConnector', 'ChatSendServerMessageToLogin'), 'Currently supported chat-commands: ' + ', '.join(self.commands.keys()), login)
else:
self.callMethod(('TmConnector', 'ChatSendServerMessageToLogin'), 'Chat-command "' + str(args) + '": ' + self.commands[args][1], login)