I’m working on a Discord bot that sends coordinates to a chat channel, but it stops functioning after two minutes with the error indicating that the task has been terminated. I’m not very experienced with Python, so I would appreciate it if you could explain the issue to me in detail.
import discord
import asyncio
import time
import datetime
from skiplagged import Skiplagged
client = discord.Client()
pokemon_locator = Skiplagged()
async def fetch_pokemon():
target_channel = discord.Object(id='channel_id')
area_bounds = (
(40.7635, -73.9865), # Southwest corner
(40.7855, -73.9581) # Northeast corner
) # Coordinates for Central Park, NYC
try:
print(pokemon_locator.login_with_pokemon_trainer('trainer_id', 'trainer_pw'))
print(pokemon_locator.get_api_endpoint())
print(pokemon_locator.get_account_profile())
for pokemon in pokemon_locator.search_pokemon(area_bounds):
timestamp = time.time()
formatted_time = datetime.datetime.fromtimestamp(timestamp).strftime('%H:%M:%S')
message = f'[{formatted_time}] {str(pokemon)}'
await client.send_message(target_channel, message)
print(message)
except Exception as error:
print(str(error))
client.logout()
await asyncio.sleep(1)
async def background_worker():
target_channel = discord.Object(id='channel_id')
while not client.is_closed:
if not client.is_logged_in:
client.login('token-discord')
else:
await fetch_pokemon()
await asyncio.sleep(5)
client.loop.create_task(background_worker())
client.run('token-discord')
I’ve heard that the issue might stem from the skiplagged.py file due to the use of the requests library. Could you assist me in converting this script to utilize aiohttp instead? I really need to incorporate async functionality for the Pokemon searching feature in my bot, but I’m unsure how to implement it. Thank you for your assistance!