F
F
fjhghfj542021-10-13 00:40:05
Python
fjhghfj54, 2021-10-13 00:40:05

When starting the bot, it gives an error AttributeError: 'NoneType' object has no attribute 'create_task' how to fix?

here is the code main.py

import sqlite3
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
from aiogram.types import ReplyKeyboardRemove, \
ReplyKeyboardMarkup, KeyboardButton, \
InlineKeyboardMarkup, InlineKeyboardButton
import asyncio
import config
from pyrogram import Client
from requests_html import HTMLSession
from time import sleep
from func import *

loop = asyncio.get_event_loop()
bot = Bot(token=config.token, loop=loop)
dp = Dispatcher(bot)

client = Client("fsfdsf", config.api_id, config.api_hash)
client.start()

async def get_channels():
while True:
await asyncio.sleep(1)
session = HTMLSession()
for _ in get_m():
_ = _[0]
r = session.get(f' https://t.me/ {_ }')
if '' in r.text:
detele_monitoring(_)
channel = await client.create_channel("tfdgfdgsdfdshfdgaaa")
await client.update_chat_username(channel.id, _)
for i in config.admins:
await bot.send_message (i, f" The bot found an empty link [@{_}] and replaced it successfully. ", parse_mode="HTML")
else:
print('Nothing found', _)

@dp.message_handler(commands=['start '])
async def process_start_command(m: types.Message):
if m.chat.id in config.admins:
text = " Cart login styler: \n\n"\
f" Channels viewed: {len(get_m())}pcs . \n"
button = KeyboardButton('➕ Add Channels')
button1 = KeyboardButton('Home')
keyboard = ReplyKeyboardMarkup(resize_keyboard=True)
keyboard.add(button)
keyboard.add(button1)
await bot.send_message(m.chat .id, text,reply_markup=keyboard, parse_mode="HTML")
else:
await bot.send_message(m.chat.id, " ❌ You are not allowed to use this bot. ", parse_mode="HTML")

@dp.message_handler()
async def echo_message(m: types.Message):
if m.text == '➕ Add channels':
await bot.send_message(m.chat.id, ' Enter channels line by line, each line a new channel. (With @) ' , parse_mode="HTML")
elif m.text == ' Main':
await process_start_command(m)
elif "@" in m.text:
for _ in m.text.split('\n'):
if "@" in _:
add_m(_.split("@")[1])
await bot.send_message(m.chat.id, f'✅ Channel "{_}" was successfully added. ', parse_mode="HTML")
else :
pass


if __name__ == '__main__':
dp.loop.create_task(get_channels())
executor.start_polling(dp)


Error:
Traceback (most recent call last):
File "main.py", line 69, in
dp.loop.create_task(get_channels())
AttributeError: 'NoneType' object has no attribute 'create_task'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-10-13
@fjhghfj54

dp.loop.create_task(get_channels())
At this point, the call to the dispatcher has not yet started, so there is no work cycle (loop) for it. I guess create_task() should be called inside some event handler so you can be sure the work loop is already running.
If the aiogram API has an event that fires when connected to the network, that event handler would be the appropriate place.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question