Во время разработки я столкнулся с проблемой:

Основываясь на моем понимании и некоторых результатах поиска на stackoverflow (в частности, этот вопрос), это происходит из-за импорта, который я сделал. Мои файлы импортируются друг из друга. Поэтому, когда Python пытается загрузить один, пока другой еще не завершен, происходит сбой с «частично инициализированным модулем». Я понял, что это своего рода проблема структурной зависимости. Поэтому я решил создать другое представление, которое вместо этого могли бы вызывать эти два представления.
Во-первых, вот логический поток, который я сделал для своего бота. Сначала я выполнил эту часть, где пользователь выбирает «Как играть», и другое представление, если пользователь хочет продолжить или выйти:

Я просто зациклил представления. Вот мои взгляды. Я добавлю только соответствующий код:
Код: Выделить всё
rps_views.pyКод: Выделить всё
import discord, datetime
from game.rock_paper_scissors.utils.game_logic import (
get_players_choice_value,
get_bots_choice_value,
rock_paper_scissors,
get_state_emoji
)
from game.rock_paper_scissors.views.rps_navigate import (
RpsNavigate
)
rps_nav_obj = RpsNavigate()
class RpsMainMenuView(discord.ui.View):
def __init__(self, *, timeout = 180):
super().__init__(timeout=timeout)
#How to play
@discord.ui.button(label="❔How to Play", style=discord.ButtonStyle.primary)
async def how_to_play(self, btn_interaction: discord.Interaction, button: discord.ui.Button):
await btn_interaction.response.send_message(
"Classic Rock, Paper, Scissors! Choose between the three, and we'll see if you win against me!",
view=rps_nav_obj.determine_view("HTP")
)
Код: Выделить всё
how_to_play_views.pyКод: Выделить всё
import discord
from game.rock_paper_scissors.views.rps_navigate import (
RpsNavigate
)
rps_nav_obj = RpsNavigate()
class HowToPlayView(discord.ui.View):
def __init__(self, *, timeout: float | None = 180):
super().__init__(timeout=timeout)
#Continue
@discord.ui.button(label="✅Continue", style=discord.ButtonStyle.success)
async def htp_continue(self, btn_interaction: discord.Interaction, button: discord.ui.Button):
await btn_interaction.response.send_message(
"Ready to Play Rock Paper Scissors?",
view=rps_nav_obj.determine_view("RPS")
)
#Exit
@discord.ui.button(label="🔚 Exit", style=discord.ButtonStyle.red)
async def htp_exit(self, btn_interaction:discord.Interaction, button: discord.ui.Button):
await btn_interaction.response.send_message(
"Goodbye!"
)
Код: Выделить всё
rps_navigate.pyКод: Выделить всё
import discord
class RpsNavigate(discord.ui.View):
def __init__(self):
self = self
def determine_view(self, view_choice):
self.view_choice = view_choice
print(f"[RPS Navigate] Determining views to be passed: {self.view_choice}")
#initialize views
print("Initializing views...")
from game.rock_paper_scissors.views.rps_views import (
RpsMainMenuView,
RpsMovesView
)
from game.rock_paper_scissors.views.how_to_play_views import (
HowToPlayView
)
rps_main_menu_view = RpsMainMenuView()
how_to_play_view = HowToPlayView()
rps_moves_view = RpsMovesView()
if self.view_choice == "RPS":
return rps_main_menu_view
elif self.view_choice == "HTP":
return how_to_play_view
elif self.view_choice == "RMV":
return rps_moves_view
else:
return rps_main_menu_view
Подробнее здесь: https://stackoverflow.com/questions/798 ... port-issue
Мобильная версия