mod_util.py

changeset 113
08e9b1c1b324
parent 111
44d9aea72947
child 117
6c0609395889
equal deleted inserted replaced
112:cdafc1a0544e 113:08e9b1c1b324
41 'name': 'calc', 41 'name': 'calc',
42 'description': 'Calculates a mathematical expression using apcalc', 42 'description': 'Calculates a mathematical expression using apcalc',
43 'args': '<expression...>', 43 'args': '<expression...>',
44 'level': 'normal', 44 'level': 'normal',
45 }, 45 },
46
47 {
48 'name': 'more',
49 'description': 'Prints subsequent command result pages',
50 'args': None,
51 'level': 'normal',
52 },
46 ] 53 ]
47 } 54 }
48 55
49 def cmd_convert (bot, args, replyto, **rest): 56 def cmd_convert (bot, args, reply, **rest):
50 value = float (args['value']) 57 try:
58 value = float (args['value'])
59 except Exception as e:
60 command_error (str (e))
61
51 valuetype = args['valuetype'] 62 valuetype = args['valuetype']
52 63
53 if valuetype in ['radians', 'degrees']: 64 if valuetype in ['radians', 'degrees']:
54 if valuetype == 'radians': 65 if valuetype == 'radians':
55 radvalue = value 66 radvalue = value
56 degvalue = (value * 180.) / math.pi 67 degvalue = (value * 180.) / math.pi
57 else: 68 else:
58 radvalue = (value * math.pi) / 180. 69 radvalue = (value * math.pi) / 180.
59 degvalue = value 70 degvalue = value
60 71
61 bot.privmsg (replyto, '%s radians, %s degrees (%s)' % 72 reply ('%s radians, %s degrees (%s)' % (radvalue, degvalue, degvalue % 360.))
62 (radvalue, degvalue, degvalue % 360.))
63 return 73 return
64 74
65 if valuetype in ['celsius', 'fahrenheit']: 75 if valuetype in ['celsius', 'fahrenheit']:
66 if valuetype == 'celsius': 76 if valuetype == 'celsius':
67 celvalue = value 77 celvalue = value
68 fahrvalue = value * 1.8 + 32 78 fahrvalue = value * 1.8 + 32
69 else: 79 else:
70 celvalue = (value - 32) / 1.8 80 celvalue = (value - 32) / 1.8
71 fahrvalue = value 81 fahrvalue = value
72 82
73 bot.privmsg (replyto, '%s degrees celsius, %s degrees fahrenheit' % (celvalue, fahrvalue)) 83 reply ('%s degrees celsius, %s degrees fahrenheit' % (celvalue, fahrvalue))
74 return 84 return
75 85
76 command_error ('unknown valuetype %s, expected one of: degrees, radians (angle conversion), ' + 86 command_error ('unknown valuetype %s, expected one of: degrees, radians (angle conversion), ' +
77 'celsius, fahrenheit (temperature conversion)' % valuetype) 87 'celsius, fahrenheit (temperature conversion)' % valuetype)
78 88
79 def cmd_ud (bot, args, replyto, **rest): 89 def cmd_ud (bot, args, reply, **rest):
80 try: 90 try:
81 url = 'http://api.urbandictionary.com/v0/define?term=%s' % (args['term'].replace (' ', '%20')) 91 url = 'http://api.urbandictionary.com/v0/define?term=%s' % (args['term'].replace (' ', '%20'))
82 response = urllib2.urlopen (url).read() 92 response = urllib2.urlopen (url).read()
83 data = json.loads (response) 93 data = json.loads (response)
84 94
90 100
91 word = data['list'][0]['word'] 101 word = data['list'][0]['word']
92 definition = data['list'][0]['definition'].replace ('\r', ' ').replace ('\n', ' ').replace (' ', ' ') 102 definition = data['list'][0]['definition'].replace ('\r', ' ').replace ('\n', ' ').replace (' ', ' ')
93 up = data['list'][0]['thumbs_up'] 103 up = data['list'][0]['thumbs_up']
94 down = data['list'][0]['thumbs_down'] 104 down = data['list'][0]['thumbs_down']
95 bot.privmsg (replyto, "\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % (word, definition, up, down)) 105 reply ("\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % (word, definition, up, down))
96 except Exception as e: 106 except Exception as e:
97 command_error ('Urban dictionary lookup failed: %s' % e) 107 command_error ('Urban dictionary lookup failed: %s' % e)
98 108
99 def cmd_commands (bot, replyto, ident, host, **rest): 109 def cmd_commands (bot, reply, ident, host, **rest):
100 commandlist = ModuleCore.get_available_commands (ident, host) 110 commandlist = ModuleCore.get_available_commands (ident, host)
101 partitioned=[] 111 partitioned=[]
102 112
103 while len (commandlist) > 0: 113 while len (commandlist) > 0:
104 partitioned.append (commandlist[0:15]) 114 partitioned.append (commandlist[0:15])
105 commandlist = commandlist[15:] 115 commandlist = commandlist[15:]
106 116
107 for part in partitioned: 117 for part in partitioned:
108 bot.privmsg (replyto, '\002Available commands\002: %s' % (", ".join (part))) 118 reply ('\002Available commands\002: %s' % (", ".join (part)))
109 119
110 def cmd_help (bot, replyto, ident, host, args, **rest): 120 def cmd_help (bot, reply, ident, host, args, **rest):
111 cmd = ModuleCore.get_command_by_name (args['command']) 121 cmd = ModuleCore.get_command_by_name (args['command'])
112 122
113 if not cmd: 123 if not cmd:
114 command_error ('unknown command \'%s\'' % args['command']) 124 command_error ('unknown command \'%s\'' % args['command'])
115 125
116 if not ModuleCore.is_available (cmd, ident, host): 126 if not ModuleCore.is_available (cmd, ident, host):
117 command_error ('you may not use %s' % cmd['name']) 127 command_error ('you may not use %s' % cmd['name'])
118 128
119 bot.privmsg (replyto, '%s %s: %s' % (cmd['name'], cmd['args'], cmd['description'])) 129 reply ('%s %s: %s' % (cmd['name'], cmd['args'], cmd['description']))
120 130
121 def mathsubstitute (expr, token, value): 131 def mathsubstitute (expr, token, value):
122 rex = re.compile (r'^(.*)\b' + token + r'\b(.*)$') 132 rex = re.compile (r'^(.*)\b' + token + r'\b(.*)$')
123 match = rex.match (expr) 133 match = rex.match (expr)
124 134
126 expr = match.group(1) + str (value) + match.group(2) 136 expr = match.group(1) + str (value) + match.group(2)
127 match = rex.match (expr) 137 match = rex.match (expr)
128 138
129 return expr 139 return expr
130 140
131 def cmd_calc (bot, replyto, args, **rest): 141 def cmd_calc (bot, reply, args, **rest):
132 expr = args['expression'] 142 expr = args['expression']
133 143
134 try: 144 try:
135 # Substitute some mathematical constants 145 # Substitute some mathematical constants
136 expr = mathsubstitute (expr, 'pi' , 3.14159265358979323846264338327950288419716939937510) 146 expr = mathsubstitute (expr, 'pi' , 3.14159265358979323846264338327950288419716939937510)
145 155
146 if errmatch: 156 if errmatch:
147 command_error ('math error') 157 command_error ('math error')
148 return 158 return
149 159
150 bot.privmsg (replyto, 'Result: %s' % result) 160 reply ('Result: %s' % result)
151 except subprocess.CalledProcessError as e: 161 except subprocess.CalledProcessError as e:
152 command_error (e.output.split('\n')[0]) 162 command_error (e.output.split('\n')[0])
153 except OSError as e: 163 except OSError as e:
154 command_error ('failed to execute calc: ' + e.strerror) 164 command_error ('failed to execute calc: ' + e.strerror)
165
166 def cmd_more (bot, replyto, **rest):
167 ModuleCore.print_responses (bot, replyto)

mercurial