Skip to content

Commit

Permalink
Merge pull request #46 from lesteenman/issue-44-change-guesses-commands
Browse files Browse the repository at this point in the history
Fix #44: Implement edit-guess and delete-guess management commands.
  • Loading branch information
lesteenman authored Apr 25, 2021
2 parents 483382e + 9f8c5bf commit f1fb38a
Show file tree
Hide file tree
Showing 16 changed files with 856 additions and 103 deletions.
44 changes: 44 additions & 0 deletions bin/register_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,50 @@ def register(config: Dict):
"required": False,
}
]
},
{
"name": "delete-guess",
"description": "Delete a member's guess on a game.",
"type": COMMAND_OPTION_TYPE_SUB_COMMAND,
"options": [
{
"name": "game-id",
"description": "The ID of the Eternal Guess game",
"type": COMMAND_OPTION_TYPE_STRING,
"required": True,
},
{
"name": "member",
"description": "The member whose guess to delete",
"type": COMMAND_OPTION_TYPE_USER,
"required": True,
},
]
},
{
"name": "edit-guess",
"description": "Edit a member's guess on a game.",
"type": COMMAND_OPTION_TYPE_SUB_COMMAND,
"options": [
{
"name": "game-id",
"description": "The ID of the Eternal Guess game",
"type": COMMAND_OPTION_TYPE_STRING,
"required": True,
},
{
"name": "member",
"description": "The member whose guess to change",
"type": COMMAND_OPTION_TYPE_USER,
"required": True,
},
{
"name": "guess",
"description": "The new guess",
"type": COMMAND_OPTION_TYPE_STRING,
"required": True,
},
]
}
]
},
Expand Down
10 changes: 9 additions & 1 deletion discord_app/eternal_guesses/api/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ def __init__(self,
add_management_channel_route: Route,
remove_management_channel_route: Route,
add_management_role_route: Route,
remove_management_role_route: Route):
remove_management_role_route: Route,
edit_guess_route: Route,
delete_guess_route: Route):
self.route_handler = route_handler
self.list_games_route = list_games_route
self.edit_guess_route = edit_guess_route
self.delete_guess_route = delete_guess_route
self.close_game_route = close_game_route
self.post_route = post_route
self.create_route = create_route
Expand All @@ -54,6 +58,10 @@ def _register_routes(self):
permission=PermissionSet.MANAGEMENT))
self._register(RouteDefinition(self.list_games_route, 'manage', 'list-games',
permission=PermissionSet.MANAGEMENT))
self._register(RouteDefinition(self.edit_guess_route, 'manage', 'edit-guess',
permission=PermissionSet.MANAGEMENT))
self._register(RouteDefinition(self.delete_guess_route, 'manage', 'delete-guess',
permission=PermissionSet.MANAGEMENT))
self._register(RouteDefinition(self.create_route, 'create',
permission=PermissionSet.MANAGEMENT))
self._register(RouteDefinition(self.guild_info_route, 'admin', 'info',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

class UnknownEventException(Exception):
def __init__(self, event: DiscordEvent):
super().__init__(f"could not handle event (type={event.type}")
super().__init__(f"could not handle event {event}")
40 changes: 40 additions & 0 deletions discord_app/eternal_guesses/routes/delete_guess.py
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)
40 changes: 40 additions & 0 deletions discord_app/eternal_guesses/routes/edit_guess.py
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)
30 changes: 5 additions & 25 deletions discord_app/eternal_guesses/routes/guess.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
from datetime import datetime

import discord
from loguru import logger

from eternal_guesses.model.data.game import Game
from eternal_guesses.model.data.game_guess import GameGuess
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.discord_messaging import DiscordMessaging
from eternal_guesses.util.game_post_manager import GamePostManager
from eternal_guesses.util.message_provider import MessageProvider


class GuessRoute(Route):
def __init__(self,
games_repository: GamesRepository,
discord_messaging: DiscordMessaging,
message_provider: MessageProvider):
message_provider: MessageProvider,
game_post_manager: GamePostManager):
self.games_repository = games_repository
self.discord_messaging = discord_messaging
self.message_provider = message_provider
self.game_post_manager = game_post_manager

async def call(self, event: DiscordEvent) -> DiscordResponse:
guild_id = event.guild_id
Expand Down Expand Up @@ -51,23 +47,7 @@ async def call(self, event: DiscordEvent) -> DiscordResponse:
game.guesses[int(user_id)] = game_guess
self.games_repository.save(game)

