K
K
kodxxdok2021-12-02 19:13:36
Python
kodxxdok, 2021-12-02 19:13:36

AttributeError: 'NoneType' object has no attribute 'create_task'?

import config
import logging
import asyncio
from datetime import datetime

from aiogram import Bot, Dispatcher, executor, types
from sqlighter import SQLighter

from stopgame import StopGame

# set logging level
logging.basicConfig(level=logging.INFO)

# initialize bot
bot = Bot(token =config.API_TOKEN)
dp = Dispatcher(bot)

# initialize database connection
db = SQLighter('db.db')

# initialize parser
sg = StopGame('lastkey.txt')

# Subscription activation command
@dp.message_handler(commands= ['subscribe'])
async def subscribe(message: types.Message):
if(not db.subscriber_exists(message.from_user.id)):
# if the user is not in the database, add him
db.add_subscriber(message.from_user.id)
else: # if he already exists, then just update his db
subscription status .
update_subscription(message.from_user.id, True)

await message.answer("You have successfully subscribed to the newsletter!\nWait, new reviews will be released soon and you will be the first to know about them =)")

# Unsubscribe command
@dp.message_handler(commands=[ 'unsubscribe'])
async def unsubscribe(message: types.Message):
if(not db.subscriber_exists(message.from_user.id)):
# if the user is not in the database, add him with an inactive subscription (remember)
db.add_subscriber( message.from_user.id, False)
await message.answer("You are not subscribed yet.")
else:
# if it already exists, just update its subscription status
db.update_subscription(message.from_user.id, False)
await message.answer("You have successfully unsubscribed from the mailing list .")

# check for new games and send
mailouts async def scheduled(wait_for):
while True:
await asyncio.sleep(wait_for)

# check for new games
new_games = sg.new_games()

if(new_games):
# if there are games, reverse the list and iterate
new_games.reverse()
for ng in new_games:
# parse new game
info nfo = sg.game_info(ng)

# get a list of bot subscribers
subscriptions = db.get_subscriptions()

# send news to everyone
with open(sg.download_image(nfo['image']), 'rb') as photo:
for s in subscriptions:
await bot.send_photo(
s[ 1],
photo,
caption = nfo['title'] + "\n" + "Score: " + nfo['score'] + "\n" + nfo['excerpt'] + "\n\n" + nfo['link'],
disable_notification = True
)

# update key
sg.update_lastkey(nfo['id'])

# start long polling
if __name__ == '__main__':
dp.loop.create_task(scheduled(10)) # leave 10 seconds for now (as a test) Here is the error
executor.start_polling(dp, skip_updates=True)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question