мне выдает ошибку при использовании команды старт, и не добавляет меня в бд, хотя раньше все работало(aiogram)
Когда я пишу команду "старт" мне выдает ошибку и не добавляет в бд, вот ошибка:
RuntimeWarning: coroutine 'add_user' was never awaited
return await wrapped()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
вот код:
from database.models import async_session
from database.models import User, Ticket
from sqlalchemy import select, update, delete, desc
from datetime import datetime
async def add_user(tg_id):
async with async_session() as session:
user = await session.scalar(select(User).where(User.tg_id == tg_id))
if not user:
session.add(User(tg_id=tg_id))
await session.commit()
elif user.name:
return True
return False
async def edit_user(tg_id, name, username=None):
async with async_session() as session:
user = await session.execute(update(User).where(User.tg_id == tg_id).values(name=name,
username=username))
await session.commit()
async def add_ticket(text, tg_id):
async with async_session() as session:
user = await session.scalar(select(User).where(User.tg_id == tg_id))
session.add(Ticket(text=text, user=user))
await session.commit()
async def get_tickets():
async with async_session() as session:
tickets = await session.scalars(select(Ticket))
return tickets
async def get_ticket(ticket_id):
async with async_session() as session:
ticket = await session.scalar(select(Ticket).where(Ticket.id == ticket_id))
return ticket
async def get_user(u_id):
async with async_session() as session:
user = await session.scalar(select(User).where(User.id == u_id))
return user
async def delete_ticket(ticket_id):
async with async_session() as session:
await session.execute(delete(Ticket).where(Ticket.id == ticket_id))
await session.commit()
и команда старт
from aiogram import Router, F
from aiogram.fsm.context import FSMContext
from aiogram.fsm.state import State, StatesGroup
from aiogram.filters import CommandStart
from aiogram.types import Message, CallbackQuery, ReplyKeyboardRemove
from database import request as rq
from Key import new_ticket, cancellation
user = Router()
class Process(StatesGroup):
name = State()
question = State()
question2 = State()
@user.message(CommandStart())
async def cmd_start(message: Message, state: FSMContext):
user = rq.add_user(message.from_user.id)
if not user:
await message.answer('Введите ваше имя')
await state.set_state(Process.name)
else:
await message.answer('Отправить запрос можно по кнопке ниже!', reply_markup=new_ticket)
@user.message(Process.name)
async def get_name(message: Message, state: FSMContext):
await state.update_data(name=message.text)
await state.set_state(Process.question)
await message.answer('yfgbibnt cdjt bz')
@user.message(Process.question)
async def get_question(message: Message, state: FSMContext):
user = await state.get_data()
await rq.edit_user(message.from_user.id,
user['name'],
message.from_user.username)
await rq.add_ticket(message.text, message.from_user.id)
await message.answer('ffbfbfbfccffbcfbfcbk;llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll'
'llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll'
'llllllllllllllllllllllll')
@user.callback_query(F.data == 'cancellation')
async def cancellation1(callback: CallbackQuery, state: FSMContext):
await callback.answer()
await callback.message.edit_text('действие отменено')
await state.clear()
@user.message(F.text == 'Новый запрос')
async def new_question(message: Message, state: FSMContext):
await state.set_state(Process.question2)
await message.answer('Напишите свой запрос', reply_markup=ReplyKeyboardRemove() and cancellation)
@user.message(Process.question2)
async def get_question(message: Message, state: FSMContext):
await rq.add_ticket(message.text, message.from_user.id)
await message.answer('Ваше обращение отправлено, ответ будет в течении часа', reply_markup=ReplyKeyboardRemove())
await state.clear()
Ответы (1 шт):
Автор решения: Deniska SosiSka
→ Ссылка
Вы запускайте корутину, но не дожидайтесь её завершения. Замените:
@user.message(CommandStart())
async def cmd_start(message: Message, state: FSMContext):
user = rq.add_user(message.from_user.id)
на:
@user.message(CommandStart())
async def cmd_start(message: Message, state: FSMContext):
user = await rq.add_user(message.from_user.id)