feat: вывести список команд после загрузки бота
- Добавить bot_ready = threading.Event() для синхронизации - Переместить bot_ready.set() после цикла загрузки команд - Добавить bot_ready.wait() в console_input() - Меню появляется только после 'Бот вошёл' и 'Загружен: ...'
This commit is contained in:
parent
5c6e09eb58
commit
d5911b226d
33
bot.py
33
bot.py
@ -18,6 +18,7 @@ intents.message_content = True
|
|||||||
|
|
||||||
bot = commands.Bot(command_prefix="!", intents=intents)
|
bot = commands.Bot(command_prefix="!", intents=intents)
|
||||||
stop_event = threading.Event()
|
stop_event = threading.Event()
|
||||||
|
bot_ready = threading.Event()
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
@ -28,6 +29,7 @@ async def on_ready():
|
|||||||
await bot.add_cog(cog)
|
await bot.add_cog(cog)
|
||||||
for cog in bot.cogs:
|
for cog in bot.cogs:
|
||||||
print(f" Загружен: {cog}")
|
print(f" Загружен: {cog}")
|
||||||
|
bot_ready.set()
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
@ -44,22 +46,29 @@ async def msg(ctx, *, text: str):
|
|||||||
|
|
||||||
|
|
||||||
def console_input():
|
def console_input():
|
||||||
|
bot_ready.wait()
|
||||||
|
print("Доступные команды:")
|
||||||
|
available = {k: v for k, v in ALL_CONSOLE_COMMANDS.items() if k != "stop"}
|
||||||
|
for idx, (name, func) in enumerate(available.items(), 1):
|
||||||
|
print(f" {idx}. {name}")
|
||||||
|
print(" 0. stop")
|
||||||
|
|
||||||
while not stop_event.is_set():
|
while not stop_event.is_set():
|
||||||
try:
|
try:
|
||||||
cmd = input().strip().lower()
|
choice = input("\nВыберите команду (номер): ").strip()
|
||||||
if cmd.startswith("!"):
|
if choice == "0":
|
||||||
cmd_name = cmd[1:]
|
print("\nОстановка бота...")
|
||||||
if cmd_name == "stop":
|
stop_event.set()
|
||||||
print("\nОстановка бота...")
|
break
|
||||||
if "stop" in ALL_CONSOLE_COMMANDS:
|
try:
|
||||||
ALL_CONSOLE_COMMANDS["stop"](stop_event, bot)
|
idx = int(choice)
|
||||||
break
|
if 0 < idx <= len(available):
|
||||||
elif cmd_name in ALL_CONSOLE_COMMANDS:
|
cmd_name = list(available.keys())[idx - 1]
|
||||||
ALL_CONSOLE_COMMANDS[cmd_name](stop_event, bot)
|
ALL_CONSOLE_COMMANDS[cmd_name](stop_event, bot)
|
||||||
else:
|
else:
|
||||||
print(f"Неизвестная команда: {cmd}")
|
print(f"Неизвестная команда: {choice}")
|
||||||
elif cmd:
|
except (ValueError, IndexError):
|
||||||
print(f"Неизвестная команда: {cmd}")
|
print(f"Неизвестная команда: {choice}")
|
||||||
except (EOFError, KeyboardInterrupt):
|
except (EOFError, KeyboardInterrupt):
|
||||||
stop_event.set()
|
stop_event.set()
|
||||||
try:
|
try:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user