await self._update_channel_messages(game)
await self.game_post_manager.update(game)

guess_added_message = self.message_provider.guess_added(game_id, guess)
return DiscordResponse.ephemeral_channel_message(content=guess_added_message)

async def _update_channel_messages(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)
44 changes: 44 additions & 0 deletions discord_app/eternal_guesses/util/game_post_manager.py
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)
53 changes: 49 additions & 4 deletions discord_app/eternal_guesses/util/injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from eternal_guesses.routes.add_management_role import AddManagementRoleRoute
from eternal_guesses.routes.close_game import CloseGameRoute
from eternal_guesses.routes.create import CreateRoute
from eternal_guesses.routes.delete_guess import DeleteGuessRoute
from eternal_guesses.routes.edit_guess import EditGuessRoute
from eternal_guesses.routes.guess import GuessRoute
from eternal_guesses.routes.guild_info import GuildInfoRoute
from eternal_guesses.routes.list_games import ListGamesRoute
Expand All @@ -17,6 +19,7 @@
from eternal_guesses.routes.remove_management_channel import RemoveManagementChannelRoute
from eternal_guesses.routes.remove_management_role import RemoveManagementRoleRoute
from eternal_guesses.util.discord_messaging import DiscordMessaging, DiscordMessagingImpl
from eternal_guesses.util.game_post_manager import GamePostManager, GamePostManagerImpl
from eternal_guesses.util.message_provider import MessageProviderImpl, MessageProvider


Expand All @@ -38,6 +41,12 @@ def _router() -> Router:
message_provider = _message_provider()
command_authorizer = _command_authorizer(configs_repository=configs_repository)

game_post_manager = GamePostManagerImpl(
games_repository=games_repository,
message_provider=message_provider,
discord_messaging=discord_messaging,
)

route_handler = _route_handler(
command_authorizer=command_authorizer,
message_provider=message_provider,
Expand All @@ -51,7 +60,7 @@ def _router() -> Router:
)
guess_route = _guess_route(
games_repository=games_repository,
discord_messaging=discord_messaging,
game_post_manager=game_post_manager,
message_provider=message_provider
)
close_game_route = _close_game_route(
Expand Down Expand Up @@ -88,6 +97,16 @@ def _router() -> Router:
configs_repository=configs_repository,
message_provider=message_provider,
)
edit_guess_route = _edit_guess_route(
games_repository=games_repository,
message_provider=message_provider,
game_post_manager=game_post_manager,
)
delete_guess_route = _delete_guess_route(
games_repository=games_repository,
message_provider=message_provider,
game_post_manager=game_post_manager,
)

return RouterImpl(
route_handler=route_handler,
Expand All @@ -102,6 +121,8 @@ def _router() -> Router:
remove_management_channel_route=remove_management_channel_route,
add_management_role_route=add_management_role_route,
remove_management_role_route=remove_management_role_route,
edit_guess_route=edit_guess_route,
delete_guess_route=delete_guess_route,
)


Expand Down Expand Up @@ -139,12 +160,12 @@ def _create_route(games_repository: GamesRepository,


def _guess_route(games_repository: GamesRepository,
discord_messaging: DiscordMessaging,
game_post_manager: GamePostManager,
message_provider: MessageProvider):
return GuessRoute(
games_repository=games_repository,
discord_messaging=discord_messaging,
message_provider=message_provider
message_provider=message_provider,
game_post_manager=game_post_manager,
)


Expand Down Expand Up @@ -212,6 +233,30 @@ def _remove_management_role_route(
)


def _edit_guess_route(
games_repository: GamesRepository,
message_provider: MessageProvider,
game_post_manager: GamePostManager,
):
return EditGuessRoute(
games_repository=games_repository,
message_provider=message_provider,
game_post_manager=game_post_manager,
)


def _delete_guess_route(
games_repository: GamesRepository,
message_provider: MessageProvider,
game_post_manager: GamePostManager,
):
return DeleteGuessRoute(
games_repository=games_repository,
message_provider=message_provider,
game_post_manager=game_post_manager,
)


def _games_repository() -> GamesRepository:
return GamesRepositoryImpl()

Expand Down
Loading

0 comments on commit f1fb38a

Please sign in to comment.