Example #1
0
 void onMessage(SleepyDiscord::Message message) 
 {
     if (message.startsWith(Config::commandPrefix))
         sendMessage(message.channelID, "Hello " + message.author.username);
 }
Example #2
0
	void onMessage(SleepyDiscord::Message message) {
		if (message.startsWith("whcg hello")) {
			games.push_back(Game(message.author));	//create a game put it on the end of the list of games
#ifdef _DEBUG
			std::cout << "DUBUG: New game created. Number of games: " << games.size() << '\n';
#endif
			std::list<Game>::iterator game = --(games.end());
			*game->state = GET_READY;
			SleepyDiscord::Message countMessage = sendMessage(message.channelID, "**ROCK!**");
			schedule([this, countMessage]() { 
				this->editMessage(countMessage, "**PAPER!**"); 
			}, 1000);
			schedule([this, countMessage]() {
				this->editMessage(countMessage, "**SCISSORS!**");
			}, 2000);
			schedule([this, game, countMessage]() {
				*game->state = SHOOT;
				this->editMessage(countMessage, "**SHOOT!**");
			}, 3000);
			game->endGameTimer = schedule([this, game, message]() {
				if (*game->state != ENDED) {
					*game->state = ENDED;
					sendMessage(message.channelID, "You lose.\\nWhen I say ``shoot``, you pick ``rock``, ``paper``, or ``scissors``");
				}
				this->games.erase(game);
			}, 4000);
			return;
		}

		if (games.empty()) return;

		//get the player's choice
		Weapon playerChoice;
		if      (message.content == "rock"     || message.content == ":fist:"       ) playerChoice = ROCK;
		else if (message.content == "paper"    || message.content == ":raised_hand:") playerChoice = PAPER;
		else if (message.content == "scissors" || message.content == ":v:"          ) playerChoice = SCISSORS;
		else return;	//go back if there's no choice was detected
		for (Game& game : games) {
			if (game.player == message.author) {
#ifdef _DEBUG
				std::cout << "DUBUG: Player detected " << game.player.username << " state: " << *game.state << '\n';
#endif
				switch (*game.state) {
				case SHOOT: {
					*game.state = ENDED; //game state is set to end here, so that "you lose" isn't sent after a weapon was picked
					Weapon botChoice = static_cast<Weapon>(rand() % 3);	//random number 0 to 2
					switch (botChoice) {
					case ROCK:     sendMessage(message.channelID, ":fist:");        break;
					case PAPER:    sendMessage(message.channelID, ":raised_hand:"); break;
					case SCISSORS: sendMessage(message.channelID, ":v:");           break;
					}
					switch ((playerChoice - botChoice + 3) % 3) {
					case LOSE: sendMessage(message.channelID, ":smiley: I won, and you lost!");  break;
					case TIE:  sendMessage(message.channelID, ":neutral_face: It's a tie");      break;
					case WIN:  sendMessage(message.channelID, ":frowning: I lost, and you won"); break;
					}
				} break;
				case GET_READY: sendMessage(message.channelID, "Not yet, I didn't get to say ``shoot``."); break;
				default: sendMessage(message.channelID, "Error: unknown state"); break;
				}
			}
		}
	}