-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from lesteenman/issue-44-change-guesses-commands
Fix #44: Implement edit-guess and delete-guess management commands.
- Loading branch information
Showing
16 changed files
with
856 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from eternal_guesses.model.discord.discord_event import DiscordEvent | ||
from eternal_guesses.model.discord.discord_response import DiscordResponse | ||
from eternal_guesses.repositories.games_repository import GamesRepository | ||
from eternal_guesses.routes.route import Route | ||
from eternal_guesses.util.game_post_manager import GamePostManager | ||
from eternal_guesses.util.message_provider import MessageProvider | ||
|
||
|
||
class DeleteGuessRoute(Route): | ||
def __init__(self, | ||
games_repository: GamesRepository, | ||
message_provider: MessageProvider, | ||
game_post_manager: GamePostManager): | ||
self.game_post_manager = game_post_manager | ||
self.message_provider = message_provider | ||
self.games_repository = games_repository | ||
|
||
async def call(self, event: DiscordEvent) -> DiscordResponse: | ||
guild_id = event.guild_id | ||
game_id = event.command.options['game-id'] | ||
member = int(event.command.options['member']) | ||
|
||
game = self.games_repository.get(guild_id=guild_id, game_id=game_id) | ||
|
||
if game is None: | ||
error_game_not_found = self.message_provider.error_game_not_found(game_id) | ||
return DiscordResponse.ephemeral_channel_message(error_game_not_found) | ||
|
||
if member not in game.guesses: | ||
error_guess_not_found = self.message_provider.error_guess_not_found(game_id, member) | ||
return DiscordResponse.ephemeral_channel_message(error_guess_not_found) | ||
|
||
del game.guesses[member] | ||
|
||
self.games_repository.save(game) | ||
|
||
await self.game_post_manager.update(game) | ||
|
||
guess_deleted_message = self.message_provider.guess_deleted() | ||
return DiscordResponse.ephemeral_channel_message(guess_deleted_message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from eternal_guesses.model.discord.discord_event import DiscordEvent | ||
from eternal_guesses.model.discord.discord_response import DiscordResponse | ||
from eternal_guesses.repositories.games_repository import GamesRepository | ||
from eternal_guesses.routes.route import Route | ||
from eternal_guesses.util.game_post_manager import GamePostManager | ||
from eternal_guesses.util.message_provider import MessageProvider | ||
|
||
|
||
class EditGuessRoute(Route): | ||
def __init__(self, | ||
games_repository: GamesRepository, | ||
message_provider: MessageProvider, | ||
game_post_manager: GamePostManager): | ||
self.games_repository = games_repository | ||
self.message_provider = message_provider | ||
self.game_post_manager = game_post_manager | ||
|
||
async def call(self, event: DiscordEvent) -> DiscordResponse: | ||
guild_id = event.guild_id | ||
game_id = event.command.options['game-id'] | ||
member = int(event.command.options['member']) | ||
guess = event.command.options['guess'] | ||
|
||
game = self.games_repository.get(guild_id=guild_id, game_id=game_id) | ||
|
||
if game is None: | ||
game_not_found_error = self.message_provider.error_game_not_found(game_id) | ||
return DiscordResponse.ephemeral_channel_message(game_not_found_error) | ||
|
||
if member not in game.guesses: | ||
guess_not_found_error = self.message_provider.error_guess_not_found(game_id, member) | ||
return DiscordResponse.ephemeral_channel_message(guess_not_found_error) | ||
|
||
game.guesses[member].guess = guess | ||
self.games_repository.save(game) | ||
|
||
await self.game_post_manager.update(game) | ||
|
||
guess_edited_message = self.message_provider.guess_edited() | ||
return DiscordResponse.ephemeral_channel_message(guess_edited_message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from abc import ABC | ||
|
||
import discord | ||
from loguru import logger | ||
|
||
from eternal_guesses.model.data.game import Game | ||
from eternal_guesses.repositories.games_repository import GamesRepository | ||
from eternal_guesses.util.discord_messaging import DiscordMessaging | ||
from eternal_guesses.util.message_provider import MessageProvider | ||
|
||
|
||
class GamePostManager(ABC): | ||
async def post(self, game: Game): | ||
raise NotImplementedError() | ||
|
||
async def update(self, game: Game): | ||
raise NotImplementedError() | ||
|
||
|
||
class GamePostManagerImpl(GamePostManager): | ||
def __init__(self, games_repository: GamesRepository, message_provider: MessageProvider, | ||
discord_messaging: DiscordMessaging): | ||
self.discord_messaging = discord_messaging | ||
self.games_repository = games_repository | ||
self.message_provider = message_provider | ||
|
||
async def post(self, game: Game): | ||
pass | ||
|
||
async def update(self, game: Game): | ||
logger.info(f"updating {len(game.channel_messages)} channel messages for {game.game_id}") | ||
if game.channel_messages is not None: | ||
new_embed = self.message_provider.game_post_embed(game) | ||
for channel_message in game.channel_messages: | ||
logger.debug(f"sending update to channel message, channel_id={channel_message.channel_id}, " | ||
f"message_id={channel_message.message_id}, message='{new_embed}'") | ||
|
||
try: | ||
await self.discord_messaging.update_channel_message(channel_message.channel_id, | ||
channel_message.message_id, | ||
embed=new_embed) | ||
except discord.NotFound: | ||
game.channel_messages.remove(channel_message) | ||
self.games_repository.save(game) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.