106 |
115 |
107 if not ModuleCore.is_available (cmd, ident, host): |
116 if not ModuleCore.is_available (cmd, ident, host): |
108 command_error ('you may not use %s' % cmd['name']) |
117 command_error ('you may not use %s' % cmd['name']) |
109 |
118 |
110 bot.privmsg (replyto, '%s %s: %s' % (cmd['name'], cmd['args'], cmd['description'])) |
119 bot.privmsg (replyto, '%s %s: %s' % (cmd['name'], cmd['args'], cmd['description'])) |
|
120 |
|
121 def cmd_calc (bot, replyto, args, **rest): |
|
122 expr = args['expression'] |
|
123 |
|
124 try: |
|
125 # I want pi around so yeah |
|
126 pirex = re.compile (r'^(.*)\bpi\b(.*)$') |
|
127 match = pirex.match (expr) |
|
128 |
|
129 while match: |
|
130 expr = match.group(1) + '3.14159265358979323846264338327950288' + match.group(2) |
|
131 match = pirex.match (expr) |
|
132 |
|
133 result = subprocess.check_output (['calc', expr], stderr=subprocess.STDOUT) \ |
|
134 .replace ('\t', '') \ |
|
135 .replace ('\n', '') |
|
136 |
|
137 errmatch = re.compile (r'^.*\bError\b.*$').match (result) |
|
138 |
|
139 if errmatch: |
|
140 command_error ('math error') |
|
141 return |
|
142 |
|
143 bot.privmsg (replyto, 'Result: %s' % result) |
|
144 except subprocess.CalledProcessError as e: |
|
145 command_error (e.output.split('\n')[0]) |
|
146 except OSError as e: |
|
147 command_error ('failed to execute calc: ' + e.strerror) |