Discord saw Tirek as a way to have a friend, but not limit what he can and can't do. In Keep Calm and Flutter On, Discord gets reformed and wants friendship. Even though the Mane Six, excluding Fluttershy, said they were friends, they didn't act like it. He tested this in Three's a Crowd to see if Twilight was a true friend. Then, when Tirek offered freedom, he saw his opportunity. When he was betrayed, he realizes that having friends that dislike you is better than someone who says they are your friend, then uses you. Also, Discord probably knew about the keys, and was helping Twilight to get hers.