Zagir-vip
@Zagir-vip
Web dev, Game dev, app dev, Разработчик на Python!

Как в discord_slash добавить кнопки на Python?

Пытался много вариантов, но получал:

File "bot.py", line 661, in calc
    row = create_actionrow(btn_numbers)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord_slash\utils\manage_components.py", line 24, in create_actionrow
    ComponentType.select in [component["type"] for component in components]
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord_slash\utils\manage_components.py", line 24, in <listcomp>
    ComponentType.select in [component["type"] for component in components]
TypeError: list indices must be integers or slices, not str


КОД:
import discord_slash
from discord_slash import SlashCommand, SlashContext
from discord_slash.utils import manage_components
from discord_slash.utils.manage_commands import *
from discord_slash.utils.manage_components import *
from discord_slash.model import ButtonStyle

@slash.slash(name='calc')
async def calc(ctx):
	btns= [
		[
			create_button(style=ButtonStyle.gray, label="Test"),
                  ]
	]

	row = create_actionrow(btns)

	await ctx.send("Нажимайте на кнопки.", components=[row])

Подскажите что можно сделать?

РЕШЕНИЕ:
create_actionrow - принимает только кнопки, а не список поэтому
	row1 = create_actionrow(
			create_button(style=ButtonStyle.gray, label="%"),
			create_button(style=ButtonStyle.gray, label="("),
			create_button(style=ButtonStyle.gray, label=")"),
			create_button(style=ButtonStyle.gray, label="÷"),
			create_button(style=ButtonStyle.red, label="←")
		)
	row2 = create_actionrow(
			create_button(style=ButtonStyle.blue, label="7"),
			create_button(style=ButtonStyle.blue, label="8"),
			create_button(style=ButtonStyle.blue, label="9"),
			create_button(style=ButtonStyle.gray, label="×"),
			create_button(style=ButtonStyle.red, label="clear")
		)
	row3 = create_actionrow(
			create_button(style=ButtonStyle.blue, label="4"),
			create_button(style=ButtonStyle.blue, label="5"),
			create_button(style=ButtonStyle.blue, label="6"),
			create_button(style=ButtonStyle.gray, label="-"),
			create_button(style=ButtonStyle.red, label="Выход")
		)
	row4 = create_actionrow(
			create_button(style=ButtonStyle.blue, label="1"),
			create_button(style=ButtonStyle.blue, label="2"),
			create_button(style=ButtonStyle.blue, label="3"),
			create_button(style=ButtonStyle.gray, label="+")
		)
	row5 = create_actionrow(
			create_button(style=ButtonStyle.gray, label="00"),
			create_button(style=ButtonStyle.blue, label="0"),
			create_button(style=ButtonStyle.gray, label="."),
			create_button(style=ButtonStyle.green, label="=")
		)


	message_btns = await ctx.send("Нажимайте на кнопки.", components=[row1, row2, row3, row4, row5])
  • Вопрос задан
  • 313 просмотров
Пригласить эксперта
Ответы на вопрос 1
phaggi
@phaggi Куратор тега Python
лужу, паяю, ЭВМы починяю
Судя по ошибке, код пытается обратиться к объекту component как к словарю, а там - список.
Почему так происходит? Возможно, тут components=[row]лишние квадратные скобки.

Возможно, функция create_actionrow(btns) должна возвращать другой тип (сейчас возвращает список чего-то, надо посмотреть внимательно).
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы