I’m facing difficulties while developing my Discord bot, particularly with async functions. I created a command to initiate a monitoring task, but whenever I attempt to invoke another async function, I receive a warning stating that the coroutine was never awaited.
Here’s the code I have:
@bot.command(name='start_monitor')
async def initiate_monitoring(ctx, ip_address):
active_ips.append(ip_address)
await ctx.send(f'Monitoring is now active for: {ip_address}')
print(f'Active IPs: {active_ips}')
if len(active_ips) == 1:
monitor_ip_status() # This is where I encounter the warning
print('Beginning monitoring procedure.')
async def monitor_ip_status():
global previous_response
global is_monitoring
for ip in active_ips:
if is_monitoring:
while True:
stop_loop = False
response_data = requests.get(api_url.format(ip)).json()
info_message = f"IP Status:\nStarted at: {response_data['start_time']}\nLoad: {response_data['load']}%\nUp Time: {response_data['up_time']} hours"
current_response = response_data.get('load')
if current_response == previous_response:
print('No updates in status.')
time.sleep(10)
else:
previous_response = current_response
print('Status update available, sending message!')
try:
bot.send_message(info_message)
except Exception:
bot.send_message("Monitoring has ended!")
if len(active_ips) == 0:
stop_loop = True
active_ips.remove(ip)
time.sleep(10)
if stop_loop:
break
I’ve attempted to use await
but it doesn’t seem to fit well. I also examined how to create tasks with the event loop, but I’m confused about the difference between coroutines and futures. Can someone explain the proper way to call an async function from another async function in this situation?