Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance message holder to be able to play multiple games #184

Merged
merged 5 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions dnd-bot/dnd_bot/dc/cogs/command_create.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import nextcord
from nextcord import slash_command
from nextcord.ext.commands import Cog, Bot

Expand All @@ -14,16 +15,17 @@ def __init__(self, bot: Bot):
self.bot = bot

@slash_command(name="create", description="Creates new lobby")
async def create(self, interaction):
async def create(self, interaction: nextcord.Interaction):
try:
token, user = await HandlerCreate.create_lobby(interaction.user.id, interaction.user.dm_channel,
interaction.user.name)

await Messager.send_dm_message(interaction.user.id,
await Messager.send_dm_message(interaction.user.id, token,
f'You have successfully created a lobby! Game token: `{token}`')

view = ViewHost(interaction.user.id, token)
await Messager.send_dm_message(user_id=interaction.user.id,
token=token,
content=None,
embeds=[MessageTemplates.lobby_view_message_template(token, [user])],
view=view)
Expand All @@ -32,7 +34,7 @@ async def create(self, interaction):
await interaction.response.send_message(f"Hello {interaction.user.mention}!", view=view,
embed=MessageTemplates.lobby_creation_message(token))
except DiscordDndBotException as e:
await Messager.send_dm_error_message(user_id=interaction.user.id, content=str(e))
await Messager.send_error_message(interaction.channel_id, content=str(e))


def setup(bot):
Expand Down
7 changes: 4 additions & 3 deletions dnd-bot/dnd_bot/dc/cogs/command_join.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio

import nextcord
from nextcord.ext.commands import Cog, Bot
from nextcord import slash_command

Expand All @@ -16,7 +17,7 @@ def __init__(self, bot: Bot):
self.bot = bot

@slash_command(name="join", description="Joins to the lobby by its id")
async def join(self, interaction, token: str):
async def join(self, interaction: nextcord.Interaction, token: str):
try:
if interaction.user.dm_channel is None:
await interaction.user.create_dm()
Expand All @@ -27,7 +28,7 @@ async def join(self, interaction, token: str):

# send messages about successful join operation
lobby_view_embed = MessageTemplates.lobby_view_message_template(token, lobby_players)
await Messager.send_dm_message(interaction.user.id,
await Messager.send_dm_message(interaction.user.id, token,
f"Welcome to lobby of game {token}.\n"
f"Number of players in lobby: **{len(lobby_players)}**",
embeds=[lobby_view_embed],
Expand All @@ -40,7 +41,7 @@ async def join(self, interaction, token: str):
await q.join()

except DiscordDndBotException as e:
await Messager.send_dm_error_message(user_id=interaction.user.discord_id, content=str(e))
await Messager.send_dm_error_message(user_id=interaction.user.discord_id, token=token, content=str(e))


def setup(bot):
Expand Down
5 changes: 3 additions & 2 deletions dnd-bot/dnd_bot/dc/cogs/command_start.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import nextcord
from nextcord.ext.commands import Cog, Bot
from nextcord import slash_command
from dnd_bot.dc.ui.messager import Messager
Expand All @@ -13,15 +14,15 @@ def __init__(self, bot: Bot):
self.bot = bot

@slash_command(name="start", description="Exits from lobby and starts game")
async def start(self, interaction, token: str):
async def start(self, interaction: nextcord.Interaction, token: str):
try:
lobby_players_identities = await HandlerStart.start_game(token, interaction.user.id)

await interaction.response.send_message('Starting the game!', ephemeral=True)

# send messages about successful start operation
for user in lobby_players_identities:
await Messager.send_dm_message(user, "Game has started successfully!\n")
await Messager.send_dm_message(user, token, "Game has started successfully!\n")

GameStart.start(token)
await GameLoop.start_loop(token)
Expand Down
2 changes: 1 addition & 1 deletion dnd-bot/dnd_bot/dc/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ async def on_error(event_name, *args, **kwargs):

await interaction.response.defer()

await Messager.send_dm_error_message(user_id=interaction.user.id, content='', embeds=[error_embed])
await interaction.user.send(content='', embeds=[error_embed])
37 changes: 19 additions & 18 deletions dnd-bot/dnd_bot/dc/ui/messager.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async def __send_dm_message(user: User, content: str | None, embeds: list[Embed]
return sent_message

@staticmethod
async def send_dm_message(user_id: int, content: str | None, embeds=None, view=None, files=None):
async def send_dm_message(user_id: int, token: str, content: str | None, embeds=None, view=None, files=None):
"""
method used to send a direct message to a user
"""
Expand All @@ -77,40 +77,40 @@ async def send_dm_message(user_id: int, content: str | None, embeds=None, view=N

sent_message = await Messager.__send_dm_message(user, content, embeds, view, files)

MessageHolder.register_last_message_data(user_id, user.dm_channel.id, sent_message.id)
MessageHolder.register_last_message_data(user_id, token, user.dm_channel.id, sent_message.id)

@staticmethod
async def send_dm_error_message(user_id: int, content: str, embeds=None, view=None, files=None):
async def send_dm_error_message(user_id: int, token: str, content: str, embeds=None, view=None, files=None):
"""
method used to send a direct error message to user
note: content will be preceded by a warning emoji
"""
if embeds is None:
embeds = []
await Messager.delete_last_user_error_message(user_id)
await Messager.delete_last_user_error_message(user_id, token)

user = Messager.bot.get_user(user_id)

sent_message = await Messager.__send_dm_message(user, f'⚠️ {content}' if content != '' else None,
embeds, view, files)

MessageHolder.register_last_error_data(user_id, user.dm_channel.id, sent_message.id)
MessageHolder.register_last_error_data(user_id, token, user.dm_channel.id, sent_message.id)

@staticmethod
async def send_dm_information_message(user_id: int, content: str, embeds=None, view=None, files=None):
async def send_dm_information_message(user_id: int, token: str, content: str, embeds=None, view=None, files=None):
"""
method used to send a direct error message to user
note: content will be preceded by an information emoji
"""
if embeds is None:
embeds = []
await Messager.delete_last_user_error_message(user_id)
await Messager.delete_last_user_error_message(user_id, token)

user = Messager.bot.get_user(user_id)

sent_message = await Messager.__send_dm_message(user, f'ℹ️ {content}', embeds, view, files)

MessageHolder.register_last_error_data(user_id, user.dm_channel.id, sent_message.id)
MessageHolder.register_last_error_data(user_id, token, user.dm_channel.id, sent_message.id)

@staticmethod
async def edit_message(channel_id: int, message_id: int, new_content: str):
Expand All @@ -120,11 +120,12 @@ async def edit_message(channel_id: int, message_id: int, new_content: str):
await message.edit(content=new_content, embeds=message.embeds)

@staticmethod
async def edit_last_user_message(user_id: int, content: str = '', embeds: list[Embed] | None = None, view=None,
async def edit_last_user_message(user_id: int, token: str, content: str = '', embeds: list[Embed] | None = None, view=None,
files=None, retain_view=False):
"""
edit message
:param user_id: id of user to which the message will be sent to
:param token: game token
:param content: message as a string
:param embeds: list of zero or more embeds to attach to the message
:param view: nextcord View will be attached to the message
Expand All @@ -134,7 +135,7 @@ async def edit_last_user_message(user_id: int, content: str = '', embeds: list[E
if embeds is None:
embeds = []

channel_id, message_id = MessageHolder.read_last_message_data(user_id)
channel_id, message_id = MessageHolder.read_last_message_data(user_id, token)

channel = Messager.bot.get_channel(channel_id)
message = await channel.fetch_message(message_id)
Expand All @@ -147,13 +148,13 @@ async def edit_last_user_message(user_id: int, content: str = '', embeds: list[E
await message.edit(content=str(content), embeds=embeds, view=view)

@staticmethod
async def edit_last_user_error_message(user_id: int, content: str):
async def edit_last_user_error_message(user_id: int, token: str, content: str):
"""
edits last user error or information message
"""
error_data = MessageHolder.read_last_error_data(user_id)
error_data = MessageHolder.read_last_error_data(user_id, token)
if error_data is not None:
MessageHolder.delete_last_error_data(user_id)
MessageHolder.delete_last_error_data(user_id, token)
await Messager.edit_message(error_data[0], error_data[1], f'⚠️ {content}')

@staticmethod
Expand All @@ -163,22 +164,22 @@ async def __delete_message(channel_id, message_id):
await message.delete()

@staticmethod
async def delete_last_user_message(user_id: int):
channel_id, message_id = MessageHolder.read_last_message_data(user_id)
async def delete_last_user_message(user_id: int, token: str):
channel_id, message_id = MessageHolder.read_last_message_data(user_id, token)
channel = Messager.bot.get_channel(channel_id)
message = await channel.fetch_message(message_id)
await message.delete()

@staticmethod
async def delete_last_user_error_message(user_id: int):
async def delete_last_user_error_message(user_id: int, token: str):
"""deletes last user error message and data about it in MessageHolder.
If there were no error messages the function does nothing"""
# check for previous error messages
error_data = MessageHolder.read_last_error_data(user_id)
error_data = MessageHolder.read_last_error_data(user_id, token)

# delete error messages
if error_data is not None:
MessageHolder.delete_last_error_data(user_id)
MessageHolder.delete_last_error_data(user_id, token)
await Messager.__delete_message(error_data[0], error_data[1])

@staticmethod
Expand Down
17 changes: 12 additions & 5 deletions dnd-bot/dnd_bot/dc/ui/views/view_character_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ async def callback(self, interaction: nextcord.Interaction):
ChosenAttributes.chosen_attributes[self.user_id]['name'] = self.name_textbox.value
ChosenAttributes.chosen_attributes[self.user_id]['backstory'] = self.backstory_textbox.value
await Messager.edit_last_user_message(user_id=self.user_id,
token=self.token,
embeds=[MessageTemplates.alignment_form_view_message_template()],
view=ViewAlignmentForm(self.user_id, self.token))

Expand Down Expand Up @@ -131,10 +132,11 @@ async def next(self, button: nextcord.ui.Button, interaction: nextcord.Interacti
try:
await HandlerAlignment.handle_next(self)
await Messager.edit_last_user_message(user_id=self.user_id,
token=self.token,
embeds=[MessageTemplates.class_form_view_message_template()],
view=ViewClassForm(self.user_id, self.token))
except DiscordDndBotException as e:
await Messager.send_dm_error_message(user_id=self.user_id, content=str(e))
await Messager.send_dm_error_message(user_id=self.user_id, token=self.token, content=str(e))


class ViewClassForm(nextcord.ui.View):
Expand Down Expand Up @@ -164,6 +166,7 @@ def __init__(self, user_id, token):
async def back(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
await HandlerClass.handle_back(self)
await Messager.edit_last_user_message(user_id=self.user_id,
token=self.token,
embeds=[MessageTemplates.alignment_form_view_message_template()],
view=ViewAlignmentForm(self.user_id, self.token))

Expand All @@ -172,10 +175,11 @@ async def next(self, button: nextcord.ui.Button, interaction: nextcord.Interacti
try:
await HandlerClass.handle_next(self)
await Messager.edit_last_user_message(user_id=self.user_id,
token=self.token,
embeds=[MessageTemplates.race_form_view_message_template()],
view=ViewRaceForm(self.user_id, self.token))
except DiscordDndBotException as e:
await Messager.send_dm_error_message(user_id=self.user_id, content=str(e))
await Messager.send_dm_error_message(user_id=self.user_id, token=self.token, content=str(e))


class ViewRaceForm(nextcord.ui.View):
Expand Down Expand Up @@ -205,6 +209,7 @@ def __init__(self, user_id, token):
async def back(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
await HandlerRace.handle_back(self)
await Messager.edit_last_user_message(user_id=self.user_id,
token=self.token,
embeds=[MessageTemplates.class_form_view_message_template()],
view=ViewClassForm(self.user_id, self.token))

Expand All @@ -213,10 +218,11 @@ async def confirm(self, button: nextcord.ui.Button, interaction: nextcord.Intera
try:
await HandlerRace.handle_confirm(self)
await Messager.edit_last_user_message(user_id=self.user_id,
token=self.token,
embeds=[MessageTemplates.stats_retrospective_form_view_message_template(self.user_id)],
view=ViewStatsRetrospectiveForm(self.user_id, self.token))
except DiscordDndBotException as e:
await Messager.send_dm_error_message(user_id=self.user_id, content=str(e))
await Messager.send_dm_error_message(user_id=self.user_id, token=self.token, content=str(e))


class ViewStatsRetrospectiveForm(nextcord.ui.View):
Expand All @@ -233,6 +239,7 @@ async def reroll(self, button: nextcord.ui.Button, interaction: nextcord.Interac
button.disabled = True
await HandlerCharacterCreation.assign_attribute_values(self.user_id)
await Messager.edit_last_user_message(user_id=self.user_id,
token=self.token,
embeds=[MessageTemplates.stats_retrospective_form_view_message_template(
self.user_id)],
view=self)
Expand All @@ -242,5 +249,5 @@ async def confirm(self, button: nextcord.ui.Button, interaction: nextcord.Intera
try:
await HandlerStatsRetrospective.handle_confirm(self)
except DiscordDndBotException as e:
await Messager.delete_last_user_error_message(self.user_id)
await Messager.send_dm_error_message(user_id=self.user_id, content=str(e))
await Messager.delete_last_user_error_message(self.user_id, self.token)
await Messager.send_dm_error_message(user_id=self.user_id, token=self.token, content=str(e))
Loading