def execute(self, param, message, system): player = message.author wheelgame = system.wheel_manager.get_game(player) if wheelgame is None: return {"response": parser.direct_call(system.id_manager.current_id, "nogame")} if wheelgame.get_current_player() is not player: current_player = system.nickname_manager.get_name(wheelgame.get_current_player()) return {"response": parser.direct_call(system.id_manager.current_id, "noturn").format(current_player)} spin_value, spin_text = wheelgame.spin_wheel() if spin_value > 0: return {"response": parser.direct_call(system.id_manager.current_id, "goodspin").format(spin_text)} elif spin_value == 0: return {"response": parser.direct_call(system.id_manager.current_id, "freespin")} else: opponent = system.nickname_manager.get_name(wheelgame.get_current_player()) return {"response": parser.direct_call(system.id_manager.current_id, "badspin").format(spin_text, opponent)}
def tictactoeend(author, system): """ Ends a game of tic-tac-toe, and removes all references to it. :param author: One of the (human) participants of the game. :param system: The system meta-object, containing information about the current bot-state. :return: A response based on the current identity of the bot, signifying either that the game has been abandoned, or an error if no game was found. """ if author in system.ttt_games: game = system.ttt_games[author] for key in game.players: system.ttt_games.pop(game.players[key], None) if game.players[key] != author: other_player = game.players[key] return parser.direct_call(system.id_manager.current_id, "abandon").format( get_addressable(system, author), get_addressable(system, other_player)) else: return parser.direct_call(system.id_manager.current_id, "error")
def execute(self, param, message, system): """ A method for changing the chat interval of the bot. :param param: The parameter string. Should contain only an integer. :param message: Unused parent parameter. :param system: The system meta-object, containing information about the current state of the bot. :return: A response that signifies whether the bot will speak more often or less often. """ identities = system.id_manager try: old_val = identities.interval identities.interval = int(param) print("Changing the interval from {} to {}".format(old_val, identities.interval)) if old_val == identities.interval: return {"response": StatusCommand.get_status(identities)} elif old_val < identities.interval: return {"response": parser.direct_call(identities.current_id, "nonchatty")} else: return {"response": parser.direct_call(identities.current_id, "chatty")} except ValueError: raise CommandError("number_not_valid", param)
def execute(self, param, message, system): player = message.author wheelgame = system.wheel_manager.get_game(player) if wheelgame is None: return {"response": parser.direct_call(system.id_manager.current_id, "nogame")} if wheelgame.get_current_player() is not player: current_player = system.nickname_manager.get_name(wheelgame.get_current_player()) return {"response": parser.direct_call(system.id_manager.current_id, "noturn").format(current_player)} if wheelgame.spin_value != 0 and not wheelgame.freespin: return {"response": parser.direct_call(system.id_manager.current_id, "spun").format(param)} solved = wheelgame.solve_word(param) player_name = system.nickname_manager.get_name(player) if solved: score_text = wheelgame.get_monetary_value(wheelgame.score[player]) system.wheel_manager.add_score(player, wheelgame.score[player]) system.wheel_manager.games.remove(wheelgame) return {"response": parser.direct_call(system.id_manager.current_id, "wheelwin").format(player_name, score_text), "board": str(wheelgame), "scores": wheelgame.get_scores_with_nicknames(system)} else: player_name = system.nickname_manager.get_name(wheelgame.get_current_player()) return {"response": parser.direct_call(system.id_manager.current_id, "wrongsolve").format(player_name)}
def identity_response(self, message): """ Method that gets a response from an identity. :param message: The message which should be responded to. :return: A string containing a response & A new identity (or false if the identity should not change). """ new_id = parser.find_new_id(message.content, self.identities.identities) if len(new_id) and self.identities.current_id not in new_id: identity_switch = new_id[0] response = parser.direct_call(identity_switch, "call") else: identity_switch = False response = parser.get_response(message.content, self.identities.current_id) return response, identity_switch
def execute(self, param, message, system): dissection = re.findall("(\\d+)\\s?([a-zA-Z])\\w*\\s+(.*)", param) if dissection: time_str, unit, text = dissection[0] try: time = int(time_str) delta = get_delta(time, unit) system.reminder_manager.add_reminder(delta, "> " + text, message.author) return { "response": parser.direct_call(system.id_manager.current_id, "reminder") } except ValueError: raise CommandError("number_not_valid", time_str) else: raise CommandError("invalid_reminder", param)
async def birthday_task(client, system): """ A separate thread, keeping track of birthdays. """ await client.wait_until_ready() while not client.is_closed(): birthday_ids = system.birthday_manager.get_today_birthdays() for birthday_id in birthday_ids: channels = get_main_channels(client) for channel in channels: birthday_user = system.get_user_by_id(birthday_id, client=client, guild=channel.guild) user_name = system.nickname_manager.get_name(birthday_user) message = parser.direct_call(system.id_manager.current_id, "birthday").format(user_name) await channel.send(message) if birthday_ids: await asyncio.sleep(THIRD_DAY * 2) await asyncio.sleep(THIRD_DAY)
def execute(self, param, message, system): player = message.author wheelgame = system.wheel_manager.get_game(player) if wheelgame is None: return {"response": parser.direct_call(system.id_manager.current_id, "nogame")} if wheelgame.get_current_player() is not player: current_player = system.nickname_manager.get_name(wheelgame.get_current_player()) return {"response": parser.direct_call(system.id_manager.current_id, "noturn").format(current_player)} if not wheelgame.board.is_valid_character(param): return {"response": parser.direct_call(system.id_manager.current_id, "nocharacter").format(param)} if wheelgame.board.is_vowel(param): return {"response": parser.direct_call(system.id_manager.current_id, "noconsonant").format(param)} if wheelgame.spin_value == 0 and not wheelgame.freespin: return {"response": parser.direct_call(system.id_manager.current_id, "noguess").format(param)} count = wheelgame.guess_consonant(param) if count == 0: opponent = system.nickname_manager.get_name(wheelgame.get_current_player()) return {"response": parser.direct_call(system.id_manager.current_id, "badguess").format(param, opponent), "board": str(wheelgame), "scores": wheelgame.get_scores_with_nicknames(system)} return {"response": parser.direct_call(system.id_manager.current_id, "goodguess").format(count, param), "board": str(wheelgame), "scores": wheelgame.get_scores_with_nicknames(system)}
def tictactoemove(text, author, system): """ Makes a move in a game of tic-tac-toe, and immediately makes a CPU move if it's their turn. If no game of tic-tac-toe is found, create one, and make the move if it is valid. :param text: The parameters supplied by the user. :param author: The user making the move OR in case of a CPU move: The other user associated with the game. :param system: The system meta-object, containing information about the current bot-state. :return: esponse (String): A response based on the current identity of the bot, signifying either a new game if none existed, the gamestate if no parameters are found, the move made by the bot, or the victory/tie of either party str(game) (string): A representation of the game board. """ response = "" if author in system.ttt_games: game = system.ttt_games[author] else: #Starting a new game if none exists: response = "{}\n".format( tictactoenewgame(author, system.bot, system)[0]) game = system.ttt_games[author] #Replying with a status: if text == "" and response == "": opponent = game.players[game.get_other_player()] player_name = get_addressable(system, game.players[game.turn]) opponent_name = get_addressable(system, opponent) response = parser.direct_call(system.id_manager.current_id, "gamestate").format( player_name, opponent_name) return response, game #Making a move: move = "" if game.players[game.turn] == system.bot: move = game.get_cpu_move(system.id_manager.get_current_ai()) response += parser.direct_call(system.id_manager.current_id, "ownmove").format(game.turn, move) + "\n" elif game.players[game.turn] == author: move = text[:2].upper() valid = game.make_move(move) if not valid and text != "": response = parser.direct_call(system.id_manager.current_id, "invalid") return response, str(game) #Handling a winstate win_state, w_p = game.get_status() if win_state: tictactoeend(author, system) response += get_win_response(w_p, game, system) return response, str(game) #If there's a CPU, have him make a move. if game.players[game.turn] == system.bot: response += tictactoemove("cpu", author, system)[0] if not response: response = parser.direct_call( system.id_manager.current_id, "gamestate").format( get_addressable(system, game.players[game.turn]), get_addressable(system, game.players[game.get_other_player()])) return response, str(game)
def execute(self, param, message, system): try: system.id_manager.un_ban(message.channel.id) return {"response": parser.direct_call(system.id_manager.current_id, "call")} except ValueError: raise CommandError("not_banned", None)
def get_status(id_manager): """Method for getting the status string. Can be used by other classes if necessary.""" return parser.direct_call(id_manager.current_id, "status").format(id_manager.get_chatty_string(), id_manager.get_bans_string().lower(), id_manager.interval)