D
D
Denze2022-04-03 00:16:54
Python
Denze, 2022-04-03 00:16:54

How to write vkbottle state selection into a separate function?

@bot.on.private_message(payload = {"cmd":"yeslvlup"})
async def yeslvlup(message: Message):
    lvl = 1+base.output_lvl(message.peer_id)
    base.input_lvl(message.peer_id,lvl)
    if lvl == 2:
        await bot.state_dispenser.set(message.peer_id, func.Data.SecondLvl)
    elif lvl == 3:
        await bot.state_dispenser.set(message.peer_id, func.Data.ThirdLvl)
    elif lvl == 4:
        await bot.state_dispenser.set(message.peer_id, func.Data.FourthLvl)
    elif lvl == 5:
        await bot.state_dispenser.set(message.peer_id, func.Data.FifthLvl)
    elif lvl == 6:
        await bot.state_dispenser.set(message.peer_id, func.Data.SixthLvl)
    elif lvl == 7:
        await bot.state_dispenser.set(message.peer_id, func.Data.SeventhLvl)
    await message.text(
        "Уровень повышен",
        keyboard = Keyboard(one_time = True)
        .add(Text("Продолжить"), color = KeyboardButtonColor.PRIMARY)
        )

There is such a function. I want to move the level check with the choice of state into a separate function, because I use it more than once

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Krostelev, 2022-04-03
@Denze

Didn't work with vkbottle, but you can try this

async def set_state(peer_id, lvl):
  states = {
    2: func.Data.SecondLvl,
    3: func.Data.ThirdLvl,
    4: func.Data.FourthLvl,
    5: func.Data.FifthLvl,
    6: func.Data.SixthLvl,
    7: func.Data.SeventhLvl
  }
  await bot.state_dispenser.set(peer_id, states[lvl])

@bot.on.private_message(payload = {"cmd":"yeslvlup"})
async def yeslvlup(message: Message):
    lvl = 1+base.output_lvl(message.peer_id)
    base.input_lvl(message.peer_id,lvl)
    await set_state(message.peer_id, lvl)
    await message.text(
        "Уровень повышен",
        keyboard = Keyboard(one_time = True)
        .add(Text("Продолжить"), color = KeyboardButtonColor.PRIMARY)
        )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question