async def on_message(message): intended_recipient = False for bn in config.bot_names: if message.content.lower().startswith(bn + ' '): intended_recipient = True message.content = message.content[len(bn) + 1:] break if not intended_recipient: return logger.log_msg( 'INFO', 'Received message from {} on {}/{}, contents = {}'.format( message.author.name, message.server, message.channel, message.content)) for (k, v) in config.command_map.items(): for cmd in k.split('|'): if message.content.lower().startswith(cmd): message.content = message.content[len(cmd) + 1:] response = await commands.parser.option_call(v, message) await client.send_message(message.channel, response) for (k, v) in config.interactive_command_map.items(): for cmd in k.split('|'): if message.content.lower().startswith(cmd): message.content = message.content[len(cmd) + 1:] await commands.parser.option_call(v, message, client)
async def purge(message, client): if message.server is None: await client.send_message(message.channel, "This function may not be used in PM's") return messages = list(reversed(client.messages)) deleted = 0 to_delete = 0 for m in message.content.split(' '): if m.isdigit(): to_delete = int(m) logger.log_msg( 'INFO', 'Deleting {} messages from {}/{}'.format(to_delete, message.server, message.channel)) if to_delete > len(messages): await client.send_message( message.channel, "There are not enough messages to delete this many; as many as {} will be deleted" .format(to_delete)) ind = 0 while deleted < to_delete and ind < len(messages): if messages[ind] != message and messages[ ind].channel == message.channel and (not message.mentions or messages[ind].author in message.mentions): await client.delete_message(messages[ind]) deleted += 1 ind += 1
def roll(msg, show_rolls=False, shadowrun=False): message = msg.content try: all_rolls = [] while (re.search(r'(\d+)d(\d+)', message)): m = re.search(r'(\d+)d(\d+)', message) num_dice = int(m.group(1)) dice_val = int(m.group(2)) if num_dice >= 1000: return 'Let\'s not' rolls = [ random.randrange(1, dice_val + 1) for i in range(num_dice) ] all_rolls.extend(rolls) message = message.replace(m.group(), str(sum(rolls)), 1) successes = 0 if show_rolls or shadowrun: all_rolls = map(str, sorted(all_rolls)) if shadowrun: all_rolls = [ '**{}**'.format(x) if int(x) >= 5 else x for x in all_rolls ] successes = sum([1 if x[0] == '*' else 0 for x in all_rolls]) if re.match(r'^[0-9\+\-\*\/\>\<\(\) ]+$', message): if shadowrun: return '{} ({})'.format(successes, ', '.join(all_rolls)) if show_rolls: return '{} ({})'.format(eval(message), ', '.join(all_rolls)) else: return str(eval(message)) else: logger.log_msg( 'Failed to parse {} => {} as a math expression'.format( msg.content, message)) return 'I couldn\'t parse `{}`'.format(message) except: logger.log_msg('Failed to parse {} => {} as a math expression'.format( msg.content, message)) return 'I couldn\'t parse `{}`'.format(message)
async def on_ready(): logger.log_msg('INFO', 'Logged in as {}'.format(client.user.name))
async def connect_four(message, client): if len(message.mentions) > 4: await client.send_message('Too many players. This connect 4 can be played with up to 5 players only') player = await init_game(message, client, len(message.mentions)+1) ap = 0 rows = 2 + 2 * len(player) cols = min(10, 5 + len(player)) board = [[0 for x in range(cols)] for y in range(rows)] last_msg = None token = [':white_circle:', ':red_circle:', ':large_blue_circle:', ':green_apple:', ':yellow_heart:', ':large_orange_diamond:'] bottom_text = ' '.join([':one:',':two:',':three:',':four:',':five:',':six:',':seven:',':eight:',':nine:',':keycap_ten:'][:cols]) moves = 0 if not player: return logger.log_msg('INFO', 'Starting connect four game between {} and {}'.format(pname(player[0]), pname(player[1]))) while True: win = False for i in range(rows): for j in range(cols): if board[i][j] != 0: horz = True vert = True d1 = True d2 = True for k in range(1, 4): horz &= (j + k < cols and board[i][j+k] == board[i][j]) vert &= (i + k < rows and board[i+k][j] == board[i][j]) d1 &= (j + k < cols and i + k < rows and board[i+k][j+k] == board[i][j]) d2 &= (j + k >= 0 and i + k < rows and board[i+k][j-k] == board[i][j]) if horz or vert or d1 or d2: win = True if last_msg: await client.delete_message(last_msg) m = "" for x in board: for y in x: m += token[y] + ' ' m += '\n' m += bottom_text + '\n' if win: await client.send_message(message.channel, m) await client.send_message(message.channel, '{} wins!'.format(pname(player[(ap+len(player)-1)%len(player)]))) return else: m += '{}, it is your turn!'.format(pname(player[ap])) last_msg = await client.send_message(message.channel, m) if moves == rows * cols: await client.send_message(message.channel, "Draw!") return msg = await client.wait_for_message(author=player[ap], check = lambda x: x.content.isdigit() and int(x.content) >= 1 and int(x.content) <= cols and board[0][int(x.content)-1] == 0) move = int(msg.content)-1 try: await client.delete_message(msg) except: pass y = rows-1 while board[y][move] != 0: y -= 1 board[y][move] = ap+1 moves += 1 ap = (ap + 1) % len(player)