Based on jamespenguin pokebot, but modified a little bit.