def sub_replace(match, packet: ircp.Packet, shared: dict) -> str: """ Naive substitution regular expression on previous message. """ global_flag = False parts = match.group(0).split('/') old, new = None, None old = parts[1] new = parts[2] if len(parts) >= 4: if parts[3].lower() == 'g': global_flag = True # Now, find the last message in the same channel that # contains `old` orig_packet = None output = '' print('looking for "{}"'.format(old)) for p in reversed(shared['recent_messages']): if p.target == packet.target: if p.msg_public and (p.text.find(old) != -1): output = str(p.text) orig_packet = p break else: return packet.reply('Could not find a suitable substitution target.') print('replacing "{}" with "{}"'.format(old, new)) print('original: "{}"'.format(output)) # patch in italics new = CLR_ITLCS + new + CLR_RESET if global_flag: output = output.replace(old, new) else: output = output.replace(old, new, 1) return packet.reply('<{}> {}'.format(orig_packet.sender, output))
def matched_url(match, packet: ircp.Packet, shared: dict): ''' Match the url regex ''' # At the moment this only cares about the first link in a message matched = match.group(0) print('matched url: {}'.format(matched)) try: title = link_info(matched) if title is not None: return packet.reply(title) except Exception: print('Failed to parse link: {}'.format(matched))
def sub_replace(match, packet: ircp.Packet, shared: dict) -> str: """ Naive substitution regular expression on previous message. """ global_flag = False parts = match.group(0).split('/') old, new = None, None old = parts[1] new = parts[2] if len(parts) >= 4: if parts[3].lower() == 'g': global_flag = True # Now, find the last message in the same channel that # contains `old` orig_packet = None output = '' print('looking for "{}"'.format(old)) for p in reversed(shared['recent_messages']): if p.target == packet.target: if p.msg_public and (p.text.find(old) != -1): output = str(p.text) orig_packet = p break else: return packet.reply('Could not find a suitable substitution target.') print('replacing "{}" with "{}"'.format(old, new)) print('original: "{}"'.format(output)) # patch in italics new = CLR_ITLCS + new + CLR_RESET if global_flag: output = output.replace(old, new) else: output = output.replace(old, new, 1) return packet.reply('<{}> {}'.format(orig_packet.sender, output))
def convert_command(arg: tuple, packet: ircp.Packet, shared: dict): if len(arg) < 5: return (packet.notice('You need to specify an amount, original current, and output currency!'), packet.notice('For example - :convert $50 USD to Pounds')) conversion = get_conversion(arg[1:], shared) if conversion == '' or conversion is None: return None else: return packet.reply('{}: {}'.format(packet.sender, conversion))
def whatis_command(arg: tuple, packet: ircp.Packet, shared: dict): if len(arg) < 2: return ircp.make_notice('You must specify a type of currency!', packet.sender) orig_currency = (' '.join(arg[1:])) currency = str(orig_currency).upper() if currency in ALIASES: currency = ALIASES[currency] if currency not in CURRENCIES: return ircp.make_notice('I don\'t know what "{}" is!'.format(currency), packet.sender) else: return packet.reply('{} is {}'.format(orig_currency, CURRENCIES[currency]))
def hello_world(arg: tuple, packet: ircp.Packet, shared: dict): ''' Hello, world! This is an example template command arg - the tuple of keywords packet - the packet object to receive shared - the shared data dictionary ''' # arg[0] is always the command name (in this case 'hello', or 'h'). # You can use this to assign multiple commands to the same function. # Different indexes are parsed similarly to how sys.argv is parsed, # paying attention to quotes and backslash-escapes. print(arg) return packet.reply('Hello, there!')
def hello_world(arg: tuple, packet: ircp.Packet, shared: dict): ''' Hello, world! This is an example template command arg - the tuple of keywords packet - the packet object to receive shared - the shared data dictionary ''' # arg[0] is always the command name (in this case 'hello', or 'h'). # You can use this to assign multiple commands to the same function. # Different indexes are parsed similarly to how sys.argv is parsed, # paying attention to quotes and backslash-escapes. print(arg) return packet.reply('Hello, there!')
def calc_command(arg: tuple, packet: ircp.Packet, shared: dict) -> str: ''' Evaluate a mathematical expression usage: :calc <expression> :calc 1 + 1 == 2 :calc 3^3 ''' problem = (''.join(arg[1:])).replace('^', '**') print('solving for {}'.format(problem)) try: result = evaluate_expression(problem) if result is None: raise ValueError('Invalid result') reply = 'Result: {}'.format(result) return packet.reply(reply) except ZeroDivisionError as e: return packet.notice('Error: Divide by zero.') except Exception as e: print(e) return packet.notice('Sorry, but that expression was invalid. Please try again.')
def calc_command(arg: tuple, packet: ircp.Packet, shared: dict) -> str: ''' Evaluate a mathematical expression usage: :calc <expression> :calc 1 + 1 == 2 :calc 3^3 ''' problem = (''.join(arg[1:])).replace('^', '**') print('solving for {}'.format(problem)) try: result = evaluate_expression(problem) if result is None: raise ValueError('Invalid result') reply = 'Result: {}'.format(result) return packet.reply(reply) except ZeroDivisionError as e: return packet.notice('Error: Divide by zero.') except Exception as e: print(e) return packet.notice( 'Sorry, but that expression was invalid. Please try again.')
def respond_greeting(matched, packet: ircp.Packet, shared: dict) -> str: ''' Respond to a nice greeting ''' return packet.reply('Hello to you too, {1}{0}{2}!'.format( packet.sender, CLR_NICK, CLR_RESET))
def uptime_command(__: tuple, packet: ircp.Packet, shared: dict): """ Print current uptime """ start_time = shared['stats']['starttime'] uptime = _uptime(start_time) return packet.reply(uptime)
def respond_bad(matched, packet: ircp.Packet, shared: dict) -> str: ''' Respond to an ugly, mean message ''' return packet.reply('F**k you too, {1}{0}{2}!'.format(packet.sender, CLR_NICK, CLR_RESET))
def respond_greeting(matched, packet: ircp.Packet, shared: dict) -> str: ''' Respond to a nice greeting ''' return packet.reply('Hello to you too, {1}{0}{2}!'.format(packet.sender, CLR_NICK, CLR_RESET))
def uptime_command(__: tuple, packet: ircp.Packet, shared: dict): """ Print current uptime """ start_time = shared["stats"]["starttime"] uptime = _uptime(start_time) return packet.reply(uptime)
def respond_good(matched, packet: ircp.Packet, shared: dict) -> str: ''' Respond to a good message ''' return packet.reply('Thanks, {1}{0}{2}!'.format(packet.sender, CLR_NICK, CLR_RESET))
def respond_bad(matched, packet: ircp.Packet, shared: dict) -> str: ''' Respond to an ugly, mean message ''' return packet.reply('F**k you too, {1}{0}{2}!'.format( packet.sender, CLR_NICK, CLR_RESET))
def respond_good(matched, packet: ircp.Packet, shared: dict) -> str: ''' Respond to a good message ''' return packet.reply('Thanks, {1}{0}{2}!'.format(packet.sender, CLR_NICK, CLR_RESET))