When running my Discord bot I get a RuntimeError: no running event loop

Question:

This is my code, when I try to run it on my Raspberry Pi (3) it appears to have an issue with no running event loop. I’ve looked for answers a lot on this website but never found one so I decided to make a post. Anyways, hope someone could help me here…

from discord.ext import tasks
import discord
import requests

client = discord.Client(intents=discord.Intents.all())
slash = SlashCommand(client, sync_commands=True)

guild_ids = [1018805336003584070]

@client.event
async def on_ready():
    activity = 
discord.Activity(type=discord.ActivityType.watching, 
name="Fortnite APIs")
await client.change_presence(status=discord.Status.online, activity=activity)

@tasks.loop(seconds=10)
async def version_update_background_task():
  aes = requests.get("https://fortnitecentral.gmatrixgames.ga/api/v1/aes")
  version = aes.json()["version"]
  mainaes = aes.json()["mainKey"]
  channel = client.get_channel(1018805336917950487)
  if version != "21.51":
    await channel.send(f"@here NEW VERSION DETECTED: Fortnite 
v{version} is now avaliable for download!nn> Main Key: {mainaes}")
    url = 'https://fortnite-api.com/images/map.png'
    req = requests.get(url)
    with open('images/Map.png', 'wb') as f:
        f.write(req.content)
        await channel.send("New Fortnite Map Detected!", 
        file=discord.File('images/Map.png'))
        version_update_background_task.end()

@tasks.loop(seconds=10)
async def lightswitch_task():
  lightswitch = requests.get("https://fn-api.com/api/status")
  onoff = lightswitch.json()["data"]["status"]
  message = lightswitch.json()["data"]["message"]
  channel = client.get_channel(1018805336917950487)
  if onoff != "UP":
    await channel.send(f"@here {message}nn> Fortnite servers 
are now {onoff} for maintenance!", 
file=discord.File('images/BEGUN.png'))
    lightswitch_task.end()

@lightswitch_task.before_loop
async def lightswitch_task_before_loop():
    await client.wait_until_ready() 
@version_update_background_task.before_loop
async def version_update_background_task_before_loop():
    await client.wait_until_ready() 

version_update_background_task.start()
lightswitch_task.start()
client.run("no")

The traceback is:

Traceback (most recent call last):
  File "/home/monksbot/testfordiscordbot.py", line 41, in <module>
    version_update_background_task.start()
  File "/home/monksbot/.local/lib/python3.9/site- 
packages/discord/ext/tasks/__init__.py", line 398, in start
    self._task = asyncio.create_task(self._loop(*args, **kwargs))
  File "/usr/lib/python3.9/asyncio/tasks.py", line 360, in 
create_task
    loop = events.get_running_loop()
RuntimeError: no running event loop
Asked By: Monks

||

Answers:

It would help if you could share the Traceback, although I have never worked with discord modules before. But I am pretty sure that you might need to await the async functions.

Answered By: Rajesh Majumdar

Your initialization of a loop is wrong, it should be:

client.loop.create_task(lightswitch_task())
client.loop.create_task(version_update_background_task())
client.run()
Answered By: walker