Thu, 15 Jan 2015 20:48:57 +0200
- meh
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
1 | import math |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
2 | import urllib2 |
76 | 3 | import json |
105 | 4 | import subprocess |
5 | import re | |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
6 | from modulecore import command_error |
102
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
7 | import modulecore as ModuleCore |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
8 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
9 | ModuleData = { |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
10 | 'commands': |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
11 | [ |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
12 | { |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
13 | 'name': 'convert', |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
14 | 'description': 'Performs numeric conversion', |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
15 | 'args': '<value> as <valuetype>', |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
16 | 'level': 'normal', |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
17 | }, |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
18 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
19 | { |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
20 | 'name': 'ud', |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
21 | 'description': 'Looks up a term in urban dictionary', |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | 'args': '<term...>', |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | 'level': 'normal', |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
24 | }, |
102
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
25 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
26 | { |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
27 | 'name': 'commands', |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
28 | 'description': 'Lists commands available to the calling user', |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
29 | 'args': None, |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
30 | 'level': 'normal', |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
31 | }, |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
32 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
33 | { |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
34 | 'name': 'help', |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
35 | 'description': 'Prints help about a given command', |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
36 | 'args': '<command>', |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
37 | 'level': 'normal', |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
38 | }, |
105 | 39 | |
40 | { | |
41 | 'name': 'calc', | |
42 | 'description': 'Calculates a mathematical expression using apcalc', | |
43 | 'args': '<expression...>', | |
44 | 'level': 'normal', | |
45 | }, | |
113
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
46 | |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
47 | { |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
48 | 'name': 'more', |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
49 | 'description': 'Prints subsequent command result pages', |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
50 | 'args': None, |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
51 | 'level': 'normal', |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
52 | }, |
117
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
53 | |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
54 | { |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
55 | 'name': 'yes', |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
56 | 'description': 'Confirms the previous command', |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
57 | 'args': None, |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
58 | 'level': 'normal', |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
59 | }, |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
60 | |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
61 | { |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
62 | 'name': 'no', |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
63 | 'description': 'Unconfirms the previous command', |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
64 | 'args': None, |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
65 | 'level': 'normal', |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
66 | }, |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
67 | ] |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
68 | } |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
69 | |
113
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
70 | def cmd_convert (bot, args, reply, **rest): |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
71 | try: |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
72 | value = float (args['value']) |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
73 | except Exception as e: |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
74 | command_error (str (e)) |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
75 | |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
76 | valuetype = args['valuetype'] |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
77 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
78 | if valuetype in ['radians', 'degrees']: |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
79 | if valuetype == 'radians': |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
80 | radvalue = value |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
81 | degvalue = (value * 180.) / math.pi |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
82 | else: |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
83 | radvalue = (value * math.pi) / 180. |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
84 | degvalue = value |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
85 | |
113
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
86 | reply ('%s radians, %s degrees (%s)' % (radvalue, degvalue, degvalue % 360.)) |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
87 | return |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
88 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
89 | if valuetype in ['celsius', 'fahrenheit']: |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
90 | if valuetype == 'celsius': |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
91 | celvalue = value |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
92 | fahrvalue = value * 1.8 + 32 |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
93 | else: |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
94 | celvalue = (value - 32) / 1.8 |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
95 | fahrvalue = value |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
96 | |
113
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
97 | reply ('%s degrees celsius, %s degrees fahrenheit' % (celvalue, fahrvalue)) |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
98 | return |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
99 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
100 | command_error ('unknown valuetype %s, expected one of: degrees, radians (angle conversion), ' + |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
101 | 'celsius, fahrenheit (temperature conversion)' % valuetype) |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
102 | |
113
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
103 | def cmd_ud (bot, args, reply, **rest): |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
104 | try: |
76 | 105 | url = 'http://api.urbandictionary.com/v0/define?term=%s' % (args['term'].replace (' ', '%20')) |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
106 | response = urllib2.urlopen (url).read() |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
107 | data = json.loads (response) |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
108 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
109 | if not 'list' in data \ |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
110 | or len(data['list']) == 0 \ |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
111 | or not 'word' in data['list'][0] \ |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
112 | or not 'definition' in data['list'][0]: |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
113 | command_error ("couldn't find a definition of \002%s\002" % args['term']) |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
114 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
115 | word = data['list'][0]['word'] |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
116 | definition = data['list'][0]['definition'].replace ('\r', ' ').replace ('\n', ' ').replace (' ', ' ') |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
117 | up = data['list'][0]['thumbs_up'] |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
118 | down = data['list'][0]['thumbs_down'] |
113
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
119 | reply ("\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % (word, definition, up, down)) |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
120 | except Exception as e: |
102
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
121 | command_error ('Urban dictionary lookup failed: %s' % e) |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
122 | |
113
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
123 | def cmd_commands (bot, reply, ident, host, **rest): |
102
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
124 | commandlist = ModuleCore.get_available_commands (ident, host) |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
125 | partitioned=[] |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
126 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
127 | while len (commandlist) > 0: |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
128 | partitioned.append (commandlist[0:15]) |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
129 | commandlist = commandlist[15:] |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
130 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
131 | for part in partitioned: |
113
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
132 | reply ('\002Available commands\002: %s' % (", ".join (part))) |
102
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
133 | |
113
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
134 | def cmd_help (bot, reply, ident, host, args, **rest): |
102
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
135 | cmd = ModuleCore.get_command_by_name (args['command']) |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
136 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
137 | if not cmd: |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
138 | command_error ('unknown command \'%s\'' % args['command']) |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
139 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
140 | if not ModuleCore.is_available (cmd, ident, host): |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
141 | command_error ('you may not use %s' % cmd['name']) |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
142 | |
113
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
143 | reply ('%s %s: %s' % (cmd['name'], cmd['args'], cmd['description'])) |
105 | 144 | |
106
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
145 | def mathsubstitute (expr, token, value): |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
146 | rex = re.compile (r'^(.*)\b' + token + r'\b(.*)$') |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
147 | match = rex.match (expr) |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
148 | |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
149 | while match: |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
150 | expr = match.group(1) + str (value) + match.group(2) |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
151 | match = rex.match (expr) |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
152 | |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
153 | return expr |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
154 | |
113
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
155 | def cmd_calc (bot, reply, args, **rest): |
105 | 156 | expr = args['expression'] |
157 | ||
158 | try: | |
106
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
159 | # Substitute some mathematical constants |
107
14e1d18d2a2a
- add more decimals to the mathematical constants
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
160 | expr = mathsubstitute (expr, 'pi' , 3.14159265358979323846264338327950288419716939937510) |
14e1d18d2a2a
- add more decimals to the mathematical constants
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
161 | expr = mathsubstitute (expr, 'e' , 2.71828182845904523536028747135266249775724709369995) |
14e1d18d2a2a
- add more decimals to the mathematical constants
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
162 | expr = mathsubstitute (expr, 'phi', 1.6180339887498948482) # golden ratio |
105 | 163 | |
164 | result = subprocess.check_output (['calc', expr], stderr=subprocess.STDOUT) \ | |
165 | .replace ('\t', '') \ | |
166 | .replace ('\n', '') | |
167 | ||
168 | errmatch = re.compile (r'^.*\bError\b.*$').match (result) | |
169 | ||
170 | if errmatch: | |
171 | command_error ('math error') | |
172 | return | |
173 | ||
113
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
174 | reply ('Result: %s' % result) |
105 | 175 | except subprocess.CalledProcessError as e: |
176 | command_error (e.output.split('\n')[0]) | |
177 | except OSError as e: | |
178 | command_error ('failed to execute calc: ' + e.strerror) | |
113
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
179 | |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
180 | def cmd_more (bot, replyto, **rest): |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
111
diff
changeset
|
181 | ModuleCore.print_responses (bot, replyto) |
117
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
182 | |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
183 | def cmd_yes (**k): |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
184 | ModuleCore.confirm (k, True) |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
185 | |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
186 | def cmd_no (**k): |
6c0609395889
- added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents:
113
diff
changeset
|
187 | ModuleCore.confirm (k, False) |